Perl, a powerful script language

Perl is a very high-level programming language originally developed in the 1980s by Larry Wall. Perl is now being developed by a group of individuals known as the Perl5-porters under the watchful eye of Larry.

 

What is Perl?

Perl officially stands for “The Practical Extraction Report Language,” but Perl is really much more than a practical reporting language. It’s practically everything really likeable about the Shells, awk, sed, grep, and C combined. Programmers can enjoy the powerful pattern matching features in Perl.

 

Perl, a GNU product (i.e., it’s free), is an interpreted language. It is used primarily as a scripting language and runs on a number of platforms.

 

Although designed for the Unix, Perl is renowned for its portability and also runs on DOS, Windows, Macintosh, etc.

 

Who uses Perl?

System administrator, Web developer, database administrator, application developer in bioinformatics, etc.

 

Perl at the Command Line

% perl –e ‘print “Hello, world\n”;’

 

How to execute The Perl Script?

% cat firstPerl

#!/usr/local/bin/perl         # the first line of the script

print “Hello, world\n”;       # statement is separated by semicolon ;

 

% perl –c firstPerl           # -c is used for Syntax checking at a prompt

 

Another way to run a Perl script is:        # No compilation step!!

% perl firstPerl

OR

% firstPerl                   # After chmod +x firstPerl

 

Quotes in Perl

Quoting rules in Perl is similar to C-Shell. Perl uses single quote (all characters are treated as literals), double quote (similar to the single quote except variable substitution), backslash \, and backquote `` (for executing commands).

 

Special literals

__LIINE__   # represents the current line number

__FILE__    # represents the current filename

__END__     # represents the logical end of the script

 

#!/usr/local/bin/perl

print “Hello, world\n”;

print “We are on line number “, _LINE_, “.\n”;

print “The name of this file is “, _FILE_,”.\n”;      # the name of current file

_END_

And this part after _END_, will be ignored by Perl.   # ignored by Perl

 

The printf function

Printf(“%-15s%-20s\n”, “Jack”, “Sprat”); # right-justified

Printf “Hello, my name is %s!\n”, “Sam”;

Printf “The number in decimal is %d\n”, 100;

Printf “The formatted floating point number is %8.2f\n”, 14.3456;

 

Printing without quotes – the Here Document

The Perl here document is a line-oriented form of quoting, requiring the << operator followed by an initial terminating string and a semicolon. There can be no spaces after the <<.

 

$price = 100;

print <<EOF;            # start of here document, there are no quotes

The price is $price.    # variables are expaned

EOF                     # end of here document, NO surrounding spaces allowed

 

print <<’FINAL’;        # start of here document, enclosed in single quotes

The price is $price.    # the variable is not expanded

FINAL                   # end of here document

 

print << x 4;           # start of here document, prints the line 4 times

Hello, there!

                        # Blank line is necessary here!!

print <<`END`;          # start of here document, back quote will execute Unix

echo hi there           # commands

date

END                     # end of here document

 

Here documents are used extensively in CGI scripts for enclosing large chunks of HTML tags for printing.

 

Perl Variables

Like shell script, Perl variables don’t have to be declared before being used. Perl has three types of variables: scalar (preceded by $), list (or array, preceded by @), and associative array (or hashes preceded by %). For example, $name, @name, and %name are all different variables.

·           Variables are case sensitive.

·           Since reserved words and filehandles are not preceded by a special character, variable names will not conflict with reserved words or filehandles.

 

$salary = 50000;              # scalar variable

@months=(Mar, “Apr”, 5);      # Perl list can store different types of data

print “$salary\n”;

print “@months\n”;

print “$months[0], $months[1]\n”;   # array subscript starts with 0

 

print “The number of the last subscript of months is $#months

 

$sym=net;

print “${sym}work\n”;         # with curly braces, the value can be appended

 

$name = “Tommy”;

print “OK\n” if defined $name;  # to check the validity of a variable’s value

 

undef $name;            # this function undefines an already defined variable

 

@months=();             # assigned a null list (empty the list)

@digits=(0..10);        # range operator, will contain 0, 1, 2, ...,10

@letters=(‘A’..’Z’);

 

### Array slice ###

