Standard Input

This document was written by CS 290W TA David Corcoran and was last modified

Perl handles STDIN (STanDard INput) differently than other programming languages. /* Common C++ Standard Input */ cin >> iAge; /* Common Java Standard Input */ DataInputStream din = new DataInputStream(System.in); String str = din.readLine() # Perl Standard Input # This reads all the way up until a newline. $iAge = <STDIN>; # This removes the newline character. chop ($iAge); Since all variables are scalars, Perl will read the data in as scalar data. It makes no difference if it is a String, Int, or Float. Perl will know what to do when an operation is performed on the variable. So it is feasible to take the above $iAge and perform arithmetic operations on it. For example, # Read Age from Standard Input $iAge = <STDIN>; chop($iAge); # suppose it is 12 $iAge = $iAge + 1; # $iAge is now 13 ++$iAge; # $iAge is now 14 $sWhatAge = "Dave is ".$iAge." years old"; # $sWhatAge is now "Dave is 14 years old" </UL>

[ Back to Main ]