Perl primer ...

13
Institute for Visualization and Perception Research IV P R 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 ...

description

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. 3 basic data structures. $variable: scalar - PowerPoint PPT Presentation

Transcript of Perl primer ...

Page 1: Perl primer ...

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 ...

Page 2: Perl primer ...

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. ...

Page 3: Perl primer ...

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

Page 4: Perl primer ...

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;

Page 5: Perl primer ...

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'}

• ...

Page 6: Perl primer ...

Institute for Visualization and Perception ResearchI VPR 6

© Copyright 1998 Haim Levkowitz

Retrieve line from STDIN ...

• $var = <>

• <> alone: --> default var $_

Page 7: Perl primer ...

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

Page 8: Perl primer ...

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

Page 9: Perl primer ...

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

Page 10: Perl primer ...

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.

Page 11: Perl primer ...

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"

Page 12: Perl primer ...

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

Page 13: Perl primer ...

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