@names=(‘Tom’, ‘Dick’, ‘Harry’, ‘Pete’, ‘Smith’);

$count = @names;        # the number of elements 11 is assigned to $count

@people = @names;       # names list is copied to @people

@friends = @names[1,2,3];           # or @names[1..3] is also ok

($enemy[0], $enemy[2])=@names;      # the enemy array is created with values

print “@enemy\n”;       # new values list is (Tom, undefined, Dick)

 

@matrix=([1,2],[3,4],[5,6]);        # 3x2 multi-dimensional array

print “Row 0, Column 0 is $matrix[0][0].\n”

@record=(“Adams”, [2,1],            # one dimensional arrary with data 2 and 1

         “Edwards”, [1,0,3],

         “Howard”, [3,4,5,6]);

print “In the first row $record[0]\n”;    # Adams

print “In the third row $record[5][2]\n”; # 5

 

Associative Arrays (Hashes)

%states = (‘CA’ => ‘California’, ‘TX’ => ‘Texas’, ‘MT’ => ‘Montana’);   # hash

 

# the first string is called a key, and the second string is called the value

print “$states{‘CA’}, $states{‘MT’}\n”;

 

%days=(‘Mon’, ‘Monday’, ‘Tue’, ‘Tuesday’, ‘Wed’,);

$days{‘Wed’}=”Wednesday”;     # The value Wednesday is assigned to the key Wed

$days{5} = “Friday”;          # The value Friday is assigned with the key 5

 

# Array of Hashes

@band=({name=>”Tom Jones”, age=>30, city=>”New York”},

       {name=>”Michael Jack”, age=>40, city=>”LA”},);

print “The total number of members: “, $#band + 1, “\n”;

print “First member name is $band[0]{name} \n”;

 

Reading from STDIN

There are three filehandles STDIN, STDOUT, and STDERR.

 

print “What is your name? “;  # The string is sent to STDOUT by default

$name = <STDIN>;              # one line of input is read and assigned to $name

@all=<STDIN>;                 # data entered into array until Ctrl-d is pressed

$course{$course_num}=<STDIN>; # data stored into hash table with the key

$num=read(STDIN, $indata, 100); # read 100 bytes at a time

$answer=getc;                 # one character at a time

 

The chop and the chomp functions

The chop function removes the last character in a scalar variable and the last character of each word in an array. It is used primarily for removing the newline from the line of input.

 

The chomp function (introduced in Perl 5) is similar to chop except that it removes the last character only if that character is the newline.

 

print “what is your name? “;

$name = <STDIN>;

chop($name);                  # removes the last character and returns it

chomp($name=<STDIN>);         # removes only if that is the newline character

 

The join function

The join function joins the elements of an array into a single string and separates each element of the array with a given delimiter—opposite of split.

 

Format: join(delimiter, list)

 

$name=”John”;

$birthdate=”1/1/2000”;

$place=”LA”;

print join(“:”, $name, $birthdate, $place), “\n”;     # John:1/1/2000:LA

 

The split function

The split function splits up a string by some delimiter (whitespace by default) and returns an array.

 

$line=”a,b,c,d”;

@letter=split(‘,’, $line);

print “The characters in the line is @letter\n”;      # a b c d

 

The pop and push functions

The pop function pops off the last element of an array and returns it. The array size is decreased by one. The push function pushes values onto the end of an array, increasing the size of the array.

 

$boy=pop(@list);                     # returns tommy and the tommy is removed from the list

push(@list, bobby, tomb);        # bobby and tomb are added to the list @list

 

The shift, splice, split functions

The shift function shifts off and returns the first element of an array, decreasing the size of the array. Splice function removes and replaces elements in an array. The general format is:

 splice(array, offset, length, list). Split function splits up a string by some delimiter (whitespace by default) and returns an array. The general format is:

split(/delimiter/, expr).

 

@names=(“bob”, “dan”, “tom”);

$man=shift @names;      # returns “bob” and @names contains now (“dan”, “tom”)

unshift(@names, Liz, bean); # @names has now (“Liz”, “bean”, “dan”, “tom”), add to the front

 

@newnames=splice(@names,1,3,yellow,orange); # @newnames (“bean”, ”dan”, ”tom”)

