How to Read Integers in Perl

How to Read Integers in Perl thumbnail
Perl can read integers and string data types, although they are both considered "scalar.""

Perl is a high-level programming language that has manipulation facilities that make it an excellent choice when creating dynamic HTML features in Web pages, creating server scripts and manipulating files and file contents. Perl is a “loosely typed” language so it does not have explicitly identified data types such as “string” or “integer”. Instead Perl responds appropriately to variable data types in context and treats variable values as integers when they are used in simple arithmetic or advanced mathematical functions.

Things You'll Need

  • text editor
  • Perl interpreter installed and properly configured
  • terminal emulator application (such as putty or terminal)
Show More

Instructions

    • 1

      Open a text editor and create a new file named perlInt.pl. To create new files in most text editors select “New” from the “File” menu.

    • 2

      Add the Perl “shebang” line to the file. The “shebang” line points to the Perl executable and is used to locate the Perl interpreter in cases where its location is not obvious. For example, on UNIX systems a typical shebang is “#!/usr/bin/perl”.

      #!/usr/bin/perl

    • 3

      Use the Perl “print” function to print the text “Number 1: .“ Follow the print function with a request for standard input (“<STDIN>”) and store the “<STDIN>” value (the user's input) in a variable named “$number1.”

      print "Number 1: ";
      $number1 = <STDIN>;

    • 4

      Use a second Perl “print” function to print the text “Number 2: .“ Follow the print function with a second request for standard input (“<STDIN>”) and store the second “<STDIN>” value in a variable named “$number2.”

      print "Number 2: ";
      $number2 = <STDIN>;

    • 5

      Add the value of the variable “$number1” to the value of the variable “$number2” using the Perl addition operator (“+”). Store the result in a variable named “$number3.”

      $number3 = $number1 + $number2;

    • 6

      Use a third Perl “print” function to print the value of the variable “$number3.” First print the text “The answer is” and use the Perl concatenation operator (“.”) to write out the value of the “$number3” variable. Follow the $number3 variable with a second concatenation operator and a return (“\n”). After step six perlInt.pl will appear as shown below:

      #!/usr/bin/perl
      print "Number 1: ";
      $number1 = <STDIN>;
      print "Number 2: ";
      $number2 = <STDIN>;
      $number3 = $number1 + $number2;
      print "The answer is: " . $number3 . "\n";

    • 7

      Execute perlInt.pl by opening a terminal emulator and typing the text “perl perlInt.pl” at the prompt. Enter two numbers when prompted and verify that the appropriate integer is written to the screen.

Tips & Warnings

  • Perl is a “loosely typed” language so reading integers and strings is equivalent; both types are scalar.

  • There are three ways to express a numeric literal in Perl: as an integer (no decimal point), as a float (with a decimal point) and with exponential notation.

  • Perl has three variable data types: scalar, arrays of scalars and hashes (associative arrays of scalars).

  • Generally conversion from one scalar form (string) to another (integer) is transparent in Perl.

  • To determine the Perl interpreter’s current location type “which perl” at the command prompt or ask your Web host.

  • In Perl, hexadecimal, octal and binary representations are not automatically converted to integers.

Related Searches:

References

Resources

  • Photo Credit Photos.com/Photos.com/Getty Images

Comments

Related Ads

Featured