240 likes | 354 Views
Perl. P ractical E xtraction and R eporting L anguage. Perl. General purpose interpreted programming language Optimized for scanning and processing of “text†data Designed to be useful: easy-to-learn, easy-to-use text-processing, efficient, extensive
E N D
Perl Practical Extraction and Reporting Language
Perl • General purpose interpreted programming language • Optimized for scanning and processing of “text” data • Designed to be useful: easy-to-learn, easy-to-use text-processing, efficient, extensive • Smallness or elegance, as defined by traditional computer science, are not that important • Free: source code, documentation, use • Portable: more than 50 operating system platforms
History • Developed by Larry Wall in 1987 • release 1.0 in December 1987 • major release 5.0 in October 1994 • minor release 5.005 in July 1998 • current minor release 5.6 to know the version execute: perl -v
Portable • UNIX: AIX, BSD, HP-UX, IRIX, Linux, Solaris, Tru64, ... • Windows: Win3.1, Win95, Win98, WinNT • MacOS • Others: AmigaOS, AS400, BeOS, MPE/iX, OS/2, OS390, QNX, Plan 9, VMS, VOS, ...
Easy Things Should Be Easy • powerful data structures: scalar, array, hash, references; dynamic typing, polymorphic variadic functions; contexts • powerful pattern matching hard things should be possible • namespaces • object orientation • interfacing with the outside world: applications, computer languages, operating systems, natural languages
About Perl • Home Page of Perl http://www.perl.com/ • Perl Mongers (user groups) http://www.perl.org/ • CPAN (Comprehensive Perl Archive Network) http://www.cpan.org/
Getting Perl • UNIX: Linux, BSD already have it, others http://www.cpan.org/ports/ • Windows: http://www.activestate.com/ • MacOS: http://www.macperl.com/ • Others: http://www.cpan.org/ports/
Writing Perl • Use any Text Editor
Running Perl • From command line perl myprog.pl or with options and arguments perl -w myprog.pl *.txt • On UNIX use the #!Trick Make the first line of your script to be #!/usr/bin/perl, or wherever your Perl is located, and add execute permissions using chmod.
A Perl Program print "Welcome to Perl!\n"; • Comment Character - # • Goes at beginning of every line with comment • Function print • Outputs text indicated by quotation marks (“…”) • Escape sequence \n • When inside a string, moves cursor to next line • Statements terminated with semicolons (;) • Exception: where braces ({}) used to denote block of code
Configuring Personal Web Server (PWS) for Perl/CGI • To run CGI with PWS • Several modifications must be made in the Windows Registry • PWS must be enabled to execute Perl scripts – does not by default
Update Windows Registry to handle Perl scripts 1. Modify Windows Registry: • Run regedit: Start - Run - type regedit - click ok • Create new string value: HKEY_LOCAL_MACHINE - System - CurrentControlSet - Services - W3SVC - Parameters - Script Map (This key is responsible for file extensions and their executable associations in PWS.) Right click in the right window and select New, then String Value from the popup menu. Type .pl and press Enter. • Edit .pl entry: Double click the .pl entry and type the path for the Perl Interpreter - C:\Perl\bin\Perl.exe%s%s - click ok (No spaces allowed.) • Exit Regedit.
PWS must be enabled to execute Perl scripts 2. Set permissions for cgi-bin directory: • Run PWS: Double click the PWS icon shown on the Task bar • Click on the Advanced icon • Select the cgi-bin folder • Click on the Edit Properties button • Select the Read, Execute and Scripts check boxes • Click on the ok button.
Where CGI scripts are stored ? • On most web servers, the CGI mechanism has been standardized in the following way. • In the normal directory tree that the server considers to be the root you create a subdirectory named cgi-bin. • The server then understands that any file requested from the special cgi-bin directory should not simply be read and sent, but instead should be executed. • The output of the executed program is what it actually sent to the browser that requested the page. The executable is generally either a pure executable, like the output of a C compiler, or it is a PERL script. PERL is an extremely popular language for CGI scripting.
cgi-bin • cgi-bin directory is located on linux system with default installation of Apache server: /var/lib/apache/cgi-bin • cgi-bin directory is located on Windows system with Xitami server installed: C:\Xitami\cgi-bin • cgi-bin directory is located on Windows system with Personal Web Server (PWS) server installed: C:\Inetpub\wwwroot\cgi-bin
Running a cgi script • So, for example, imagine that you type the following URL into your browser: http://www.howstuffworks.com/cgi-bin/search.pl • The server recognized that search.pl is in the cgi-bin directory, so it executes search.pl (which is a PERL script) and sends the output from the execution to your browser. That is how the search engine for How Stuff Works is implemented.
Simple CGI Scripts • Assuming that you have access to a cgi-bin directory, and assuming that you know either the C programming language or PERL, you can do a whole bunch of interesting experiments with CGI. Let's start by creating the simplest possible CGI script. • The simplest possible HTML web page looks something like this: <html> <body> <h1>Hello there!</h1> </body> </html>
CGI program wrote in Perl • The simplest possible CGI script would, upon execution, create this simple, static page as its output. print "Content-type: text/html\n\n"; print "<html>\n"; print "<body>\n"; print "<h1>Hello there!</h1>\n"; print "</body>\n"; print "</html>\n";
cout << "Content-type: text/html\n\n"; • The line "Content-type: text/html\n\n" is special piece of text that must be the first thing sent to the browser by any CGI script. • If you forget, the browser will reject the output of the script.
Compiling a CGI script in Perl • You don’t have to. • Perl is an interpreted language. • As long as your Server knows where to find the Perl interpreter.
PERL script The simplest CGI script without line breaks. print "Content-type: text/html\n\n"; print "<html><body><h1>Hello World!</h1></body></html>\n";
PERL script for Apache on Linux and Xitami on Windows: #!/usr/bin/perl # The above line is pointing where the Perl interpreter is. print "Content-type: text/html\n\n"; print "<html><body><h1>Hello World!</h1></body></html>\n";
Practice • Implement the example cgi script on your serverto print Hello World!. • Go to the following site and implement the example cgi script “Forms Create Input for CGI Scripts” on your server in Perl. Syntax to read the environment variable is: print $ENV{'QUERY_STRING'}; How CGI Scripting Worksby Marshall Brain http://www.howstuffworks.com • Also write a cgi script that will take a name from a form and send you back on a dynamic Web page with server name and IP address. • Practice with the cgi scripts available in chapter_27 of Deitel’s Internet & WWW How to Program Book.
Test your CGI scripts on Apache Server running on csplinux • telnet to csplinux.saultc.on.ca • Create a directory called public_html in your home directory (if does not exist): e.g., mkdir public_html • Change to the directory public_html : cd public_html • Create a directory called cgi-bin : mkdir cgi-bin • Transfer your cgi scripts (e.g., firstperl.pl) to the cgi-bin directory using ws_ftp. • Using your telnet session: • Rename your cgi scripts from .pl to .cgi e.g., mv firstperl.plfirstperl.cgi • Change file permissions to executable e.g., chmod 755 firstperl.cgi • In your Browser’s address line type the following address and hit the Enter key. http://csplinux.saultc.on.ca/~username/cgi-bin/firstperl.cgi