print “the spliced array is @names \n”;   # @names has (“Liz”, “yello”, “orange”)

 

$line = “a b c d e”;

@letter=split(‘ ‘, $line); # @letter contains (a, b, c, d, e)

 

The sort and reverse functions

The sort function sorts and returns a sorted array. The reverse function reverses the elements in an array.

@string=(a, d, f, c, b);

@string_sorted=sort(@string);       # a b c d f

@string_reverse=reverse(@string);   # f d c b a

 

sub numeric {$a <=> $b;}            # numeric subroutine definition

@sorted_num=sort numeric 10, 5, 6, 0, 1; #

 

Associative Array Functions and foreach Loop

%weekdays=(‘1’=>’Mon’, ‘2’=>’Tue’, ‘3’=>’Wed’, ‘4’=>’Thu’, ‘5’=>’Fri’);

foreach $key (keys(%weekdays))

{print “$key “;}              # 1 2 3 4 5

 

foreach $value (values(%weekdays))

{print “$value “;}            # Mon Tue Wed Thu Fri

 

while (($key, $value) = each %weekdays)

{print “$key = $value \n“;}   # it prints each pair of key and value

 

delete $weekday{1};           # removes the element with the key 1, which is Mon

 

Special Associative Array

%ENV is a associative array that contains the environment variables handed to Perl from the parent Shell.

 

foreach $key (keys(%ENV)) {print "$key\n";}

print “your home directory is $ENV{‘HOME’}”;

 

The grep function

The grep function evaluates the expression for each element of the array.

Format: grep(expr, list)

 

@list=(tomatoes, tomorrow, potatoes, phantom, tommy);

$count=grep(/tom/i, @list);   # the number of times the expression was true

@items=grep(/tom/i, @list);   # the array consisting of those element (true)

                                                            # i means case-sensitive

Perl operators

Perl performs appropriate type conversion by testing the operands of mixed types. Operators are very similar to C language.

 

+, -, *, /, %(modulus), ++, --, ==(equal to), !=(not equal to), &&(logical and), ||(logical or), >, >=, +=, -=, <=> (signed return, e.g., -1, 0, 1 for the number comparison), ..(range operator), x (string repetition), ?:(ternary conditional), String comparison operators: eq(equal to), ne(not equal to), cmp(signed return), qt (greater than), ge(greater than or equal), lt(less than), le(less than or equal)

 

$price = ($age > 60) ? 0: 5.55; # if $age > 60 then 0 else 5.55 is assigned to $price

print “The numbers: “, 1..10;   # range operator ..

$z = “kid”;

print $z x 5, “\n”;           # print 5 “kid”

print $z . “nap”, “\n”;       # concatenate “kid” and “nap”

$num1 <=> $num2               # returns –1 or 0 or 1

 

Random number generation

srand time;                   # setting the seed value

print “Random number: “, int(rand 6);  # the random number range 0 ~ 5

$roll = int(rand 6) + 1;               # the random number range 1 ~ 6

 

Regular Expressions

The regular expression operators are used for matching patterns in searches and for replacements in substitution operations.

m// (m/pattern/ or /pattern/) operator is used for matching patterns and s/// (s/old/new/) operator is used for substitution one pattern for another. However, m is optional if the delimiter is the forward slash (default). m/good/ is equivalent to /good/

 

/abc/  Any string that matched the pattern ‘abc’ will be matched in a string or file.

?abc?  Only the first occurrence of the string is matched.

$_ = “xabcy”;

print “found it\n” if /abc/; # will print a message ‘found it’,

                             # $_ is the default space for pattern matching.

 

Modifiers: i(turn off case sensitivity), m(treat a string as multiple lines), g(match globally, e.g., find all occurrences and returns a list if an array context, and true or false if a scalar context), s(treat string as single line when newline is embedded), e(evaluate the replacement side as an expression).

 

$_ = “I lost my gloves in the clover, Love.”;

@list=/love/g;    # love love

@list=/love/gi;   # love love Love.

 

$ cat sample.dat

Steve Blenheim

Norma Cord

Jon DeLoach

 

$perl –ne ‘s/Norma/Jane/; print;’ sample.dat    # will replace Norma by Jane

