Unformatted text preview:

EECS 339 Perl in a Nutshell Dinda Page 1 of 18 Perl in a Nutshell The purpose of this document is to introduce you to enough of the Perl language so that you can understand RWB, the Perl CGI script handed out as part of the first project, and to write your own Perl CGI scripts. In addition to the Perl book mentioned in your syllabus (“the Camel book”) and the pointers provided on the course web page, there are other resources on Perl available online and in bookstores. A Minimally typed, Context-dependent, Interpreted Scripting Language With A Heuristic Parser The above is a basic description of Perl 5.x (we will use Perl 5.10 in this class, which is installed in /usr/bin). What does it it mean? A scripting language is a high-level language that makes it easy to glue together programs. Perl is interpreted. When you run a Perl script, it is quickly compiled into an intermediate format for the Perl virtual machine which then executes it. In Perl, variables come in a few simple types. There is no such thing as a “char”, “int”, “real”, “float”, “double”, “struct foo”, or whatever. Instead, the type of a variable or constant is inferred from surroundings, from the context in which it is used. This means that type errors may happen as the program is running! Finally, Perl’s parser is heuristic, just like a parser for English. This means that there are many different ways of saying the same thing. In fact, there is a running joke that Perl can make sense of line noise. There is also a competition for the best Perl Poetry. Hello World This is the smallest Perl program: #!/usr/bin/perl –w use strict; print “Hello World\n”; The first line specifes that it is the perl interpreter that should execute this code. The “-w” indicates that Perl will print warnings at run-time for type mismatches and other issues. The second line brings in a Perl library, strict.pm. Strict will prevent you from doing things that would be dangerous within certain environments, such as in a CGI script. Finally, the third line prints “Hello World”. To run this program, you would place it in the file (“hello.pl”, for example), and then run: $ chmod 755 hello.pl $ ./hello.pl Hello World $ The “chmod” line sets the permissions on the file to include execute permission for you, your group, and everyone. These are also the permissions you need for CGI scripts. Here is the same program, except modified to act as a CGI script:EECS 339 Perl in a Nutshell Dinda Page 2 of 18 #!/usr/bin/perl –w use strict; use CGI qw(:standard); print header(); print start_html(“Hello World”); print “Hello World\n”; print end_html(); To run that as a CGI script, place it in ~you/www (or ~you/public_html, depending on your server’s configuration) and chmod 755 hello.pl it. Then you should be able to run it via http://339/~you/hello.pl. Asking Perl for help The Perl manual is available in the form of the perldoc command. Some examples of usage: perldoc –h # show to use perldoc perldoc perlfunc # show all Perl’s built-in functions. perldoc perlop # show all Perl’s built-in operators perldoc perlvar # show all Perl’s predefined variables perldoc perlmod # show how Perl modules work perldoc perlform # show how Perl’s output formatting tools work perldoc perllocale # show how Perl’s internationalization support works perldoc CGI # describe the CGI module perldoc DBI # describe the DBI module perldoc DBD::Oracle # describe the Oracle driver for DBI perldoc SOAP::Lite # describe the SOAP::Lite module perldoc IO::Socket::INET # describe Perl sockets perldoc –f split # detailed information on the split builtin function perldoc –q SQL # search the FAQ for references to SQL You may also find www.perl.com and www.cpan.org helpful. www.perl.com is the “official” site for perl and its ports. www.cpan.org, or CPAN, is the Comprehensive Perl Archive Network, the way to find perl modules and scripts that can help you. Adding things to Perl CPAN provides a HUGE range of scripts and modules that can be added to a Perl installation, making it possible to do, or interact with almost anything you could imagine. It’s a giant library maintained and shared by all the Perl users in the world. Perl includes an interactive interface to automatically install tools from CPAN. To start this, run perl –MCPAN –e shell By default, installation is to the shared directories on the system, which you can only do if you have root or administrator privileges. However, you can install your own localEECS 339 Perl in a Nutshell Dinda Page 3 of 18 extensions as well, and there is documentation on the CPAN site and elsewhere for how to do this. Variables and Literals Perl variables include scalars ($x), lists (@x), and hashes (%x). Scalars include ints, floating point values, strings, and references. The following are some examples. $a=5; $b=53.4; $c=”Hi!\n”; $cref=\$c; # \$ creates a reference to a scalar print $a, “\n“, $b, “\n”, $c, ${$cref}; A “reference” is similar to a C or C++ pointer. “$cref=\$c” is basically like “cref=&c” in C or C++, while ${$ref} is like “*cref” in C/C++, meaning “dereference the reference $cref and treat the result as a scalar.” You can also treat the result in other ways, like an a list (@{$cref}) or a hash (%{$cref}). Of course, this only makes sense if $cref “really” points to a list or hash. You can find out what kind of thing a reference points to using ref: print “\$cref points to a “,ref $cref,”\n”; We will discuss references as we go through this section. A list (or one dimensional array if you like), contains a list of scalar variables and . Any combination is OK. You can also find out the index of the last element of the list. @d = ($a,”Hi”, 34, 10.4,$cref); print @d; print “The last element of the list is $#d. The list has ”.($#d+1).” elements.\n”; print “The contents are\n”; for ($i = 0; $i<=$#d;$i++) { print “$d[$i]\n”; } You can convert lists to scalars by using join. Join concatenates list items using its first argument. We could write the above for loop as: print join(“\n”,@d), “\n”; There is no notion of a list of lists. @e = (@d,@d); just concatenates the two input lists. A typical approach to lists of lists is to use list references:EECS 339 Perl in a Nutshell Dinda


View Full Document

NU EECS 339 - Perl in a Nutshell

Download Perl in a Nutshell
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Perl in a Nutshell and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Perl in a Nutshell 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?