Like HowStuffWorks on Facebook!

How Perl Works


Reading from STDIN

To read in data from the stdin (standard in), use the STDIN handle. For example:

   print "Enter high number: ";
   $i = <STDIN>;
   for ($j = 0; $j <= $i; $j++)
      print $j, "\n";

As long as you enter an integer number, this program will work as expected. <STDIN> reads a line at a time. You can also use getc to read one character, as in:

$i = getc(STDIN);

Or use read:

read(STDIN, $i, 1);

The 1 in the third parameter to the read command is the length of the input to read.

Reading Environment Variables

PERL defines a global hash named ENV, and you can use it to retrieve the values of environment variables. For example:

print $ENV{'PATH'};

Reading Command Line Arguments

PERL defines a global array ARGV, which contains any command line arguments passed to the script. $#ARGV is the number of arguments passed minus 1, $ARGV[0] is the first argument passed, $ARGV[1] is the second, and so on.

You should now be able to read and write simple Perl scripts. You should also be able to wade into the full documentation to learn more. For additional information, see the links on the next page.

More to Explore