Post on 08-Jan-2016
description
Institute for Visualization and Perception ResearchI VPR 1
© Copyright 1998 Haim Levkowitz
Perl primer ...
• Syntax similar to C, various shell script langs.• 3 basic data structures ...• Retrieve line from STDIN ...• Pattern matching ...• Command execution, argument evaluation ...• Variables within quotes ...• E.g., welcome.pl ...
Institute for Visualization and Perception ResearchI VPR 2
© Copyright 1998 Haim Levkowitz
3 basic data structures ...• $variable: scalar• @variable: array (indexed by integer; [ ])• %variable: associative array (indexed by string;
{ })• Prefix char. indicates type of element stored at
index ...• No other datatypes recognized ...• Associative array %ENV: current environment
vars. ...
Institute for Visualization and Perception ResearchI VPR 3
© Copyright 1998 Haim Levkowitz
Prefix char. indicates type of element stored at index ...
• $array[0]: 1st element of @array
• $array[1]: 2nd element
• $array{'index_name'}: value of %array at index 'index_name'
• $array{index_name}: same
Institute for Visualization and Perception ResearchI VPR 4
© Copyright 1998 Haim Levkowitz
No other datatypes recognized ...
• Strings, integers, floating-points
• Interconverted, based on context
• $result = "1" + 2.3 + 54;
Institute for Visualization and Perception ResearchI VPR 5
© Copyright 1998 Haim Levkowitz
Associative array %ENV: current environment vars. ...• $ENV{'PATH'}
• $ENV{'HOME'}
• $ENV{'REMOTE_HOST'}
• ...
Institute for Visualization and Perception ResearchI VPR 6
© Copyright 1998 Haim Levkowitz
Retrieve line from STDIN ...
• $var = <>
• <> alone: --> default var $_
Institute for Visualization and Perception ResearchI VPR 7
© Copyright 1998 Haim Levkowitz
Pattern matching ...
• $var =~/pattern_to_match/
• Verify that contents of $var match specified pattern
• Pull out matched subpatterns
• egrep-style regular expressions
Institute for Visualization and Perception ResearchI VPR 8
© Copyright 1998 Haim Levkowitz
Command execution, argument evaluation ...
• system("command")• Invoke subshell• Execute command
• eval("expression")• Evaluate argument as Perl expression• Return result
Institute for Visualization and Perception ResearchI VPR 9
© Copyright 1998 Haim Levkowitz
Variables within quotes ...• Inside text surrounded by double quotes:
interpolated
• "\n": new line
• Inside single-quoted text: ignored
• E.g. ...
• Backticks ==> execute command ...
• print <<END ==> all text till END as double quoted
• Easier to read
Institute for Visualization and Perception ResearchI VPR 10
© Copyright 1998 Haim Levkowitz
E.g., ...• $name = 'Haim';
• print "My name is $name."; ==> My name is Haim.
• print 'My name is $name.'; ==> My name is $name.
• print "My name is \n $name.";
• ==> My name is
• Haim.
Institute for Visualization and Perception ResearchI VPR 11
© Copyright 1998 Haim Levkowitz
Backticks ==> execute command ...
• `date` ==> execute date• Return output
• Often with chop()• Remove last newline from end of
output• chop($date=`date`); # $date becomes
"Tue Apr 16 1996"
Institute for Visualization and Perception ResearchI VPR 12
© Copyright 1998 Haim Levkowitz
E.g., welcome.pl ...#!/usr/local/bin/perl
print "Content-type: text/plain","\n\n";
print "Welcome to IVPR's WWW Server!", "\n";
$remote_host = $ENV{'REMOTE_HOST'};
print "You are visiting from ", $remote_host, ". ";
$uptime = `/usr/ucb/uptime`;
($load_average) = ($uptime =~ /average: ([^,]*)/); ...
print "The load average on this machine is: ", $load_average, ".", "\n";
print "Happy navigating!", "\n";
exit (0);
blank line between header and body
regular expression
Institute for Visualization and Perception ResearchI VPR 13
© Copyright 1998 Haim Levkowitz
($load_average) = ($uptime =~ /average: ([^,]*)/); ...
($load_average) = ($uptime =~ /average: ([^,]*)/)
Search $uptime
Match 0 or more characters till next "," store in $load_average