1 / 13

Perl primer ...

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

derex
Download Presentation

Perl primer ...

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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. 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. 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. No other datatypes recognized ... • Strings, integers, floating-points • Interconverted, based on context • $result = "1" + 2.3 + 54;

  5. Associative array %ENV: current environment vars. ... • $ENV{'PATH'} • $ENV{'HOME'} • $ENV{'REMOTE_HOST'} • ...

  6. Retrieve line from STDIN ... • $var = <> • <> alone: --> default var $_

  7. Pattern matching ... • $var =~/pattern_to_match/ • Verify that contents of $var match specified pattern • Pull out matched subpatterns • egrep-style regular expressions

  8. Command execution, argument evaluation ... • system("command") • Invoke subshell • Execute command • eval("expression") • Evaluate argument as Perl expression • Return result

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

  10. 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. 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. 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. ($load_average) = ($uptime =~ /average: ([^,]*)/); ... ($load_average) = ($uptime =~ /average: ([^,]*)/)

More Related