Steve Blenheim

Jane Cord

Jon DeLoach

 

$perl –ne ‘print if s/Jon/Tom/;’ sample.dat

Tom DeLoach

 

$_=50;

s/$_/$&*2/e;      # A special variable $& will hold the string that was matched.

print “The new value is $_\n”;      # will print 100

 

Pattern Binding Operators

If you have a string that is not stored in the $_ variable and need to perform matches or substitutions on that string, then the pattern binding operators are used. They are also used with the tr function for string translations.

 

General Formats:

$var =~ /expr/          # true if $var contains pattern /expr/, returns 1 for true, null for false.

$var !~ /expr/           # true if $var does not contain pattern /expr/

$var =~ s/old/new/  # replace first occurrence of /old/ with /new/

$var =~ s/old/new/g  # replace all occurrences of /old/ with /new/

$var =~ tr/a-z/A-Z/   # translate all lower case letters to upper case.

$var =~ /$pattern/    # a variable can be used in the search string.

 

$ cat test.pl           # Perl code

while(<>) {

      ($name, $phone, $address) = split(/:/, $_);

      print $name if $phone =~ /400-/

}

$ perl test.pl customer.dat   # assume customer.dat contains customer data

 

# while loop is used to explicitly loop through the file named at the command line. It will get a line from the file and store it in the $_ variable. The line in $_ will be split by colon (:) and the value returned stored in the list ($name, $phone, $address). The pattern /400-/ is matched against the $phone variable. If the pattern is matched in $phone, the value of $name is printed.

 

Metacharacters

.                       matches any character except newline.

[a-z0-9]           matches any single character in set.

[^a-z0-9]         matches any single character not in set.

\d                     matches one digit.

\D is equivalent to [^0-9] matches a non-digit.

\w                    matches an alphanumeric character.

\W                   matches a non-alphanumeric character.

\s                      matches whitespace character, spaces, tabs, and newlines.

\S                     matches non-whitespace character.

^                      matches to beginning of line.

$                      matches to end of line.

\A                    matches the beginning of the string.

\Z                     matches the end of the string.

x?                     matches 0 or 1 x

x*                    matches 0 or more x’s

x+                    matches 1 or more x’s

x{m,n}             matches at least m x’s and no more than n x’s.

a|b|c                 matches a or b or c.

 

Examples

/^a..c/  # It searches at the beginning of the line for an ‘a’, followed by any three characters,

      # followed by a ‘c’. For example, it will match ‘abbbc’, ‘a123c’, ‘aAx3c’, etc.

print if /[A-Z][a-z]eve/;  #Find A-Z, followed by a-z, followed by ‘eve.

Print if /2\d\d/;          # find a ‘2’ followed by exactly two digits.

Print if /5+/;          # find one or more 5’s

Print if /5{1,3}/;      # find at least one 5 but not more than 3

Print if /10*/;         # find 1 followed by 0 or more 0’s

Print if /5{3}/;        # find exactly three consecutive 5’s

Print if /5{1,}/;       # find at least one or more consecutive 5’s

 

tr /a-z/A-Z/; # each lower case letters a-z will be replaced by upper case A-Z.

 

Control structures and compound statements (block)

Simple IF modifier: expr2 if expr1; # if expr1 is true, execute expr2

$x = 10;

print $x if $x > 5; # will print 10

 

A compound statement consists of a group of statements surrounded by curly braces. Unlike C, Perl requires if, else, while, etc. to have {} even  with one statement.

 

Conditional constructs

if (expr) {block}

 

if (expr)

    { block }

else

    { block }

 

if (expr1)

        { block 1}

elsif (expr2)

        { block 2}

...

else    { block n}

unless (expr) {block}

 

unless (expr) {block} else {block}

 

unless (expr1) {block} elsif (expr2) {block} ... else {block}

 

# example

$hour = 10;

if ($hour <= 10)

      {print “good morning\n”;}

elsif ($hour == 12)

      {print “Lunch time\n”;}

else {print “Good night\n”;}

 

LOOP construct

While modifier: expr2 while expr1; # repeatedly executes expr2

   # as long as expr1 is true.

