hp e3000 perl programming perl programming presented by Mark Bixby mark_bixby@hp.
hp e3000 introduction and history perl programming • Practical Extraction and Report Language • Pathologically Eclectic Rubbish Lister? • the Swiss Army chainsaw of scripting languages • optimized for text processing • combines the best of C, sh, awk, and sed • released in 1987 by Larry Wall • initially ported to MPE by Mark Klein • re-ported by Mark Bixby in 1997 with periodic updates since then • "There's more than one way to do it!" Solution Symposium February 9, 2001 Page 2
hp e3000 current status perl programming • latest & greatest Perl release v5.6.0 available for MPE from bixby.
hp e3000 installation perl programming • download from http://www.bixby.org/mark/perlix.
hp e3000 variable names • scalar values perl programming • $days # the simple scalar value "days" • $days[28] # the 29th element of array @days • $days{'Feb'} # the 'Feb' value from hash %days • $#days # the last index of array @days • entire arrays or array slices (aka lists) • @days # ($days[0], $days[1],... $days[n]) • @days[3,4,5] # same as @days[3..5] • @days{'a','c'} # same as ($days{'a'},$days{'c'}) • entire hashes • %days # (key1, val1, key2, val2 ...
hp e3000 value constructors • scalar values perl programming • $abc = 12345; • $abc = 12345.
hp e3000 scalar vs.
hp e3000 simple statements perl programming • terminated with a semicolon • may be followed by one optional modifier • if EXPR • unless EXPR • while EXPR • until EXPR • foreach EXPR • $os = 'mpe'; • $os = 'mpe' if $model == 3000; Solution Symposium February 9, 2001 Page 8
hp e3000 compound statements perl programming • a block is a sequence of statements delimited by curly brackets (braces) that defines a scope • compound statements that control flow: • if (EXPR) BLOCK • if (EXPR) BLOCK else BLOCK • if (EXPR) BLOCK elsif (EXPR) BLOCK ...
hp e3000 subroutines perl programming sub max { my $max = shift(@_); foreach $foo (@_) { $max = $foo if $max < $foo; } return $max; } $bestday = max($mon,$tue,$wed,$thu,$fri); • parameters passed via @_ array • @_[0] = parm1, @_[1] = parm2, etc • @_ is an alias (i.e.
hp e3000 arithmetic operators perl programming • addition: + • subtraction: • multiplication: * • division: / • modulus: % • exponentiation: ** • auto-increment and -decrement: ++ -• ++$a - increments $a, returns new value • $a++ - returns current value, then increments $a Solution Symposium February 9, 2001 Page 11
hp e3000 assignment operators perl programming • works like C • $a += 2; is equivalent to $a = $a + 2; • **= += *= &= <<= &&= -= • |= >>= ||= .
hp e3000 relational operators perl programming • numeric comparisons: •< > <= >= == != <=> • <=> returns -1, 0, or 1 depending on whether the left argument is numerically less than, equal to, or greater than the right argument • string comparsions: • lt gt le ge eq ne cmp • cmp returns -1, 0, or 1 depending on whether the left argument is stringwise less than, equal to, or greater than the right argument Solution Symposium February 9, 2001 Page 13
hp e3000 bitwise operators perl programming • shift left: << • shift right: >> • AND: & • OR: | • XOR: ^ • negation: ~ Solution Symposium February 9, 2001 Page 14
hp e3000 i/o and file handles perl programming • open files are identified via file handles • uppercase handle names by convention • predefined file handles: STDIN, STDOUT, STDERR • in a scalar context reads the next record from the file • in a list context reads ALL of the remaining records from the file Solution Symposium February 9, 2001 Page 15
hp e3000 opening files with open() perl programming • open(HANDLE, "/file/path") - open for reading • open(HANDLE, "< /file/path") - open for reading • open(HANDLE, "> /file/path") - open for writing • open(HANDLE, ">> /file/path") - open for appending • open(HANDLE, "| shell command") - open pipe for writing • open(HANDLE, "shell command |") - open pipe for reading • be very careful when passing user data to open() as a file name! Solution Symposium February 9, 2001 Page 16
hp e3000 a file i/o example perl programming #!/PERL/PUB/perl open(HPSW, "/SYS/PUB/HPSWINFO"); # open for input $one = ; # read first line $two = ; # read second line $three = ; # read third line @therest = ; # read all remaining lines close(HPSW); # close the file open(PATCHES, "> /tmp/MPE.
hp e3000 regular expressions • a vast superset beyond standard Unix regexps perl programming • a ? modifier to make patterns non-greedy • zero-width lookahead and lookbehind assertions • conditional expressions • extra character class matches: • \w - match a "word" character (alphanumeric, "_") • \W - match a non-word character • \s - match a whitespace character • \S - match a non-whitespace character • \d - match a digit • \D - match a non-digit • http://www.perl.com/pub/doc/manual/html/pod/perl re.
hp e3000 using regular expressions perl programming $showme=`callci showme`; if ($showme =~ /RELEASE: ([A-Z]\.(\d)(\d)\.\d\d)/) { $release = $1; # the matching V.UU.FF $mpe = "$2.$3"; # the matching U and U (i.e. 7.
hp e3000 how to execute the interpreter perl programming • /PERL/PUB/perl [optional parameters] • #!/PERL/PUB/perl [optional parameters] • -c - check syntax without doing execution • -d - run the Perl debugger • -e - specify one line of script (like sed) • -v - print minimal version information • -V - print verbose version information • -w - prints VERY useful syntax and runtime warnings!!! Solution Symposium February 9, 2001 Page 20
hp e3000 predefined variables perl programming • $| or $OUTPUT_AUTOFLUSH • By default, all Perl output is buffered (0). To enable automatic flushing, set this variable to 1. Needed when doing MPE I/O which is usually unbuffered.
hp e3000 built-in functions - a partial list • Functions for SCALARs or strings perl programming • chomp, chop, chr, crypt, hex, index, lc, lcfirst, length, oct, ord, pack, q/STRING/, qq/STRING/, reverse, rindex, sprintf, substr, tr///, uc, ucfirst, y/// • Regular expressions and pattern matching • m//, pos, quotemeta, s///, split, study, qr// • Numeric functions • abs, atan2, cos, exp, hex, int, log, oct, rand, sin, sqrt, srand • Functions for real @ARRAYs • pop, push, shift, splice, unshift • Functi
hp e3000 built-in functions (cont.
hp e3000 built-in functions (cont.
hp e3000 debugging perl programming • invoke the debugger by starting Perl with the -d parameter • #!/PERL/PUB/perl -d • examine or modify variables • single-step execution • set breakpoints • list source code • set actions to be done before a line is executed • a 53 print "DB FOUND $foo\n" • debugger terminal I/O may act a bit strangely on MPE Solution Symposium February 9, 2001 Page 25
hp e3000 interprocess communications perl programming • POSIX signals between related processes • named pipes between unrelated processes • create named pipes with POSIX mkfifo command • unnamed pipes to child processes • create using Perl open() function with "|" • Internet-domain TCP and UDP sockets • Unix-domain stream sockets • SysV IPC - shared memory, semaphores, messages Solution Symposium February 9, 2001 Page 26
hp e3000 sockets - a procedural client example perl programming #!/PERL/PUB/perl -w use Socket; $proto = getprotobyname('tcp'); $ipaddr = inet_aton('localhost'); $port = getservbyname('daytime', 'tcp'); $address = sockaddr_in($port, $ipaddr); socket(SOCK, PF_INET, SOCK_STREAM, $proto); connect(SOCK, $address); $timestamp = ; print "$timestamp\n"; close(SOCK); Solution Symposium February 9, 2001 Page 27
hp e3000 perl programming sockets - an object-oriented client example #!/PERL/PUB/perl -w use IO::Socket; $remote = IO::Socket::INET->new( Proto = 'tcp', PeerAddr = 'localhost', PeerPort = 'daytime'); $timestamp = <$remote>; print "$timestamp\n"; close($remote); Solution Symposium February 9, 2001 Page 28
hp e3000 perl programming comprehensive perl archive network (cpan) • http://www.cpan.org/ • a vast collection of free Perl modules • ~800 megabytes total as of July 2000 • mirrored at more than 100 sites around the world • typical installation process for a CPAN module: • perl Makefile.
hp e3000 object oriented programming perl programming • an object consists of: • attributes (data) • methods (functions to manipulate the attributes) • many CPAN modules are object-oriented • for more info: • http://www.perl.com/pub/2000/12/begperl5.html • http://www.perl.com/pub/doc/manual/html/pod/perltoot .
hp e3000 object definitions example - Foo.
hp e3000 object usage example perl programming #!/PERL/PUB/perl use Foo; # refers to Foo.
hp e3000 perl extensions perl programming • binary code residing in an external NMXL loaded at run time • a thin layer of C that allows the Perl interpreter to call compiled code written in other languages • several extension libraries come bundled with Perl (sockets, POSIX, etc) • a decent tutorial is available - the examples even work on MPE! • http://www.perl.com/pub/doc/manual/html/pod/perlxstut.
hp e3000 web server cgi - a simple example use CGI qw(:standard); perl programming print header; print start_html('A Simple Example'), h1('A Simple Example'), start_form, "What's your name? ",textfield('name'), p, "What's the combination?", p, checkbox_group(-name=>'words', -values=>['eenie','meenie','minie','moe'], -defaults=>['eenie','minie']), p, "What's your favorite color? ", popup_menu(-name=>'color', -values=>['red','green','blue','chartreuse']), p, submit, end_form, hr; Solution Symposium Februar
hp e3000 perl programming web server cgi - a simple example (cont.) if (param()) { print "Your name is ",em(param('name')), p, "The keywords are: ",em(join(", ",param('words'))), p, "Your favorite color is ",em(param('color')), hr; } print end_html; • http://stein.cshl.
hp e3000 mpe as a web client perl programming • it's now possible to write MPE applications that look like web browsers • perform simple HTTP GET requests, or even complicated HTTP POST requests to fill out remote web forms #!/PERL/PUB/perl use LWP::Simple; $webpage = get('http://www.bixby.org/mark/perlix.html'); • See http://www.linpro.
hp e3000 integration with mpe perl programming • for access to MPE commands: • system("callci mpe_command") • `callci mpe_command` • integration with Apache via mod_perl available from http://www.bixby.org/mark/apacheix.
hp e3000 perl resources perl programming • http://www.perl.com/ - the starting point for all things Perl • http://perl.oreilly.com/news/success_stories.html - how Perl is being used in real-life situations • http://www.perl.com/pub/2000/10/begperl1.html Beginner's Introduction to Perl • http://perl.apache.org/ - The Apache/Perl Integration Project • http://www.bixby.org/mark/perlix.html - for the latest info about Perl on MPE • Usenet newsgroups comp.lang.perl.
hp e3000 join the hp3000-l community! perl programming • Available as a mailing list and as the Usenet newsgroup comp.sys.hp.mpe • In-depth discussions of all things HP e3000 • Talk with other people using Perl on MPE • seek advice, exchange tips & techniques • Keep up with the latest HP e3000 news • Interact with CSY • http://jazz.external.hp.com/papers/hp3000-info.