Perl Summary
Nice tutorial: http://www.comp.leeds.ac.uk/Perl/basic.html
Our textbook is at: http://www.perl.org/books/beginning-perl/
Many references in this summary to Ford, Jerry Lee. Perl Programming for the absolute beginner. Thomson Course Technology: Boston, 2007.
get version: perl –v
get documentation: perldoc perl
hello world one liner: perl -e "print \"Hello World\n\";"
hello world perl script:
· PEPPER>more hello.pl
· #!/usr/bin/perl -w
· print "Hello world\n";
· PEPPER>chmod +x hello.pl
· PEPPER>perl hello.pl
· Hello world
perl is not compiled – it is interpreted.
# is called shebang
windows/mac – recognizes perl by .pl
linux recognizes by #!/sur/bin/perl
-w enables warnings.
First joke script:
#!/usr/bin/perl -w
#
# JokeMachine.pl
clear_the_screen();
$reply = " " ;
while ($reply ne 'yes') {
print 'Would you like to hear a joke? (yes/no)';
$reply = <STDIN> ;
chomp($reply);
if ($reply ne 'yes') {
print "\ntry again\n\n "
}
}
clear_the_screen();
print 'What disappears the moment you say its name?';
$reply = <STDIN> ;
chomp($reply);
if ($reply ne 'SILENCE' ) {
print "\nSorry wrong answer\n\n ";
}
else {
print "\nGreat job \n\n";
}
sub clear_the_screen {
for ($i = 0; $i < 25; ++$i){
print "\n";
}
}
Double and single quotes expand (variable interpolation) just like shell.
no commas on numbers but _ okay.
Can hold float.
can chain = with $x = $y = $z = 1;
dot concatenates and is used like an operator
$message = "one" . "two" so message holds onetwo.
$new = "one and inside $(message)" holds "one and inside onetwo" - use () to tell where the variable name ends.
x repeats and is used like an operator
#!/usr/bin/perl -w
$row = "_" x 75;
print "$row\n\n";
print "Monthly report\n";
print "$row\n";
$other = "h" x 4;
print $other;
Exponent : ** , so 5 ** 2 is 25
Modulus : %
Other operators: + - / *
casting by putting "int" before number as : $result = int 10 / 3 gives you 3.
Can use format string
$result = sprintf ("%.3f", 10/3);
print "$result\n";
printf("%.3f\n", 10/3);
operators :
· Unary operators: ++, --, .= (concat this and the next), +=
· Test Numbers: == and != and <=
· Test Strings: eq, ne, gt,lt, ge, le
· Logical operators: and or not ; || && ! also work – not example: 4 not 5
False:
· empty string which is ""
· zero value string "0"
· number 0
· undefined value (undef)
IO
· print "x" implies print STDOUT x
· <STDIN> resolves to the input up to and including a line feed
· Open: open filehandle mode filename
open ($filein, '<', "$readfile" )
or die "cannot open $readfile: $!";
· When reading for a script hosted on a web site, make sure the full file path is given. ex : /home/pe16131/public_html/input.txt
o Modes:
o Input : <
o output : >
o append: >>
o i/o: +>
· Read: <variable of filename to read>
o $buf = <$filein>
o Reads to end of line (\n) including \n
o chomp($buf) ; (effects $buf in place)
o @list = <$filein> reads in the entire file to the array
· Parse with split: ($x, $y, $z) = split /,/, $buf ; (splits at , char )
· Read Loop Typical
while ($buf = <$filein>) {
chomp($buf) ; remove \n
($x, $y, $z) = split /,/, $buf ; (splits at , char )
· Write: print filehandle string
o print $filein @fileContents;
o print $filein "hello".$x;
· Seek – set the read and write offsets – one shared offset
o Read/Write shares offset
o seek $filein , 0, 0 # goes to beginning
o seek filehandle, offset, startingat
o startingat options:
§ 0 – beginning
§ 1 – current position
§ 2 – end of file
· Tell – tell where the file offset is
o tell ($filein);
· Truncate – kill off the rest of the file
o truncate filehandle length
o truncate $filein, 0 – kill the whole file contents
o truncate $filein, tell ($filein) – kill up to the current offset
o
Decisions
· if ( ){ } elsif ( ) { } else { }
· unless() : if (not $x == 3) is the same as unless( $x == 3)
· can have unless ( ) { } else if ( _) …
·
tertiary: $result = $x >
7 ? "more than 7" : "less than 7"
Repetition
· while ( ) { }
o funky short while – put repeated statement before while – implied do?
o $x = 1; print $x++ while ($x <10); prints 123456789
· until ( ) { }
o same funky short until
o $x = 1; print $x++ until ($x >=10); prints 123456789
o
· do{ } while ( ) ;
· do{} until ( ) ;
· for – same as java for ($x=1; $x < 10;$x++)
· foreach - loop through list or array
o structure:
foreach iteratorVariableName (listname) {
# do stuff with iteratorVariableName containing one value
# at a time from array
foreach $name (@playernames) { print "$name\n";}
o leave iteratorVariableName out and access that variable with $_
foreach (@playernames) { print "$_\n";}
branch
·
exit optional value : ex exit 2;
- stops program execution
·
LABEL: the label begins the line and ends with a colon. Provides a
tag
·
Loop control: (and a block is a loop that executes once)
o
next : go directly to the continue if it exists,
otherwise test
o
last :
§ if just last: like
break – exits the current loop (no
continue statements done)
§ if last label –
breaks the labeled loop (MYLOOP: while)
o
continue : execute before the condition. last and redo skip continue, but next will execute continue
first.
o
redo : restart
the first statement in the loop. The continue block, if any, is not
executed.
o
goto : goto label, goto expr, and goto &name.
$x = 0;
while($x < 5) {
$x++;
print "hi
$x\n";
print
"before next;\n";
if ($x
== 2) { next; }
print
"after next $x\n";
if ($x == 4)
{ print "before
last \n" ;
last;
print
"after last\n " ;
}
if ($x == 3){
print
"before redo\n";
redo;
print
"after redo\n";
}
print
"bottom of loop\n";
} continue { print "doing my continue thing\n"; }
Functions to use
· lc ($reply) returns lower case value of reply's contents
· defined ($x) returns true if $x = undef.
o defined (&function) returns true if the function exists
Collections
·
Lists
o
creation with not name
§ comma separated: (,
, )
§ range: (A..Z)
§ list of variable
addresses: qw($x, $xy)
§ note: qw turns off variable interpretation
· Arrays
o named list
o has an index starting at 0
o Note: all arguments passed into a function can be found in @ARGV, with the filename as the first $ARGV[0]
o must assign at creation, so at minimum : @a = (); (empty list)
o Array variable has @ before name : @a
o Array element has $ before name and [ ] after : $a[2]
§ can take multiples with @a[3, 4, 6] to get an array of just index 3, 4, and 6
§ can get to end with $a[-1], and second to last with $a[-2]
§ must be $a[ ] for setting, but @a[ ] is okay for getting
o Array length:
§ $#a gives length of @a
§ $x = @a will also give the length because it is scalar scope – a single value is expected.
o setting later elements creates them and places undefined elements in between former end and new end
o adding and removing:
§ add another array element(s): push @a, "last one";
§ add another array element(s) to beginning: unshift @a, ("x", "y");
§ delete using delete $a[3] or undef $a[3]\
§ delete first element using shift @a
§ delete last element using pop @a
o testing: if ( exists $a[2]) or if (defined $a[2])
o make a list with split: @a = split(/ /, "x y z");
o reverse split with join : $x = join (' ' , @a);
o print space separated with @a
o sort with sort @a returns a sorted @a
§ sorting numbers has a/b low/high indicator used with
· @num = sort($a <=> $b) (3,5,32,5);
·
Hashes
o
value pair of unique key and then value
o
no guaranteed order and no index
o
has variable %b
o
creation
§ $b{'key'}= 'value';
§ %b = ('key1',
'value1','key2','value2')
§ %b=key 1=>
'value1', key2 => 'value2';
o
access
§ $b{key1}
§ delete $b{key1};
§ to loop, grab a key
list using "keys": foreach $x (sort keys
%b) { print $x;}
o
convert array to hash – no issue
§ @b = %b
§ %b = @b
· operations
o collections can be lvalue
o ($x,$y) = (10, 20);
o ($x,@array,$y) = (10, 20, 30, 40, 50) ; gives $x 10 and @array the rest.
o @a = @b is a full memory copy
o
Perl's built in code modules at: perldoc.perl.org/index-modules-P.html
functions you create
·
syntax:
·
sub functionname {
·
access values following call with @_
·
rename @_ using ($x, $y) = @_ if you want
·
my varname to make the variable private
to the function
·
return whatever }
·
call function:
o
var = function (
variable, variable) – but variables are not specified and ( ) not required
o
can use &function
instead.
Database
Packages:
·
Variables created in package can be accessed without packagename::
·
All variables declared following words "package namespace"
(with namespace being whatever) are in that package.
Modules
·
reusable code collections
(one package?)
·
import with : use modulename
o
Ex: use Env;
§ can include version
use Env 2
§ can include path :
use Env wq(C:\MyModules);
o
Alternate: require modulename
§ only pulled if
needed
·
access functions and
variables with modulename:: prefix.
o
with Env::function
·
Create:
o
package modulename
o
our $version = .2 # importer can specify use modulename
.2
o
our variablename; # means the variable is
available to importers
o
code
o
1; # because every module must return true and 1 is true)
·
@INC holds the search path for locating module code for importing
·
Finding and installing
o
CPAN utility
§ perl –MCPAN –e shell
§ need root authority
§ install and then
search
§ search with I /
keyword/
§ install
HTML::Template
§
Regular Expressions:
· =~ is a pattern matching operator. Regex will be enacted on the lvalue
o ex: if ("world has wizards" =~ m/has/)
· if a pattern is found, return true
o add options such as i to ignore case.
· shorten statements with implied options:
o m/pattern/ but m is implied.
o optional if $_ (last implied variable created, often with <STDIN> or in foreach) then =~ implied
o if (/has/) same as if ($_ =~ m/has/)
· s/pattern/replacement/options
o $mystring =~ s/one/two/g
· tr/old characters/new characters/
· matches in parentheses go to $1, $2, etc
if ($name =~ /(son.?)$/) {
print "$name found $1\n";
· grep a list
o grep {/pattern/} @list returns a smaller list
o @m_names = grep {/^M/} @mylist ; pulls all the names starting with M from mylist array and puts it into m_names array.
Perl Web Page Creation: (simplest explanation )
·
Set up the top of your script to indicate perl
and plain text
o
#!/usr/bin/perl
o
print "content-type:text/html;
charset=utf-8\n\n";
·
Access the page on the web
o
www.adelphi.edu/~pe16132/271/yourscript.pl
o
must be under public_html
·
File needs permissions set for read and execute (chmod +rw filename)
·
You will be printing HTML code from your script, so you will want
to use methods that make printing text easy:
o
use print qq to avoid having to use
double quotes and to not have to escape the double quotes
§ print qq(no double quotes are necessary around this parameter)
o
use a here document notation
§ print
<<KEYWORD_TO_STOP
§ then all you want to print
§ end with
KEYWORD_TO_STOP on a line by itself
o
Some Html you may need
§ surround all code
with <HTML></HTML>
§ inside HTML, place
<BODY> </BODY> so you will have <HTML><BODY>page
content</BODY></HTML>
§ use <BR/> instead of \n to move to the next line.
§ for a link use <a
href="the link url or
filename"> the text to click </a>
·
If you want to get input from the user on the web page,
<STDIN> does not work so use CGI:
use strict;
use CGI qw/:standard/;
my $search = param("input")||"";
<form method="post" action="myscript.pl">
<input type="test" name="input" />
<input type="submit" value="Submit "/>
</form>
note that input matched
the form's input field name "input" and myscript.pl should be
replaced with your script's name. It says to repost the same script once the
submit button is pressed.
·
When reading from a file on a web page, make sure the full file
path is given. ex : /home/pe16131/public_html/input.txt
Debugging
· perl –d scriptname.pl
o n = next statement without stepping into a function
o s = next statement, even if inside a function
o r = finish a subroutine and get to calling function
o R = run the program
o t = turn trace on
o c = continue to next breakpoint
o l = list the lines nearby
o x = examine a variable's value
o can also execute any statement you like
o b = set a breakpoint to line number, function name or current position
o L = list set breakpoints
· perl –d –el : to just write code in the debugger
Database
·
Package to use:
o
use DBI;
o
provides connection and statement objects
·
Sample method for grabbing user and pwd:
o
print "Enter user\n";
o
my $user = <STDIN> ;
o
chomp($user);
o
print "Enter password\n";
o
use Term::ReadKey;
o
ReadMode('noecho');
o
my $pswd = ReadLine(0);
o
chomp($pswd);
o
ReadMode ('normal');
·
Connection environment variables to set:
o
my $ENV{"DBI_DRIVER"} = "mysql";
o
my $ENV{"DBI_DSN"} = "pepperdb";
o
my $ENV{"DBI_USER"} = $user;
o
my
$ENV{"DBI_PASS"} = $pswd;
·
Connect:
o
$dbh=DBI->connect() or die "Error
opening database: $DBI::errstr\n";
·
Statement
o
prepare
§ $sth=$dbh->prepare("SELECT * from tutorials_tbl;") or die "Prepare failed: $DBI::errstr\n";
o
execute
§ $sth->execute() or die "Couldn't execute query: $DBI::errstr\n";
o fetch
§ $sth ->fetchrow_array
§ gives back an array of columns
· Repeatedly fetch construct:
o while (( $id, $name) = $sth ->fetchrow_array) {
o print "$name has ID $id\n";
o }
· Close
o Close the statement object
§ $sth->finish();
o Disconnect from the database
§ $dbh->disconnect || die "Failed to disconnect\n";
Given variables
· $main::TEMP or $TEMP; $PATH; %ENV
· $_ is the default parameter – some functions implicitly create it such as <STDIN> and others implicitly use it such as print
· $! is the message of the last error