Perl Perl = Practical Extraction and Report Language Developed by Larry Wall (late 80’s) as a replace- ment for awk. Has grown to become a replacement for awk, sed, grep, other filters, shell scripts, C programs, ... (i.e. ”kitchen sink”). An extremely useful tool to know because it: • runs on Unix variants (Linux/Android/OSX/IOS/.. Windows/DOS variants, Plan 9, OS2, OS390, VMS.. • very widely used for complex scripting tasks • has standard libraries for many applications (Web/CGI, DB, ...) Perl has been influential: PHP, Python, Ruby, ... even Java (interpreted) perl-1
68
Embed
Perl = Practical Extraction and Report Language Developed by
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Perl
Perl = Practical Extraction and Report Language
Developed by Larry Wall (late 80’s) as a replace-
ment for awk.
Has grown to become a replacement for awk, sed,
grep, other filters, shell scripts, C programs, ...
(i.e. ”kitchen sink”).
An extremely useful tool to know because it:
• runs on Unix variants (Linux/Android/OSX/IOS/..),
Windows/DOS variants, Plan 9, OS2, OS390,
VMS..
• very widely used for complex scripting tasks
• has standard libraries for many applications
(Web/CGI, DB, ...)
Perl has been influential: PHP, Python, Ruby, ...
even Java (interpreted)
perl-1
Perl (cont.)
Some of the language design principles for Perl:
• make it easy/concise to express common id-
ioms
• provide many different ways to express the
same thing
• use sensible defaults to minimise declarations
• don’t be afraid to use context as a syntactic
tool
• create a large language that users will learn
subsets of
Many of these conflict with design principles of languages
for teaching.
perl-2
Perl (cont.)
So what is the end product like?
• a language which makes it easy to build use-
ful systems
• readability can sometimes be a problem (lan-
guage is too rich?)
• interpreted
⇒
efficient? (although still remarkably efficient)
Summary: it’s easy to write concise, powerful,
obscure programs in Perl
perl-3
Reference Material
Getting started ...
• A Little Book on Perl
by Robert W. Sebesta
Prentice Hall, 2000, ISBN:0139279555
Getting serious ...
• Perl Cookbook
by Tom Christiansen, Nathan Torkington, Larry
Wall
O’Reilly@ Associates, 1998, ISBN:1565922433
• Mastering Algorithms With Perl
by Jon Orwant, Jarkko Hietaniemi, John Mac-
Donald, John Orwant
O’Reilly@ Associates, 1999, ISBN: 1565923987
• www.perl.com
by various authors, on-line repository of Perl
information
perl-4
Using Perl
Perl programs can be invoked in several ways ...
• giving the filename of the Perl program as acommand line argument
perl PerlCodeFile.pl
• giving the Perl program itself as a commandline argument
• using the #! notation and making the pro-gram file executable
chmod 755 PerlCodeFile./PerlCodeFile
perl-5
Using Perl (cont.)
Advisable to always use -w option.
Causes Perl to print warnings about common er-rors.
perl -w PerlCodeFile.pl
perl -w -e ’PerlCode’
Can use options with #!
#!/usr/bin/perl -w
PerlCode
you can also get warnings via a pragma:
use warnings;
To catch other possible problems
use strict;
Some programmers always use strict, others find
it too annoying.
perl-6
Syntax Conventions
Perl uses non-alphabetic characters to introduce
various kinds of program entities (i.e. set a context
in which to interpret identifiers).
Char Kind Example Description# Comment # comment rest of line is a comment$ Scalar $count variable containing simple@ Array @counts list of values, indexed% Hash %marks set of values, indexed@ Subroutine @doIt a callable piece of Perl
perl-7
Syntax Conventions (cont.)
Any unadorned identifiers are either
• names of built in (or other) functions (e.g.
chomp, split, ...)
• control-structures (e.g. if, for, foreach,
...)
• literal strings (like the shell!)
The latter can be confusing to C/Java/PHP pro-grammers e.g.
$x = abc; is the same as $x = "abc";
perl-8
Variables
Perl supports three kinds of variable:
• scalars ... a single atomic value (number or
string)
• arrays ... a list of values, indexed by number
• hashes ... a group of values, indexed by
string
Variables do not need to be declared or initialised.
If used when not initialised, the value is 0 or
empty string or empty list.
Beware: spelling mistakes in variable names, e.g.
print "abc=$acb
n"; rather than print "abc=$abc
n";
prints no value, leaving you wondering what hap-
pened to $abc’s value.
perl-9
Variables (cont.)
Many scalar operations have the notion of a ”de-
fault source/target”.
If you don’t specify an argument, they assume
one called $ .
This makes it
• often very convenient to write brief programs
(minimal syntax)
• sometimes confusing to new users (”Where’s
the argument??”)
$ performs a similar role to ”it” in English text.
E.g. “The dog ran away. It ate a bone. It had lots of
fun.”
perl-10
Scalars
One scalar data type: string.
In expressions/assignments, values of scalar vari-
Compound assignments for the most commonoperators allow you to write
$var op= expression
Examples:
$x += 1; # increment the value of $x$y *= 2; # double the value of $y$a .= "abc" # append "abc" to $a
perl-14
Logical Operators
Perl has two sets of logical operators, one like
C, the other like ”English”.
The second set has very low precedence, so can
be used between statements.
Operation Example MeaningAnd x @@ y false if x is false, otherwiseOr x || y true if x is true, otherwiseNot ! x true if x is not true, falseAnd x and y false if x is false, otherwiseOr x or y true if x is true, otherwiseNot not x true if x is not true, false
perl-15
Logical Operators (cont.)
Example of using statement-level logical opera-tions:
if (!open(FILE,"myFile")) {die "Can’t open myFile";
}# or
if (!open(FILE,"myFile")){ die "Can’t open myFile"; }
# can be replaced by Perl idiom
open(FILE,"myFile") or die "Can’t open myFile";
perl-16
Arrays (Lists)
An array is a sequence of scalars, indexed by
position (0,1,2,...)
The whole array is denoted by @array
Individual array elements are denoted by $array[index]
last OUTER if $i > 90; # terminates both loops$i += 3;next if $i > 80; # next iteration of INNERif ($i > 70) { next; } # next iteration of INNER$i += 4;redo if $i < 70; # next iteration of INNERnext OUTER if $i == 42; # next iteration of OUTER
}}
perl-23
Terminating
Normal termination: call exit 0 at the end of
the script.
• accepts a list of arguments
• concatenates them all into a single string
• appends file name and line number
• prints this string
• and then terminates the Perl interpreter
Example:
if (! -r "myFile") {die "Can’t read myFile\(\backslash\)n";
}# ordie "Can’t read myFile\(\backslash\)n" if (! -r "myFile");
Convention: omit parentheses on die argument list if just
one argument
perl-24
Defining Functions
Perl functions (or subroutines) are defined viasub, e.g.
sub sayHello {print "Hello!\(\backslash\)n";
}
And used by calling, with or without &, e.g.
&sayHello; # arg list optional with &sayHello(); # more common, show empty arg list explicitly
perl-25
Defining Functions (cont.)
Function arguments are passed via a list variable@ , e.g.
sub mySub {@args = @_;print "I got ",$#args+1," args\(\backslash\)n";print "They are (@args)\(\backslash\)n";
}
Note that @args is a global variable.
To make it local, precede by my, e.g.
my @args = @_;
perl-26
Defining Functions (cont.)
Can achieve similar effect to the C function
int f(int x, int y, int z) {int res;...return res;
}
by using array assignment in Perl
sub f {my ($x, $y, $z) = @_;my $res;...return $res;
}
perl-27
Defining Functions (cont.)
For functions with array or hash parameters
sub ff {my ($x, @y, %z) = @_;...
}
Prototypes indicate expected parameter struc-
ture. Use one $ for each scalar and @ for a list.
Examples:
sub f($$$);sub ff($@%);
\begin{slide}\Heading{ Input/Output}
Files are accessed via {\em{handles}} ~ {\small (cf. Unix
<@><<</@>Handle<@>>></@> for an input file means "read the
E.g. ~~ <@> $line = <<STDIN>>; </@>
... stores the next line from standard input in the variable
Output file handles are used as the first argument to the
E.g. ~~ <@> print REPORT "Report for $today\(\backslash\)n";</@>
perl-28
... writes a line to the file attached to the <@>REPORT</@>
{\small Note: no comma after the handle ID}\end{slide}
\begin{slide}\ContHeading{ Input/Output}
Example (a simple <@>cat</@>):\makeatletter\begin{small}\begin{program}
#!/usr/bin/perl# Copy stdin to stdout
while ($line = <<STDIN>>) {print $line;
}
However, this can be simplified to:
while (<<STDIN>>) { print; }
# or even
print <<STDIN>>;
Defaults:
• the default destination variable for input is $
• the default argument for print is also $
Input/Output (cont.)
Handles can be explicitly attached to files via theopen command:
open(DATA, "<< data"); # read from a file called "data"open(RES, ">> results"); # write to a file called "results"open(XTRA, ">>> stuff"); # append to a file called "stuff"
Handles can even be attched to pipelines to read/writeto Unix commands:
open(DATE, "/bin/date |"); # read from output of dateopen(FEED, "| more"); # send output through the more
Opening a handle may fail:
open(DATA, "<< data") or die "Can’t open data file";
Handles are closed using close(HandleName) (or
automatically closed on exit).
perl-29
Input/Output (cont.)
The special file handle <>>
• treats all command-line arguments as file names
• opens and reads from each of them in turn
If there are no command line arguments, then
<>> == <STDIN>>
Example:
perl -e ’print <<>>;’ a b c
Displays the contents of the files a, b, c on std-
out.
perl-30
Perl and External Commands
Perl is shell-like in the ease of invoking other
commands/programs.
Several ways of interacting with external com-
mands/programs:
‘cmd‘; capture entire output of cmd as single
system("cmd")execute cmd and capture its exit status
open(F,"cmd|")collect cmd output by reading from a
perl-31
Perl and External Commands (cont.)
External command Examples:
$files = ‘ls $d‘; # string output from command
$status = system("ls $d"); # numeric status of command
You might find this a useful tool to test out your under-
standing of regular expressions.
perl-42
Lists as Strings
Recall the marks example from earlier on; we
used "54,67,88" to effectively hold a list of marks.
Could we turn this into a real list if e.g. we
wanted to compute an average?
The split operation allows us to do this:
Syntax: split(/pattern/,string) returns a list
The join operation allows us to convert from list
to string:
Syntax: join(’char’,list) returns a string
(Don’t confuse this with the join filter in the shell. Perl’s
join acts more like paste.)
perl-43
Lists as Strings (cont.)
Examples:
$marks = "99,67,85,48,77,84";
@listOfMarks = split(/,/, $marks);# assigns (99,67,85,48,77,84) to @listOfMarks
$sum = 0;foreach $m (@listOfMarks) {
$sum += $m;}
$newMarks = join(’:’,@listOfMarks);# assigns "99:67:85:48:77:84" to $newMarks
Note: argument parentheses are optional: I use them for
more than one argument or where it looks ambiguous
(though not for printf).
perl-44
Lists as Strings (cont.)
Complex splits can be achieved by using a fullregular expression rather than a single delimitercharacter.If part of the regexp is parenthesised, the corre-sponding part of each delimiter is retained in theresulting list.
$days{"Sun"} # returns "Sunday"$days{"Fri"} # returns "Friday"$days{"dog"} # is undefined (interpreted as "")$days{0} # is undefined (interpreted as "")
# inserts a new (key,value)$days{dog} = "Dog Day Afternoon"; # bareword OK as key
# replaces value for key "Sun"$days{"Sun"} = Soonday; # bareword OK as value
Perl functions (or subroutines) are defined viasub, e.g.
sub sayHello {print "Hello!\(\backslash\)n";
}
And used by calling, with or without &, e.g.
&sayHello; # arg list optional with &sayHello(); # more common, show empty arg list explicitly
perl-54
Defining Functions (cont.)
Function arguments are passed via a list variable@ , e.g.
sub mySub {@args = @_;print "I got ",@#args+1," args\(\backslash\)n";print "They are (@args)\(\backslash\)n";
}
Note that @args is a global variable.
To make it local, precede by my, e.g.
my @args = @_;
perl-55
Defining Functions (cont.)
Can achieve similar effect to the C function
int f(int x, int y, int z) {int res;...return res;
}
by using array assignment in Perl
sub f {my ($x, $y, $z) = @_;my $res;...return $res;
}
perl-56
Defining Functions (cont.)
Lists (arrays and hashes) with any scalar argu-
ments to produce a single argument list.
This in effect means you can only pass a single
array or hash to a Perl function and it must be
the last argument.
sub good {my ($x, $y, @list) = @_;
This will not work (x and y will be undefined):
sub bad {my (@list, $x, $y) = @_;
And this will not work (list2 will be undefined):
sub bad {my (@list1, @list2) = @_;
perl-57
References
References
• are like C pointers (refer to some other objects)
• can be assigned to scalar variables
• are dereferenced by evaluating the variable
Example:
$aref = [1,2,3,4];print @$aref; # displays whole array... $$aref[0]; # access the first element... ${$aref}[1]; # access the second element... $aref->[2]; # access the third element
perl-58
Parameter Passing
Scalar variable are aliased to the correspond-ing element of @ . This means a function canchanged them. E.g. this code sets x to 42.