Notes: Perl Lab 01

Outline

  1. Why use Perl?
  2. Why not use Perl?
  3. How Perl works.
  4. Perl Help
  5. Scalar Variables
  6. Resources

  1. Why use Perl?

    Perl: "Practical Extraction and Report Language"
    There are many good reasons to use Perl:
    • Excellent for manipulating data, particularly tabular data - great for pulling apart text bulletins, model data, etc.
    • Used by NWS offices.
    • Available on every modern version of Unix, Linux and BSD systems.
    • Free. Source code is available under the terms of the GPL or Artistic licenses.
    • Available on Windows, Mac, OS/2 and other dinosaur systems.
    • Very fast to write. Perl has many built in functions to simplify tasks.
    • Very flexible ("There's More Than One Way To Do It").
    • Extensible - There are modules available on CPAN that add new functions for nearly anything imaginable.
    • Mature - Perl has been under active development since 1987.
    • Fun!
  2. Why not use Perl?

    • It's a bit weird. Perl borrows features from other languages (C, Bourne Shell, Awk).
    • syntax is not rigid. It is easy to get sloppy and messy code abounds. Perl is sometimes referred to as a "Write-only" language because nobody else can understand what you wrote. Below are two examples of valid, but practically unreadable, Perl code:

       
      #!/usr/bin/perl
      @P=split//,".URRUU\c8R";@d=split//,"\nrekcah xinU / lreP rehtona tsuJ";sub p{
       @p{"r$p","u$p"}=(P,P);pipe"r$p","u$p";++$p;($q*=2)+=$f=!fork;map{$P=$P[$f^ord
       ($p{$_})&6];$p{$_}=/ ^$P/ix?$P:close$_}keys%p}p;p;p;p;p;map{$p{$_}=~/^[P.]/&&
       close$_}%p;wait until$?;map{/^r/&&<$_>}%p;$_=$d[$q];sleep rand(2)if/\S/;print
      

       
      #!/usr/bin/perl
      /;{}def/#{def}def/$_={/Times-Bold exch selectfont}#/_{rmoveto}#/"{dup}#/*/!/$ 
      ;/q{exch}#/x ; {/J q #}#/.{/T q #}#{stringwidth}#{}#{}# 14 string dup dup dup
      260 40 moveto 90 rotate ; %/}};$0='"\e[7m \e[0m"';@ARGV=split//,reverse
      q(ThePerl). q(Journal) x 220 ; q ; 0 T putinterval exch 7 J putinterval ;
       ; $_= q /m$ pop T($*!$"=!$ " )pop " * true% ? $ " $!" "  !!  !! % !" !"    !
      ! charpath {!"""}pop $ pop{""!}pop ! neg{!#}pop 220 ! neg _{!!}pop J false %T
      charpath  clip " pop 0 " moveto 6{!!}pop $_= 105{!!}pop {$ ! $ " !  #! ##}
      pop{dup dup $ ! " pop pop q{"}pop 22{dup show}repeat {"}pop q 22 mul{$ "} pop 
      neg{!#! $ "}pop ! 8 .65 mul{$ # # $}pop ! neg{"}pop  _ pop{"}pop } repeat pop 
      " {  $ " ! ! ! $ " ! !" "#"  #"!"""""! #" " # "m/;@ARGV=(@ARGV[-14..-1])x50;q}
       0 "%};s/m[ou]|[-\dA-ln-z.\n_{}]|\$_=//gx;s/(.)(?{$*=''})/('$*.='.(++$# 
      %2?'':"$0;").'pop;')x(ord($1)-31).'$*'/gee;s/((.(\e\[.m)*|.){77})/$1\n/g;print
      ; sub showpage {}
      

    • Slightly slow startup time for large apps - not much of a problem on current systems.
    • Good for text manipulation and "glue" but may not be the most well suited for gui apps or low level apps.
    • Objects are an after-thought. Very inconsistent behavior.
  3. How Perl Works

    With Perl, the source code is the program. Technically, Perl is an interpreted language - which means that the source code is read in at execution time. It is actually compiled at run time so there is no need for the compilation and linking steps in building an executable as with other languages. This compilation adds some delay in the startup of a perl program but allows it to run more efficiently once it is fully loaded.

    An example of a basic perl script:
     
    #!/usr/bin/perl
    
    # Name: Mark Tucker
    
    # Assignment: Lab01 Example #1
    
    # Description: Basic "Hello World" program example.
    
    #==========================================================================
    
    # code goes here
    print "Hello World!\n";  # another comment
    
    
    exit;  # not reuquired but a good idea.
    # DONE
    

    Some notes on the structure of a perl program:

    • Perl programs are just a plain text file.
    • The first line, first column, must start with the string #!/usr/bin/perl. The initial character pair #! tells the Unix shell that that this script will be using it's own interpreter which is the executable program that follows immediately, in this case /usr/bin/perl. The actual location of the perl executable may vary depending on the version of Unix being used. On Windows systems, this line is not necessary as Windows uses the file name extension to determine how to run the executable. It is still a good idea to include the line in case the program is moved over to another type of system. Under Unix based systems the file may be named any valid Unix file name but it is helpful to name perl scripts with the .pl extension so that they can easily be identified.
    • Comments start with the # character. They may be on a line by themselves or follow a perl statement. Any text after the # symbol is ignored by the perl interpreter.
    • All perl statements must end with the semi-colon ";" character. New lines do not end a statement. Perl ignores "white space" in the code such as spaces, tabs and new-line characaters except when found within single quoted strings.
    Running the script:

     
    tuckerm@apollo:~>
    tuckerm@apollo:~> perl zero.pl
    Hello World!
    tuckerm@apollo:~>
    
    In this example, the perl interpreter is executed with the script name as an argument. perl then reads the contents of the file, compiles the script in system memory, and executes the code.

     
    tuckerm@apollo:~>
    tuckerm@apollo:~> chmod a+x zero.pl
    tuckerm@apollo:~>
    tuckerm@apollo:~> ./zero.pl
    Hello World!
    tuckerm@apollo:~>
    
    In the example above, the script file itself is made executable with the Unix chmod command. It is then executed. The Unix shell reads the first line "#!/usr/bin/perl" and launches the perl interpreter to read the remainder of the file. As in the previous example, perl reads the contents of the file, compiles the script in system memory, and executes the code. For more information about the Unix environment, check out the Unix course pages.

  4. Variables

    Perl has two basic variables types: scalars and lists. Scalars are single elements that hold a value, such as a text string or number. List variables consist of arrays or hashes (know as associative arrays). More on list variables in future labs.

    Scalar variables do not need to be "typed" as in C or other languages. Nor do they need to be declared before using them (although there are contexts in which it is very prudent to do so). Below are some examples of the behavior of variable assignment, subsitition and print statements.

    Scalar variables in perl will always begin with the $ character. Array variables in list context will begin with the @ character and hashes in list context will begin with the % character. Variable names should not contain any non-alphanumeric characters other than Perl's "built-in" variables. Variables names should also not have a numeric character at the beginning of the name. Perl variable names are case-sensitive; ie. $fish is a different variable than $Fish.

    one.pl
     
    #!/usr/bin/perl
    
    # Name: Mark Tucker
    
    # Assignment: Lab01 Example #2
    
    # Description: Basic "Hello World" program  example modified to show variable 
    #              substitution and print behavior.
    
    #==========================================================================
    
    # code goes here
    $string = "Hello World!\n";  # another comment
    
    print STDOUT $string;
    
    # notice the escaping of the quotes.
    print 
        "string is \"$string\"";
    
    # notices the single quotes and their behavior.
    print 'string is $string';
    
    # assign a new value 
    $string = 4;
    
    print $string;
    
    print "XX\n";
    
    exit;  # not reuquired but a good idea.
    # DONE
    
    

    two.pl
     
    #!/usr/bin/perl
    
    # Name: Mark Tucker
    
    # Assignment: Lab01 Example #3
    
    # Description: Simple program to show the capture of STDIN
    #
    
    #==========================================================================
    
    # code goes here
    print "Please enter your name\n";
    
    # get input from the user 
    $name = <STDIN>;
    
    # strip any newline and carriage return characters with chomp
    chomp($name);
    
    # print variable back to stdout
    print STDOUT "Your name is $name\n";
    
    
    exit;
    # DONE
    
    

  5. Perl Help

    Perl has a wealth of quality documentation included in it's standard distribution. In a Unix environment, there are many manual (man) pages for various aspects of perl. The method for accessing the documentation may vary depending on the system but the contents and structure will be the same.

    man perl - Perl overview. This is the main man page for Perl. This document lists all the other man pages for perl available on the system.
    man perlfunc - Perl built-in functions
    man perlre - Perl regular expressions
    man perlfaq - Perl frequently asked questions
    Many, many others...

  6. Perl Resources

    Web links:
    Comprehensive Perl Archive Network
    ActiveState (Perl for Windows 9x,NT,2k,XP) Free
    The Perl Directory
    O'Reilly Perl.com site

    Tutorials:
    Robert's Perl Tutorial

    Text Editors:
    GNU/Emacs (Windows, Mac, Unix)
    XEmacs (Unix).
    VIM (windows, Mac, Unix)
    Elvis (windows, Unix)
    Kate (KDE environment, Unix)
    Open Perl IDE (win32)
    FTE (Unix, win32)
    NEdit (Unix)

last updated: 27 Aug 2015 12:42