CSC 272 - Software II: Principles of Programming Languages

Dr. R. M. Siegfried

The Perl Programming Language

Origins of Perl

Perl's Influences

Running Perl Programs

myfirst.pl

    # A first program in Perl
    # Comments run from the # until the
    # end of the line
    print "Welcome to Perl!\n";

Output for myfirst.pl

    Welcome to Perl!

Input

mysecond.pl

    # My second program in Perl
    print "Enter a number\t?";

    # Read a number from the keyboard
    # which is standard input
    $value1 = <STDIN>;
    chomp $value1;

    print "Enter a number\t?";
    $value2 = <STDIN>;
    chomp $value2;

    $sum = $value1 + $value2;
    print "The sum of $value1 and $value2 is $sum\n";

Running mysecond.pl

    perl mysecond.pl
    Enter a number	?34
    Enter a number	?45
    The sum of 34 and 45 is 79

mythird.pl

    # Using if - elsif - else in Perl

    print "Please enter a number\t?";
    $mynumber = <STDIN>;
    chomp $mynumber;

    # You need braces even for just one statement
    if ($mynumber > 0)      {
            print "$mynumber is positive.\n";
    }
    # elsif - NOT else if
    elsif ($mynumber < 0)   {
            print "$mynumber is negative.\n";
    }
    else    {
            print"mynumber is zero.\n";
    }

myfourth.pl

    print "Enter a number\t?";
    $number = <STDIN>;
    chomp($number);

    # A conditional loop using while
    while ($number != 0)	{
    	if ($number > 0)        {
    		print "Positive\n";
    	}
    	elsif ($number < 0)     {
    		print "Negative\n";
    	}
    	else    {
    		print "Zero\n";
    	}
	print "Enter a number \t?";
	$number = <STDIN>;
	chomp($number);
    }

myfifth.pl

    print "Enter a number\t?";
    $number = <STDIN>;
    chomp($number);

    # An until loop - executes UNTIL the condition
    # is no longer true
    until ($number == 0)    {
    	if ($number > 0)        {
    		print "Positive\n";
    	}
    	elsif ($number < 0)     {
   		print "Negative\n";
    	}
	else    {
		print "Zero\n";
	}

	print "Enter a number \t?";
	$number = <STDIN>;
	chomp($number);
    }

do-while loops

    # A do-while loop, where the condition is testedr
    # at the end
    do      {
            print "Enter a number\t?";
            $number = <STDIN>;
            chomp($number);

            if ($number > 0)        {
                    print "Positive\n";
            }
            elsif ($number < 0)     {
                    print "Negative\n";
            }
            else    {
                    print "Zero\n";
            }
    } while ($number != 0);

A Basic Subroutine

    # Subroutines really DO work like this!?

    print "Before test\n";
    test("Robert", "Michael", "Jason", "Philip", "Katherine");
    print "After test\n";

    sub test	{
    	for ($i = 0; $i < @_;  $i++)    {
    		print "Argument #$i\t$_[$i]\n";
    	}
    }

infile.pl

    use strict;  # Forces you to use Perl in a safe way
    use warnings;	# cause Perl to just give a
    				# warning

    open (IN, "infile.txt")
            or die("Can\'t open infile for input\n");

    while (my $line = <IN>) {
            chomp($line);
            print("File: $line\n");
    }

    close <IN> or die ("Can\'t close file: $!");

copyfile.pl

    use strict;
    use warnings;

    open (IN, "infile.txt")
            or die("Can\'t open infile for input\n");

    open (OUT, ">outfile.txt")
            or die("Can\'t open outfile for output\n");

    while (my $line = <IN>) {
            chomp($line);
            print(OUT "File: $line\n");
    }

    close <IN> or die ("Can\'t close file: $!");

arraytest.pl

    # Let's try reading in an array

    $n = 0;
    print "Enter a non-zero value\t?";
    $a[n] = <STDIN>;
    chomp $a[$n++];
    while ($a[$n-1] != 0)   {
            print "Enter a non-zero value\t?";
            $a[$n] = <STDIN>;
            chomp $a[$n++];
    }

    #Print them
    for ($i = 0;  $i < $n;  $i++)   {
            print "$a[$i]\n";
    }

NumOver.pl

    # Initialize the array
    @array = ( 11, 22, 73, 40, 56 );

    # Calculate and print the mean
    $mean = calcmean(@array);
    print "The mean is $mean\n";

    # Calculate and print the number of 
    # values above the mean
    $more = overavg(@array);
    print "There are $more values that are greater ",
     "than the mean\n";

    sub overavg()
    {
          $mean = calcmean(@array);
          $numover = 0;

	  # @_ is the formal parameter array and the
     	  # number of elements in the array
          for ($i = 0;  $i < @_;  $i++)   {
		#  $_[0] is the first member of 
		#  the array
		if ($_[$i] > $mean)     {
			$numover = $numover + 1;
		}
          }
          return $numover;
    }

    sub calcmean()
    {
        $sum = 0;
        $n = 0;
        for ($i = 0;  $i < @_;  $i++)   {
                $sum = $sum + $_[$i];
                $n = $n + 1;
        }
        print $_;
        return $sum / @_;
    }

Output

panther.adelphi.edu> perl NumOver.pl
The mean is 40.4
There are 2 values that are greater than the mean

[Back to the Assignment Index]