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?

    There are many good reasons to use Perl:
  2. Why not use Perl?

  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:

    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.

  4. 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...

  5. 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
    
    

  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: 02 Sep 2008 18:28