$x = 1;

print $x++, “\n” while $x != 5;

 

General While Loop

while (expr) {block}

 

until (expr) {block}

 

for (expr1; expr2; expr3) {block}   # just like for loop in C

 

# example

for ($i=0; $i<10; $i++) {           # print 0 1 2 ... 9

      print “$i “;

}

 

foreach var (array) {block}         # just like foreach in C-shell

 

# example

foreach $pal (Tom, Dick, Harry, Pete) {   # A list of values

      print “Hi $pal!\n”;

}

 

foreach $hour (1..24) {             # the range operator is used here

      print “Time is $hour\n”;

}

 

@num=(1, 3, 5, 7);

foreach $odd (@num) {

      print “$odd\n”;

}

 

File processing

There are three default filehandles, STDIN, STDOUT, and STDERR. Perl also allows you to create your own filehandles for input and output operations on files, devices, pipes, or sockets.

 

Open for Reading

open (filehandle, filename);

open (filehandle);

 

# Example

open(myhandle, “myfile”);

 

$myhandle = “myfile”;

open(myhandle);

 

# Can’t open: No such file or directory

open(myhandle, “etc/password”) || die “Can’t open: $!\n”;  

 

while(<FILE>) {

      print “Hi \n” if /Lori/;

}

 

% cat scanfile                           # using null filehandle

while (<>) {               # <> allows the program to read a line from a file at a time

   print if /Normal/;      # each line will be stored in $_ and test it and print it

}

 

% scanfile empl.dat        # will return the matched line in this file

 

Closing the filehandle

close(filehandle);

 

Open for Writing

Open(filehandle, “>filename”);

 

# Example

open(myoutfile, “>temp”);         # output redirect from STDOUT to temp file

print myoutfile “Hello world\n”;

print myoutfile “Another Hello world\n”;

 

Open for Appending

Open(filehandle, “>> filehandle”);

 

# Example

open(APPFILE, “>>temp”);

 

Open for Pipes

open(OUTPIPE, “| wc –l”);

print OUTPIPE “apples\npears\npeaches\n”; # returns 3

close(OUTPIPE);

 

open(INPIPE, “date |”);

$today = <INPIPE>;      # $today will receive its input from the INPIPE

print $today;

close(INPIPE);

 

Passing Arguments—note that ARGV should be UPPERCASE

@ARGV       # the array

$ARGV       # the first argument $ARGV[0], second arg $ARGV[1], etc.

$0          # the name of the script itself

$#ARGV      # number of element in ARGV array (or the number of arguments)

 

ARGV        # the filehandle

 

File Testing

-r          # if readable, i.e., if (-r $filename)

-w          # if writable

-x          # if executable

-e          # if exists

-z          # if empty

-f          # if plain file

-d          # if directory

 

Interfacing with the System

Those migrating from Shell programming to Perl often expect that a Perl script is like a Shell script—just a sequence of Unix commands. However, Unix utilities are not accessed directly in Perl programs as they are in Shell scripts. Perl has a set of functions, in fact, that specifically interfaces with the operating system and is directly related to the Unix system calls so often found in C programs.

 

Directories and Files

# using some Perl functions

mkdir($argv[0], 0755);  # the format of mkdir: mkdir(filename, access_mode)

rmdir(“junk”) || dies “rmdir: $!\n”;      # rmdir(directory)

chdir(“/home/profs/tryu”);    # or chdir “/home/profs/tryu”;

 

print “The hour is “, `date`; # using backquote

 

# The Shell.pm module (Perl5) lets you use Unix commands

# in a script

use Shell qw(pwd ls date);    # Shell commands listed

print “Today is “, date();    # or date;

print “The present working directory is “, pwd;

$list=ls;

print $list;      # will list all files

 

# using the system function, system(“unix_command”);

system(“cal 1 1998”);

 

# using the Here Documents derived from the shell here document

print <<`END`;

echo hi there

date

END

 

Some special symbols

$!                     # will hold the value of the system error

$$                    # process-ID of the Perl program running this script

$=

$_                    # pattern space

 

Subroutine

subroutine definition

sub subroutine_name {block}

 

subroutine call

do subroutine_name;

&subroutine_name;

