Exercise 1 : Executing sample Perl scripts

Laboratory Exercise for Introduction to Perl

This assumes you have already set up your initial directory with the init-perllab command (instructions).

  1. Change to the Lab1 directory:

    cd /scratch/mylogin/PerlLab/Lab1

    The directory path may be different if you installed the files in another location.
  2. List the directory contents:

    ls -l

    You should see the files: INSTRUCTIONS README badconst.pl goodconst.pl hello.pl trig.pl
  3. Look at the hello.pl file:

    cat hello.pl

    The contents of this file are as follows (note the line numbers have been added for clarity and are not in the actual file):
    1. #!/usr/bin/perl
    2.  
    3. print "Hello, world!\n";
  4. Invoke perl to run the program:

    perl hello.pl

    The output should be:
    output
    Hello, world!
    
  5. Execute the program directly:

    ./hello.pl

    The output should be the same.
  6. Now, look at the trig.pl file:

    cat trig.pl

    The contents of this file are as follows (note the line numbers have been added for clarity and are not in the actual file):
    1. #!/usr/bin/perl
    2.  
    3. use warnings;
    4. use strict;
    5.  
    6. use constant RAD => (atan2(1,0) / 90.0);
    7.  
    8. my $angle;
    9. my ($s, $c);
    10.  
    11. print "ANGLE SIN() COS()\n";
    12. $angle = 0;
    13. while ($angle <= 360)
    14. {
    15. $s = sin($angle * RAD);
    16. $c = cos($angle * RAD);
    17. printf(" %3d %6.3f %6.3f\n", $angle, $s, $c);
    18. $angle += 45;
    19. }
  7. Invoke perl to run the program:

    perl trig.pl

    The output should be:
    output
    ANGLE    SIN()   COS()
       0    0.000    1.000
      45    0.707    0.707
      90    1.000    0.000
     135    0.707   -0.707
     180    0.000   -1.000
     225   -0.707   -0.707
     270   -1.000   -0.000
     315   -0.707    0.707
     360   -0.000    1.000
    
  8. Now, try to execute the program directly:

    ./trig.pl

    You should see an error message, "bash: ./trig.pl: Permission denied". This is because the permissions are not set to allow execution.

    ls -l trig.pl

    Notice that the permissions on the file trig.pl are "-rw-r--r--", which means you can read and write the file, but not execute. To run it, you'll have to run the actual Perl program and tell it where to find the file, or change the permissions.
  9. Change the permissions of the file to add execute privilege:

    chmod +x trig.pl

    This means "change mode to add execute (x) permission to trig.pl". Check by listing:

    ls -l trig.pl

    Now you sould see the permission as "-rwxr-xr-x".
  10. Try again to execute the program directly:

    ./trig.pl

    This time, the program should run.

Understanding the hello.pl program

  1. #!/usr/bin/perl

This directive instructs the shell to use the program /usr/bin/perl to run hello.pl . Without that line, the shell would interpret the Perl program as bash or ksh commands, which will produce many errors. You can experiment by editing the file and removing this line to see what happens.

  1. print "Hello, world!\n";

The print function sends the specified string to the standard output. In this case, the string is surrounded by double quotes, so the backslash n ("\n") is replaced with a newline.

Understanding the trig.pl program

  1. #!/usr/bin/perl

This directive instructs the shell to use the program /usr/bin/perl to run trig.pl .

  1. use warnings;
  2. use strict;

Calls to the use function usually import symbols from a given module, like using #include in C or C++. However, in this case, they act as pragmas. The warnings pragma instructs the compiler to turn on optional warnings, which are particularly helpful to avoid common programming errors. The strict pragma instructs the compiler to require that variables must be declared, among other checks. This helps to detect misspelled variables and subroutine names, for instance.

  1. use constant RAD => (atan2(1,0) / 90.0);

The constant pragma is a means to create constant symbols, much like the const declaration in C and C++. A Perl constant is simply a word, without a dollar sign or any other punctuation before it (as is the case with variables). To compute an accurate number, the atan2 function is used. [ caveat for constant pragma ]

  1. my $angle;
  2. my ($s, $c);

Calls to the my function declares the listed variables within the current scope (which is everything in the file after these lines). The variables $s and $c are declared on the same line, so they must be grouped with parentheses.

  1. print "ANGLE SIN() COS()\n";

The print function sends the specified string to the standard output. In this case, the string is surrounded by double quotes, so the backslash n ("\n") is replaced with a newline.

  1. $angle = 0;
  2. while ($angle <= 360)
  3. {
  4. $s = sin($angle * RAD);
  5. $c = cos($angle * RAD);
  6. printf(" %3d %6.3f %6.3f\n", $angle, $s, $c);
  7. $angle += 45;
  8. }

This is a basic while loop. The highlighted lines show the initialization, test, and increment. Note that the angle is represented as an integer, so there is no risk of roundoff errors which might complicate the given test if we were using floating point values.

Inside the loop, the sin and cos functions are computed on the angle, converted from degrees to radians by the constant RAD. Finally, the printf function prints the three values in a column-oriented format.