Download presentation
Presentation is loading. Please wait.
Published byAubrey Spencer Modified over 9 years ago
1
Institute for Visualization and Perception Research 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...
2
Institute for Visualization and Perception Research 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....
3
Institute for Visualization and Perception Research 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
4
Institute for Visualization and Perception Research 4 © Copyright 1998 Haim Levkowitz No other datatypes recognized... Strings, integers, floating-points Interconverted, based on context $result = "1" + 2.3 + 54;
5
Institute for Visualization and Perception Research 5 © Copyright 1998 Haim Levkowitz Associative array %ENV: current environment vars.... $ENV{'PATH'} $ENV{'HOME'} $ENV{'REMOTE_HOST'}...
6
Institute for Visualization and Perception Research 6 © Copyright 1998 Haim Levkowitz Retrieve line from STDIN... $var = <> <> alone: --> default var $_
7
Institute for Visualization and Perception Research 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
8
Institute for Visualization and Perception Research 8 © Copyright 1998 Haim Levkowitz Command execution, argument evaluation... system("command") Invoke subshell Execute command eval("expression") Evaluate argument as Perl expression Return result
9
Institute for Visualization and Perception Research 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 all text till END as double quoted Easier to read
10
Institute for Visualization and Perception Research 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.
11
Institute for Visualization and Perception Research 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"
12
Institute for Visualization and Perception Research 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);
13
Institute for Visualization and Perception Research 13 © Copyright 1998 Haim Levkowitz ($load_average) = ($uptime =~ /average: ([^,]*)/);... ($load_average) = ($uptime =~ /average: ([^,]*)/)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.