3

3rd Perl Idiom

Just as there is a default standard output device (the screen), there is also a standard input device. What is this standard input device?

The Keyboard

Let's take in some standard input in our Perl program.

  1. #!/usr/bin/perl
  2. while(<>){
  3. print;
  4. }

    ruben@ruben:/home/ruben/perl_course
    > chmod 744 file3
    ruben@ruben:/home/ruben/perl_course
    > file3
    Hello
    Hello
    How are you
    How are you
    stop
    stop
    ruben@ruben:/home/ruben/perl_course >

This program is dubiously complex and the root idiom for much of Perls handling of standard input and standard output. It also introduces the concept of data control constructs and loops.

Concept 1- Retrieving STDIN <>

Angle Brackets <>, in this program capture input by the keyboard. It waits for input, storing each keystroke until it retrieves an <ENTER> or line feed character.

Concept 2- Conditional loops: while(){}

The while construction tests for a value in the parentheses. If the value is true, it then hands control to the curly braces, and then returns to reevaluate the expression in the parenthesis. If not, it hands control to the line following the closing curly brace.

Concept 3- System Variables

Just as a cup holds water, computer memory holds data. All the data which enters the computer is stored. This storage is most generally in the computers RAM. All data is entered and retrieved in one of 3 ways

- Randomly placed and retrieved in a mapped location.

- Serially stored in a location where the first data entered into the stream is later the first retrieved.

-Serially stacked in a location where the first data entered is the last data retrieved.

The most fundamental form of the data storage in RAM is Randomly assigned and retrieved. This process is "random" in the sense that it's placement location is not related to the order of memory locations around it. It's placed whereever it is convenient for the Computer.

Perl stores a unit of data in locations which are mapped to symbolic names. Together, the location and the symbol form a variable.

Computers, by the nature of their hardware design, understand a limited number of datatypes. These types are stored as multiple bytes in the computer such as 8,16,32, and 64 bits. 8 bits are in a byte. Each bit is a single on/off switch. Unix is generally a 32 bit operating system, and therefor prefers 32 bit (or 4 byte) data. Some of the fundamental data types on your computer include single byte chars, 2 bytes integers, and 2 byte floating point numbers.

When you run your Perl program, it waits for a stream of data to be entered from your standard input device (the keyboard). That data is sent as one 8 bit ASCII encoded character at a time and stored by Perl in our program.

Where is stores? What symbol is being used to access the data? Perl has a number of hidden system variables that automatically get created by the program. In the case of

while(<>){

}

Perl actually creates

while($_ =<>){

$_ is a system variable.

= is an assignment operator

The data is stored serially in a location assessable only through $_. Perl automatically takes care to store the stream of data for you. In this sense, since a string is not a fundamental datatype, but an array of chars, Perl creates the appearance of static randomly stored data which is actually stored as a stream. Otherwise it would not be able to print your string legibly.

print;

Perl automatically prints to STDOUT the stream of chars stored in $_.

To end this program, use the keys <CTL D>.

Try this program:

#!/usr/bin/perl
while($temp=<>){
print $temp;
}


ruben@ruben:/home/ruben/perl_course > chmod 744 file4
ruben@ruben:/home/ruben/perl_course > file4
This is my test
This is my test
Helllooooo
Helllooooo
Bye
Bye

In this instance, we are overtly creating a variable called $temp and assign it the value from STDIN. Functionally, this is nearly the same program as the previous one.

Basic Programming