Notes: Perl Lab 03

Outline

  1. Program Arguments
  2. Conditional Statements
  3. Comparison Operators
  4. File Test Operators
  5. Regular Expression Operators
  6. Boolean Operators

  1. Perl Script Arguments

    Perl scripts can take arguments just as any other Unix commanline program would. The arguments added are stored in the built-in array names @ARGV. These may be accessed within the perl script as any other array would be. Example below:

     
    #!/usr/bin/perl -w
    
    # Name: Mark Tucker
    # Assignment: Lab03 Example 00
    # Description: An example of arguments in perl
    #==========================================================================
    
    # remove any newline characters from the last argument
    chomp($ARGV[$#ARGV]);
    
    print "The first argument has a value of \"$ARGV[0]\"\n";
    
    # Display all program arguments
    foreach $argument (@ARGV) {
        $counter++;
        print "Argument $counter is $argument\n";
    }
    
    
    exit;
    # DONE
    
    

    When executed, the script above produces the following output:
     
    tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03> ./lab03_0.pl "Hello World"
    The first argument has a value of "Hello World"
    Argument 1 is Hello World
    tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03> 
    

  2. Conditional Statements

    To control the execution of section of code, the if statement can be used. The if statement evaluates a condition and then executes the "block" if the condition is "true". A condition is true if it returns a value that is not 0 (zero), "" (null string). Undefined variables will be evaluated as "false" as well. A block is a grouping of statements between braces. An if stateent can be used to control the execution of a single statement without a block but the block syntax is more commonly used. A simple example:

     
    #!/usr/bin/perl -w
    
    # Name: Mark Tucker
    # Assignment: Lab03 Example 01
    # Description:  if/then/else string comparison examples
    #==========================================================================
    
    # set variables with string values
    $one = "chicken";
    $two = "egg";
    
    # single statement test
    print "Two is \"egg\"\n" if($two eq 'egg');
    
    # test string variables
    if($one eq $two ) {
        print "They are equal.\n";
    }
    
    exit;
    # DONE
    
    

    When executed, the script above produces the following output:
     
    tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03> ./lab03_1.pl 
    Two is "egg"
    tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03> 
    

    Additional conditions can be tested and executed with the elsif and else options:
     
    #!/usr/bin/perl -w
    
    # Name: Mark Tucker
    # Assignment: Lab03 Example 02
    # Description:  if/then/else numeric comparison example
    #==========================================================================
    
    # set variables with integer values
    $first = 3;
    chomp($second = $ARGV[0]);  # assume that we'll pas a number as an argument.
    
    # test variables as numeric
    if( $first == $second ) {
        print "They are equal.\n";
    }elsif( $second >= 4 ) {
        print "Second is 4 or more.\n";
    }else{
        print "All are false.\n";
    }
    
    exit;
    # done
    
    

    When executed, the script above produces the following output:
     
    tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03>
    tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03> ./lab03_2.pl 4
    Second is 4 or more.
    tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03> ./lab03_2.pl 2
    All are false.
    tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03> ./lab03_2.pl 3
    They are equal.
    tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03>
    

  3. Comparison Operators

    Below is a table of comparison operators that may be used in perl.

    Comparison Numeric Operator String Operator
    equal == eq
    not equal != ne
    less than < lt
    greater than > gt
    less than or equal <= le

  4. File Test Operators

    Perl allows you to test whether a file or directory exists or has specific attributes (such as a specific permission attribute). Below are a few of the more commonly used tests:

    Test Operator Description
    -eReturns "true" if the file exists.
    -rReturns "true" if the file is readable.
    -wReturns "true" if the file is writeable.
    -xReturns "true" if the file is executable.
    -dReturns "true" if the file is a directory.
    -fReturns "true" if the file is a regular file.
    There are several other tests which can be used, see man perlfunc and look under the "-X" section for details.

     
    #!/usr/bin/perl -w
    
    # Name: Mark Tucker
    # Assignment: Lab03 Example 03
    # Description:  file test operators
    #==========================================================================
    
    # set variables with integer values
    $first_file = '/etc/fstab';
    $second_file = '/usr/share';
    
    # test variables as numeric
    if( -d $second_file ) {
        print "$second_file is a directory\n";
    }
    
    if( -r $first_file ) {
        print "$first_file can be read\n";
    }else{
        print "Cannot read file $first_file.\n";
    }
    
    exit;
    # done
    
    

    When executed, the script above produces the following output:
     
    tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03>
    tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03> ./lab03_3.pl
    /usr/share is a directory
    /etc/fstab can be read
    tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03>
    

  5. Regular Expression Operators

    Perl has the built in ability to use "regurlar expressions" as a method of matching strings. Regular expressions are used by many Unix utilities and text editors. In perl, a regular expression comparison is specified by the =~ operator. The expression itself will be placed between slashes "//". A simple example follows:

     
    #!/usr/bin/perl -w
    
    # Name: Mark Tucker
    # Assignment: Lab03 Example 04
    # Description:  simple regular expression operators
    #==========================================================================
    
    # Get user input.
    print "Enter your name:\n";
    chomp($input = <STDIN>);
    
    # Check to see if the name contains a certain substring anywhere in the string
    if( $input =~ /ar/ ) {
        print "Your name comtains the substring \"ar\"\n";
    }
    
    exit;
    # done
    
    

    When executed, the script above produces the following output:
     
    tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03>
    tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03> ./lab03_4.pl
    Enter your name:
    Mark
    Your name comtains the substring "ar"
    tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03> ./lab03_4.pl
    Enter your name:
    Tucker
    tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03>
    

    Regular expressions can be negated with the !~ operator.
     
    #!/usr/bin/perl -w
    
    # Name: Mark Tucker
    # Assignment: Lab03 Example 04
    # Description:  simple regular expression operators
    #==========================================================================
    
    # Get user input.
    print "Enter your name:\n";
    chomp($input = <STDIN>);
    
    # Check to see if the name contains a certain substring anywhere in the string
    if( $input !~ /X/ ) {
        print "Your name does not comtain the letter \"X\"\n";
    }
    
    exit;
    # done
    
    

    When executed, the script above produces the following output:
     
    tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03>
    tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03> ./lab03_5.pl
    Enter your name:
    Mark
    Your name does not comtain the letter "X"
    tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03>
    

    Regular expressions allow you to specify whether a string begins or ends with a certain patter. The ^ symbol is used to specify if a string begins with the pattern and the $ symbol is used to specify the ending pattern. Example:
     
    #!/usr/bin/perl -w
    
    # Name: Mark Tucker
    # Assignment: Lab03 Example 04
    # Description:  simple regular expression operators
    #==========================================================================
    
    # Get user input.
    print "Enter your name:\n";
    chomp($input = <STDIN>);
    
    # Check to see if the name ends with the letter e
    if( $input =~ /e$/ ) {
        print "Your name ends with the letter \"e\"\n";
    # Now check to see if the name starts with the letters Ma
    }elsif( $input =~ /^Ma/ ) {
        print "Your name begins with the letters \"Ma\"\n";
    }else{
        print "The strings do not match\n";
    }
    
    exit;
    # done
    
    

    When executed, the script above produces the following output:
     
    tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03> 
    tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03> ./lab03_6.pl
    Enter your name:
    Mark
    Your name begins with the letters "Ma"
    tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03> ./lab03_6.pl
    Enter your name:
    Elephant  
    The strings do not match
    tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03> ./lab03_6.pl
    Enter your name:
    Eeeee
    Your name ends with the letter "e"
    tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03> 
    

    There are several special escaped character values that can match specific string types:
    ExpressionMatches...
    /\w/Matches any alphanumeric and "_" characters
    /\d/Matchs any digit character [0-9] in the string
    /\D/Matches any non-digit character in the string
    An example of testing for the existance of a digit in a string:
     
    #!/usr/bin/perl -w
    
    # Name: Mark Tucker
    # Assignment: Lab03 Example 07
    # Description:   regular expression operators
    #==========================================================================
    
    # Get user input.
    print "Enter a word:\n";
    chomp($input = <STDIN>);
    
    # Check to see if the word contains a digit
    if( $input =~ /\d/ ) {
        print "The string \"$input\" contains a digit\n";
    }else{
        print "The string \"$input\" does not contains a digit\n";
    }
    
    exit;
    # done
    
    

    When executed, the script above produces the following output:
     
    tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03> 
    tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03> ./lab03_7.pl
    Enter a word:
    grep
    The string "grep" does not contains a digit
    tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03> ./lab03_7.pl
    Enter a word:
    aardvark 1275
    The string "aardvark 1275" contains a digit
    tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03>
    

    More detailed information and examples can be found in the Perl regular expressions man page, man perlre.

  6. Boolean Operators

    Conditional statments can make their tests based on multiple conditions using boolean operators. The operators are listed below:

    symbolic operator text operator Function
    && and Returns "true" if both conditions are true.
    || or Returns "true" if either of the conditions are true.
    ! not Returns "true" if the condition is false.

     
    #!/usr/bin/perl -w
    
    # Name: Mark Tucker
    # Assignment: Lab03 Example 08
    # Description:   boolean operators
    #==========================================================================
    
    # Get user input.
    print "Enter a word:\n";
    chomp($input = <STDIN>);
    
    # Check to see if the word ends with the letter e and starts with the 
    # letter A
    if(( $input =~ /e$/ ) && ( $input =~ /^A/)) {
        print "Yes! It matches\n";
    }
    
    # Get user input.
    print "Enter a number less than 10:\n";
    chomp($input = <STDIN>);
    
    if(( $input !~ /\d/) || ( $input >= 10)) {
        print "Invalid input\n";
    }
    
    exit;
    # done
    
    

    When executed, the script above produces the following output:
     
    tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03> 
    tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03> ./lab03_8.pl
    Enter a word:
    anteater
    Enter a number less than 10:
    4
    tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03> ./lab03_8.pl
    Enter a word:
    Ante    
    Yes! It matches
    Enter a number less than 10:
    awk
    Invalid input
    tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03>
    


last updated: 18 Mar 2012 13:16