subroutine_name();

subroutine_name;

subroutine_name(para1, para2, ...)  # or &subroutine_name(para1, para2, ...)

 

Example

Sub bye

{

    print “bye $name\n”;    # $name is Ellie

    $name=Tom;              # variables used in subroutine are global by default

}

 

$name=”Ellie”;

print “Hello world\n”;

if (defined &bye) {           # defined function checks to see if bye defined

    &bye;                     # subroutine call

}

print “Out of the subroutine: Hello $name again.\n”; # $name is now Tom

 

Call-by-reference

 

In Perl, the default is call-by-reference when arrays or scalars are passed to a function or subroutine. The @_ is a special local array, (elements of the @_ array are $_[0], $_[1], etc.) that handles arguments passed to the subroutine. Perl doesn’t care if you don’t use all the parameters passed, or if you have an insufficient number of parameters. 

 

# example-1

$first = “Steve”;

$last = “Kim”;

&greeting($first, $last);                    # call the greeting function

print “After greeting: $first and $last\n”;  # Smith and Kim

 

sub greeting

{ print “Welcome to the club, $_[0] $_[1]\n”;

  $_[0] = “Smith”;

}

 

# example-2

sub passArray

{  print ‘the values in the @_ array are ‘, “@_\n”;   # 1 2 3 4 5

   print “the last value is “, pop(@_), “\n”;         # 5

   foreach $val (@_) {

      $val += 5;

      print “The value is $val”, “\n”;

   }

}

 

print “Give me 5 numbers: “;        # let’s assume 1 2 3 4 5

@num=split(‘ ‘,<STDIN>);

&passArray(@num);

print “After the function call\n”;

print “The new values are @num\n”;  # 6 7 8 9 5

 

Call-by-value

There are two types of functions that can be used for declaring local variables: local and my functions. Any variable declared by local function is said to be dynamically scoped (visible to any functions called from within this block) and any variable declared by my function is said to be lexically scoped (only visible within the subroutine).

 

# example

$friend = Louise;

$pal = Danny;

print “I start with global $friend and $pal\n”; # Louise and Danny

 

&guests;

print “After the guests call\n”;

print “friend is $friend and pal is $pal\n”; # Louise and Danny

 

sub guests

{  my $friend=Pat;

   local $pal=Chris;

   print “my friend is $friend and local pal is $pal\n”;

   &who_are_they;

}

 

sub who_are_they

{  print “Your friend is $friend\n”;      # Louise because of my function

   print “Your pal is $pal\n”;            # Chris because of local function

}

 

Return value

The value returned is the value of the last expression evaluated within the subroutine or the return function returns a specified value.

 

sub MAX

{  local($max) = pop(@_);

   foreach $val (@_) {

      $max = $val if $max < $val;

   }

   $max;    # or this can be return $max;

}

$biggest=&MAX(2,3,4,5,100,0);

print “The biggest value is $biggest\n”;

 

BEGIN and END subroutines

A BEGIN subroutine is executed immediately, e.g., before the rest of the program is even parsed (similar to constructor function in C++). If you have multiple BEGINs, they will be executed in the order they were defined. The END subroutine is executed when all is done; that is, when the program is exiting (similar to destructor function in C++).

 

print “hello there\n”;                          # this is the second

chdir(“/poop”) || die “Can’t cd: $!\n”;         # this is the third

BEGIN {print “Welcome to my program.\n”};       # this will be executed first

END {print “Bailing out somewhere near line “, _LINE_, “ Bye \n”};      # last

 

Packages and Modules

The bundling of data and functions into a separate namespace is termed encapsulation (i.e., class). The separate namespace is termed a package. Perl5 extends the notion of packages to that of modules. A module is a package that is usually defined in a library and is reusable. Modules are packages stored in a file where the basename of the file is given the package name appended with a .pm extension. The use function takes the module name as its argument and loads the module into your script.

 

$name=”Susan”;    # this variable is in package main

print “The name was $name in main\n”;

 

package friend;   # package declaration

sub welcome

{   print “The name in friend package is $name\n”;          # blank

    print “The name from main package is $main::name\n”;    # Susan

    $name=”john”;

}

 

