Perl, Part A - bhecker.com
Download
Report
Transcript Perl, Part A - bhecker.com
An Introduction to Perl
1
PERL: Practical Extraction and
Report Language
Developed in 1986 by Larry Wall
Perl is very portable, very friendly to
the operating system
Perl has gained wide distribution and
is readily available.
Perl programs syntax are taken from
shells scripts, awk and C programs.
2
PERL: syntax
• A bunch of statements and declarations like
in a shell script (no main( ) like in C);
• Each statement ends with a semi-colon (;)
• #!/usr/bin/perl -w (w for warnings) at the
beginning of your program.
(like sed and awk) PERL, completely parses
and compiles the program into an internal
format before executing any of it.
• (unlike sed and awk) PERL executes
sequences of statements exactly once.
3
PERL: Features
• rich collection of built-in operators /
functions
• regular expressions capabilities
• no arbitrary limits on
– array sizes
– length or content of strings
• comes with a symbolic debugger
4
PERL Features (cont’)
• at first used as pattern -scanning and report
generating language
• now used for
–
–
–
–
–
system administration
client-server programming
CGI programming for WWW
database access
other tasks
• many modules available: examples
– CGI, LWP, DBI, Tk
5
PERL: Example:
#!/usr/bin/perl -w
# -w issues
`warnings
print “What is your favorite color?
“;
$color = <STDIN>;
chomp($color);
if ($color eq ‘blue’) {
print “That is my favorite!
\n”;
} elsif ($color =~ /black/) {
print “ I do like $color for
my shoes\n”;
6
Running Perl in UNIX
• Explicit invocation of a program
$perl options program arguments
• Using the #! Directive in program
$program arguments
• Debugging mode
$perl -d program args
• Interactive debugging to test statements
$perl -de 0
7
Perl Data Structures
• Three data structures
– scalars
– arrays
– associative arrays, known as “hashes”
• arrays are indexed by numbers, starting
with 0
• hashes are indexed by string
8
Perl Data structures
• Scalars:
– $str = “The world is round” ;
– $num = 134.99;
#string var
#numeric val
• Arrays
–
–
–
–
@students =(“Mike, “Lisa”, “John”);
print $students[0];
#print Mike
$students[3]= “Julie”;
#add a new element
@students = ();
#empty array
9
Perl data structures (cont’)
• Hashes
– %emp =(“Julie”, “President”, “Mary”, “VP”);
– print $emp {“Julie”};
#print
president
– print $emp{Julie}
– $emp{John} = “controller”;
– %emp =();
# empty hash
10
Scalars: STRINGS and NUMBERS
• Perl supports strings and numbers as
the simplest kind of data.
A scalar: is either a number or a string
of characters.
A scalar value can be acted upon with
operators (like + or concatenation)
yielding a scalar result.
11
Perl data structures
reference: Perl data structures (MIT)
• Scalar values are named with $, even when
referring to a scalar that is part of an array
• it works like the English word “the”
$days
$days[28]
$days{‘Feb’}
%days
$#days
#simple scalar value days
#the 29th element of array @days
# the ‘Feb’ value from hash
#the last index of array @days
12
Perl data structures
reference http://www.mit.edu/perl/perldata.htm
• Entire array and array slice are denoted by
‘@’, which works like “these” or “those”
@days
#$days[0], $days[1],…$days[n])
@days[3,4,5] #same as @days[3..5]
@days{‘a’,’c’} #same as ($days{‘a’},$days{‘b’})
Entire hashes are denoted by %
%days
# (key1, val1, key2, val2 …)
13
Perl: namespace
reference: Perl data structures (MIT)
• Each variable type has its own namespace
– you can use, with fear of conflict, same name
for a scalar variable, array, hash, subroutine.
Example: $foo and @foo are two different
variables
• Context:
– scalar and list
– certain operations return list values in contexts
wanting a list, and scalar otherwise
14
PERL: Operators for numbers
Addition, subtraction, multiplication ,
division, etc..
For integers, a range .. operator to
specify a range of integers.
– Example : print 1. . 9"\n"
–123456789
15
PERL: Operators for strings:
Concatenation operator "."
– Example: print "hello" . "world" ;
helloworld
String repetitor operator x
– string x num makes as many
concatenated copies of the string as
indicated by num
– "fred" x 3
# is "fredfredfred"
16
PERL: Numeric and String comparison
operators
Comparison
•
•
•
•
•
•
Numeric
equal
==
not equal
!=
less than
<
greater than
>
less than or equal to <=
greater than or equal to
>=
String
eq
ne
lt
gt
le
ge
17
PERL:SCALAR VARIABLES
Scalar variables are like those of the
shell:
A scalar variable holds a single scalar
value;
Variables can be assigned any type of
scalar value and Perl keeps track of the
type for you.
$ is always used to denote the variable
and also to expand the value of the
variable
18
PERL: Scalar variables
(see Perl mit)
• $var = value; = is the assignment
operator
• print "$var \n"; prints the value
of var
• $car = "buick";
• $count = 1;
19
More on scalars
• Not required to declare or initialize
• pattern matching
– if ($var =~ m/pattern/) {….}
– if ($var !~ m/pattern/) { …}
• substitution
– $var1 =~ s/pattern/replacement/;
20
FUNCTIONS ON SCALAR VARIABLES
chop($var) takes a single argument, a
scalar variable, and removes the last
character from the string value of that
variable.
– $doc = "the Perl document"
– chop($doc); $doc is now "the Perl
documen"
chomp($var) removes only a newline
character at the end if there is one:
– $a = "hello world\n";
– chomp($a); # is now "hello world"
– chomp($a); # nothing happens
21
<STDIN> as a scalar value
• when <STDIN> is used where a scalar
value is expected,
• Perl reads the next complete text line from
standard input (up to the first newline) and
uses that string as the value of <STDIN>
$a = <STDIN>;# get the text
chomp($a);
# get rid of that pesky
newline
same as: chomp($a =
<STDIN>);
22
PERL: ARRAYS
•
A list is an ordered scalar data.
An array is a variable that holds that list.
arrays are dynamically allocated
An array is denoted by an "@" sign:
– example:
– @arr = (1, 2 , 3, 4, 5);
23
PERL: ARRAY OPERATORS AND
FUNCTIONS
Assignment:
@fred = (1, 2, 3);
@barney = @fred;
in @barney
#copies @fred
if an array variable is assigned to a scalar
variable, the number assigned is the length
of the array (i.e., number of elements)
– $length = @arr ;
24
PERL: ARRAY OPERATORS AND
FUNCTIONS
access a single element with index
number in brackets. Note that the @ on
the array name becomes a $ on the
element reference
• example:
print $arr[0], "\n";
# prints
first element of @arr which is 1
25
ARRAYS (cont')
undef is returned when accessing an
array element beyond the end of the
current array.
$#arrname is used to get the index of
the last element of array @arrname.
26
ARRAY FUNCTIONS
push and pop functions
• when arrays are used a stack of information,
where new values are added to and removed
from the right hand side of the list.
• Example:
push(@mylist, $newvalue); like
@mylist = (@mylist, $newvalue);
$oldvalue = pop(@mylist);
removes last element of my list.
27
ARRAY FUNCTIONS (cont')
shift and unshift functions
• Perform similar action to push and pop
on the "left" side of a list.
• Example:
@fred = (5, 6, 7);
unshift(@fred, $a) ; like @fred = ($a,
@fred);
$x = shift(@fred);
x gets 5,
@fred is now (6, 7)
28
ARRAY FUNCTIONS (CONT')
sort function
• returns a sorted list in ascending ASCII
order without changing original list
reverse function
• reverses the order of the elements of its
argument, returning the resulting list
@a = (1, 2, 3);
@b = reverse(@a);
2, 1);
# means @b = (3,
29
ARRAY FUNCTIONS (CONT')
sort function
• returns a sorted list in ascending ASCII
order without changing original list
chomp function
• @stuff = ("hello\n", "hi\n", "ola\n");
• chomp(@stuff);
# @stuff is now
("hello", "hi", "ola")
30
Filtering a list
• Grep function may be used to select from
list
–
–
–
–
@toys =qw(car racecar truck bluecar train);
@cars = grep(/car/, @toys);
print “@cars\n”;
car racecar bluecar
• grep from any list
– @daemons = grep /daemon/, `ps-ef`;
31
Iterating through an array
• C-style for loop
for( $I = 0; $I < @myarray; $I++) {
print “$myarray[$I]\n”;
}
• foreach loop
foreach $element (@myarray) {
print “$element\n”;
}
32
<STDIN> as an array
• in a list context, <STDIN> returns all
remaining lines up to the end of a file
each line is returned as a separate
element of the list.
• @a = <STDIN> # read standard input
in a list context.
33
@ARGV Array
• @ARGV array contains command line
arguments
• @ARGV does not include program name,
• $0 contains program name
• use in scalar context to determine argument
count
if (@ARGV !=2) {
die: “usage: $0 infile argument\n”;
}
34
PERL: HASHES
• A hash is a collection of scalar data with
individual elements selected by some index.
Index values are arbitrary scalars called
keys; They are used to retrieve values from
the array.
Elements of a hash have no special order
A hash is denoted by "%" sign
Elements of a hash are referenced by
$hashname{key}
35
HASH: Examples
• Example: %carcolor = ("buick",
"yellow");
• $mycolor = $carcolor{ "buick"};
• $mycolor is now "yellow"
• %copy = %original; # copy from
%original to %copy
36
More Hash examples
• Using the => operator
– %carcolor = {
buick
=>
ge
=>
nissan =>
}
‘yellow’
‘red’
‘blue’
37
HASH FUNCTIONS
• keys functions:
• keys(%hashname) yields a list of the
current keys in the hash %hashname.
• Example: keys(%hashname) = keys
%hashname;
# once for each key of % fred
foreach $key (keys (%fred)) {
print "at $key we have $fred{$key} \n";
# show key and value
38
Hash Keys
• Order of keys is unpredictable
• special %ENV hash stores environment
variables
•
print $ENV{“SHELL”}
– SHELL =/bin/csh
39
HASH FUNCTIONS (CONT')
• values function:
• values(%hashname) yields a list of the
current values of %hashname in the
same order as keys returned by
keys(%hashname)
• %lastname = ("barney", "flinstone",
"gerry", "smith");
• @lastname = values(%lastname);
#grab the values
40
HASH FUNCTIONS (each)
each function:
• each(%hashname) returns a key-value
pair as a two element list.
• Used to iterate over an entire hash
(examining every element of ).
Example:
while (($first, $last)) = each(%lastname)) {
print "the last name of $first is $last\n";
}
41
Hash function: delete
• removes hash elements, takes a hash
reference as argument
• delete $lastname{"barney"}; #lastname
has only one key-value pair now.
42
Checking hash keys or values
• Checking for a true/false value
if ($emp{John}) { …}
• checking for an exsiting key
if (exists $emp{john}) {…}
• checking for an existing key and a defined
value
if (defined $emp{John})
{..}
43
Checking hash keys or values
(cont’)
• Undefining a value
undef $emp{john};
• deleting a key-value pair from hash
delete $emp{john}
• checking to see if hash contains anything
if (%emp) {..}
44
CONTROL STRUCTURES
Perl supports "if", "for" while" similar
than those in C.
"foreach" constructs is from the C shell
• foreach example:
– If the list we are iterating over is made of
real variables rather than some functions
returning a list value, then the variable
being used for iteration is in fact an alias
for each variable in the list instead of being
a merely copy of the values
45
Foreach example
• Example:
@numbers = ( 3, 5, 7, 9)
foreach $one (@numbers ) {
$one*=3;
}
# @numbers is now (9,15,21,27)
46
BASIC I/O
Input from STDIN
• Perl uses the variable $_ to contain the line
read from STDIN.
• $a = <STDIN>;
#reads the next line
• @a = <STDIN>;
# reads all lines until
control ^D
• typically
while (defined ($line = <STDIN>) {
# process $line here
}
when no more lines read. <STDIN> returns undef.
47
using the diamond operator <>
<> operates like <STDIN>, but gets
data from file or files
• specified on the command line that
invoked the PERL program.
• <> looks at the @ARGV array
#!/usr/bin/perl
while (<>) {
print $_;
}
48
Output to STDOUT
• print for normal output
• printf for formatted output
49
REGULAR EXPRESSIONS
PERL supports the same regular
expressions as in SED.
=~ match operator
• It takes a regular expression operator on the
right side and changes the target of the
operator to some value.
• The target of the =~ operator can be any
expression that yields some scalar string
value.
Example: if (<STDIN> = ~ /^[yY]/) {
print" what can I do for you? ";
50
Regular expressions:
split function
• split function takes a regular expression
and a string, and looks for all
occurrences of the regular expression
within that string.
• Parts of the string that don't match the
regular expression are returned in
sequence as a list of values
51
Example of split function
$line =
"merlyn::118:120:Randal:/home/merlyn:/usr/bi
n/perl";
@fields = split(/:/, $line); # split $line, using : as
delimiter
# now @fields is ("merlyn,"", "118", "120",
"Randal",
#
"/home/merlyn", "/usr/bin/perl")
52
join function
• takes a list of values and glues them
together with a glue string between
each list element.
• Example: $outline = join(":", @fields);
53
PERL :FUNCTIONS
• Defining a user function
sub subname {
Statement_1;
Statement_2;
Statement_3;
}
return value is the value of the return
statement or of the last expression
evaluated in the subroutine.
54
Calling a function/subroutine
• $var = myroutine( paramters);
– or
• @array = myroutine( parameters);
55
Function Arguments
• Subroutine invocation is followed by a
list within parenthesis
• Causing the list to be automatically
assigned to a special variable named
@_ for the duration of the subroutine.
• $_[0] is the 1st element of the @_ array
• @_ variable is private to the subroutine.
56
Function private variables
• using the my operator
sub add {
my ($sum);
#make $sum a local variable
foreach $_ ( @_) {
$sum += $_;
# add each element
}
return $sum
#last expression evaluated
}
57
Functions: Semi-private variables
semi-private variables using local
• local variables are visible to functions
called from within the block in which
those variables are declared.
58
Requiring Variable Declarations
• Programmer may choose to require
declarations
– use strict or
– use strict ‘vars’
• compiler will be strict in requiring that
– variable must be declared private with my
– otherwise, variable must be qualified with
package names
59
Functions:arguments
• Passing arrays
$score = average(@maths, @english, @french);
• passing references to arrays
• passing hashes
– %checks = payroll(%emp);
• passing references to hashes
60
Signal handling
• Special hash %SIG used for signal handling
• hash keys are the signal names
– example: SIGCHLD, HUP
• hash values are the signal decisions
– ignoring signals:
$SIG{INT} = ‘IGNORE’;
– resetting the default
$SIG{INT} = ‘DEFAULT’;
– catching signals
$SIG{INT} = \&mycatch;
61
FILEHANDLES
• Recommendation use all uppercase
letters in your filehandles .
• 3 files handles,
• STDIN,
for standard in
• STDOUT,
for standard out
• STDERR
for standard error.
62
Filehandles (cont’)
• Opening a file for reading:
open(FILEHANDLE, "filename");
• die: equivalent to "open that file or die.”
• opening a file for writing (> or >> for append)
open(FILEHANDLE,”>filename") | |
die "Sorry could not create filename\n";
63
FileHandles (cont’)
• Writing to a file (note, no comma after file
handle)
print FILEHANDLE “hello world\n”;
• PERL provides -op file tests just like the
shells
64
Executing Unix commands from
a PERL script
• To capture command output, use command
substitution:
– capturing output in a scalar:
$var = `who`;
– capturing output in an array
@output = `who`;
• to execute command, without capturing
output,use system
$status = system(“who”);
65
Reading Directory files
(references: Perl CookBook)
• Use opendir to set up a directory handle
$dir =“/d/d1/test”;
– opendir DH, $dir or die “cannot open
$dir: $!”;
• use readdir in scalar context to read next
entry
• while (defined($filename = readdir
DH)) {
print $filename\n”;
}
66
Reading Directory files
(cont’) (references: Perl CookBook)
• use readdir in list context to read all entries
@files= readdir DH;
• other directory routines
closedir DH
handle
rewinddir DH
start
close directory
#reposition to
67
USER DATABASE MANIPULATION
• Most UNIX systems have a standard library
called DBM, which allows programs to store a
collection of key-value pairs into a pair of disk
files.
• In Perl, a hash may be associated with a
DBM through a process similar to opening a
file.
• dbmopen function associates a DBM
database with a DBM array:
68
Database interface example
dbmopen(%ARRAYNAME, "dbmfilename",
$mode);
dbmopen(%FRED, ."mydatabase",
0644);
delete $FRED{"barney"}
while (($key, $value) =
each(%FRED)) {
print "$key has value of
$value\n";
}
69
Perl debugging
•
•
•
•
•
•
•
•
•
perl -d
h print out a help message
T stack trace
s insgle step
n next
f finish
c continue
q quit
D delete all breakpoints
70
SYSTEM CALLS
• Perl provides an interface to many UNIX
system calls.
• Interface is via Perl functions, not
directly through the system call library.
• The interface use is dependent on the
implementation and version of Perl
being used.
71
Perl Modules
(references: PERL Cookbook)
• Perl modules may export symbols to your
namespace
• must use use to import these symbols
– use CGI;
• may request only certain symbols be
imported:
– use CGI qw($ONE $TWO);
72
Steps to making a Module
(references: PERL Cookbook)
• Module names should not be all lower case
• module, file and package names should
match
• module name
filename
• Modabc
Modabc;
Modbc.pm package
package statement
73
Steps to making a module:
(references: PERL Cookbook)
• Most modules should load in the standard
Exporter module, and then create three
special arrays:
use Exporter;
@ISA = qw(….);
#names of base
classes
@EXPORT = qw(…);
#default
symbols to export
@EXPORT_OK =qw(…) #symbols to
export on request
74
Steps to making a module
(references: PERL Cookbook)
• Next, comes any module initialization code
• function definitions
• file needs to return some true value to
indicate success
1;
#last statement in file
75
End of Lecture
76