Using perl to write programs



Perl was developed for system administration on Unix operating systems by Larry Wall. Perl 1.0 was released to usenet's alt.comp.sources in 1987.

Perl scripts are interpreted (as opposed to being compiled). A script is a sequence of instructions that is carried out by another program rather than by the computer processor.


#!/usr/local/bin/perl

print "This is my first perl file!\n";

Comments:

            #! /usr/local/bin/perl

            Perl comments are introduced by the #.  The #! is a special comment that signals what interpreter to apply to the file. 

            print "This is the output from one!\n";

            Prints out some text demarcated by either single or double quotes.  Note the use of the backwards slash to turn the following character into a control character:

            \n                     newline

            \t                      tab

#!/usr/local/bin/perl

print "What is your name?\n";

$name = <STDIN>;

print "Hello $name";

Comments:

            $name

Variables begin with the dollar sign: $

Variables can hold numbers or strings.  Note that variable names are case sensitive.  Hence: $name is not the same variable as $Name and both of these differ from $NAME, etc.

            The single equals sign is the assignment operator: 

$odd_number = 3;                    assigns the number three to the variable $odd_number

$student = “Sally”;                assigns a string to a variable

            Equality or inequality is tested with different operators depending whether the information is numeric or string:

$odd_number == 4;                 false: this is a test of equality for numbers

$odd_number != 3;                  false: this is a test of inequality for numbers

$student eq “Larry Wall”        false: this is a test of equality for strings

$number_value ne “Sally”         false: this is a test of inequality for strings

            STDIN

            The standard input file handle; that is, input from the keyboard.  Evaluating a filehandle in angle brackets yields the next line from that file.

            STDOUT        The standard output filehandle.

 

            It is possible to use specific files for input and output. 

            Consider the following program that opens a file “in_stuff” and writes the contents to “out_stuff”.

#!/usr/local/bin/perl

open(IN, "in_stuff");

open(OUT, ">out_stuff");

while(<IN>) {

    print OUT $_;

}

close(IN);

close(OUT);

Note the use of the > to pipe the contents into the file “out_stuff”.

Note the use of $_ as a default variable to hold data.

#!/usr/local/bin/perl

print "Here are three colors: Red, White, Blue\n";

print "Guess which one is special\n";

$guess = <STDIN>;

chop($guess);

$guess =~ tr/A-Z/a-z/;

if ($guess eq "red") {

    print "You are right!\n";

} else {

    print "Sorry!\n";

}

chop($guess); The chop function removes the last character from a string. Here it removes the newline character from the end of the input line.

=~ A matching operator (i.e., an equals sign, tilde) is often used with other operators to either find a pattern or transform a pattern:

$guess =~ tr/A-Z/a-z/; Transforms all upper-case characters into lower-case ones.
$guess =~ /please/ Is true if the $guess variable contains the word "please".

if ... then ... else conditional statements The conditional statement is evaluated for truth.

	
	if ($guess eq "red") {    \\  The condition that is evaluated for truth
	    print "You are right!\n";   \\  Fires if condition is TRUE
		} else {
		print "You are wrong!\n";    \\ Fires if condition is FALSE
		}
	
	
#!/usr/local/bin/perl

@myList = ("Larry", "Moe", "Curley");

foreach $name ( @myList ){
    print $name;
    print "\n";
}


An array is a variable that holds a list. Indicate an array by using the @ before the name. @myList is an array that holds three strings: "Larry", "Moe" and "Curley".