package main;     # package declaration; back in main

&friend::welcome; # call subroutine

print “back in main package the name is $name\n”;           # Susan

print “the name in friend package is $friend::name\n”;      # john

 

package birthday; # another package declaration

$name=Mary;

print “The name in birthday package is $name\n”;      # Mary

print “Final names: $::name and $friend::name\n”;     # Susan and john

 

The Standard Perl Library

The Perl distribution comes with a number of standard Perl library functions and packages. The Perl library routines end in the .pl extension and the Perl modules end in the .pm. The special array @INC, contains the directory path to where the library subroutines are located.

 

The require function loads Perl files into the program during run time. The require function also checks to see if the library has already been included (similar to #include in C).

 

# example-1

require “ctime.pl”;     # include the Perl library

printf “The current working directory is %s\n”, $ENV{PWD};

$today=&ctime(time);    # call the subroutine

print “$today”;

 

# example-2

 

# in the avg.pl script file that is located in /home/tryu/mylib directory

% cat avg.pl

sub avg

{  my(@grades)=@_;

   my($num)=$#grades+1;

   foreach $grade (@grades) {

      $tot=$tot+$grade;

   }

   $tot/$num;

}

1;    # Make sure the file returns TRUE or require function will not succeed!!!

 

# this is a test program for example-2

unshift(@INC, “/home/titan0/profs/tryu/mylib”); # add directory for the avg.pl

require “avg.pl”;

print “Enter your scores\n”;

@scores=split(‘ ‘,<STDIN>);

printf “The average is %.1f\n”, $avgscore=&avg(@scores);

 

Modules and .pm Files

# English.pm provides aliases for built-in variables such as $_, $/.

use English;            # Use English words to replace special Perl variables

print “The pid is $PROCESS_ID\n”;

print “The real uid $REAL_USER_ID\n”;

print “This version of Perl is $PERL_VERSION\n”;

 

Reference in Perl

A reference is a variable that refers to another one. A symbolic reference is a variable that names another variable. A hard reference is a scalar variable that holds the address of another type of data (i.e., pointers in C).

$english=”Brooklyn”;         

$language=”English”;

print “Your native tongue is $language\n”;            # English

print “But you speak with a ${$language} accent.\n”;  # Brooklyn

 

$num=5;

$p=\$num;

print ‘The address assigned $p is ‘, $p, “\n”;

print “The value stored at that address is $$p \n”;

 

Anonymous variables

$arrayref = [‘Woody’, ‘Buzz’, ‘Bo’, ‘Potato head’];

print “$arrayref->[3]\n”;     # Potato head

print $$arrayref[3], “\n”;    # Potato head

 

Object-Oriented Perl5

 

The big addition to Perl5 is the ability to do object-oriented programming. This is not really a new idea for Perl since data is already encapsulated in packages. The notion of modules was introduced in Perl5. A module is really just a fancy package that is reusable.

 

Objects, Methods, and Classes

An object is simply a referenced thing that happens to know which class it belongs to. A class is simply a package that happens to provide methods to deal with objects. A method is simply a subroutine that expects an object reference (or a package name, for class methods) as its first argument.

A Perl class is just a package. The terms are interchangeable. But if you want to distinguish between the two terms, a class is a package containing special subroutines called methods that manipulate objects. An object in Perl is created by using a hard reference, then blessing it into a package.

 

package Employee;

$clerk={name=>”Tom”, salary=>2500};             # anonymous hash

bless($clerk, Employee);                        #

print “Clerk is an “, ref($clerk), “\n”;        # Employee

print “The clerk’s name is $clerk->{name}\n”;   # Tom

 

Methods

package Employee;       # class

 

sub new                 # a constructor

{ my $class=shift;      # $class has Employee

  $worker={name=>undef, salary=>undef};   # values will be assigned later

  bless($worker, $class);                 # $worker now references an object

  return $worker;       # the reference to the object is returned

}

 

package main;           # switch namespaces to main package

 

my $empref = new Employee; # call the new method, or $ref=Employee->new()

$empref->{name}=”Dan savage”; # use the object

$empref->{salary}=75000;

print “name is $empref->{name} \n”;

print “salary is $empref->{salary} \n”;