1.5 Standard Input

 

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()

<i># Perl Standard Input</i>

# This reads all the way up until a newline.<br> <font color="#FF0000">$iAge = $iAge = &lt;STDIN&gt;; </font>

<STDIN>

# This removes the newline character.
<font color="#FF0000">chop ($iAge); </font>

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 <br> $iAge = &lt;STDIN&gt;; <br> chop($iAge); <br> # suppose it is 12

$iAge = $iAge + 1; <br> # $iAge is now 13

++$iAge;<br> # $iAge is now 14

$sWhatAge = "Dave is "$iAge." years old"; <br> # $sWhatAge is now "Dave is 14 years old"

[ Back to Main ]