1 / 28

Programming in Perl

Programming in Perl. Perl is widely used to write CGI programs or scripts Perl is an interpreted language A Perl script is just a text file whose first line tells the operating system where to find the Perl interpreter

sonora
Download Presentation

Programming in Perl

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. Programming in Perl

  2. Perl is widely used to write CGI programs or scripts • Perl is an interpreted language • A Perl script is just a text file whose first line tells the operating system where to find the Perl interpreter • On Unix machines, Perl script files should have have execute permission enabled by using chmod 755 <filename> or chmod o+x <filename>

  3. Path-to-Perl-interpreter • The first line in a Perl script is of the form #!<path-to-perl-interpreter> • This tells the operating system where to find the Perl interpreter • On student.cs.ucc.ie, the Perl interpreter is at/usr/local/bin/perl • Therefore, the first line in Perl scripts on this machine should be #!/usr/local/bin/perl

  4. A “hello world” script • In your web directory on student.cs.ucc.ie write a text file, called prog1.pl, which contains the following: #!/usr/local/bin/perl print <<END; Hello, world! How are you? END • Make this executable by entering the following Unix command student.cs.ucc.ie> chmod 755 prog1.pl

  5. Executing this script • In response to the Unix prompt, type the command student.cs.ucc.ie> prog1.pl • You will get the response Hello, world! How are you? followed by another Unix prompt

  6. What was this script doing? • In Perl, a statement pair of the form print <<END; ... END simply print all the intervening lines directly to standard output • The word END is not a pre-defined word. We could have used print <<ANYWORD; ... ANYWORD

  7. Making this a CGI script • Change the file name to prog1.cgi • Edit the file, as follows: #!/usr/local/bin/perl print <<END; Content-Type: text/html Hello, world! How are you? END • Note the blank line above

  8. Executing this CGI script (a) • Suppose your user-name is xyz1 • Then the URL for your web directory is http://student.cs.ucc.ie/~xyz1 • Point your browser at http://student.cs.ucc.ie/~xyz1/prog1.cgi • Your browser screen will display: Hello, world! How are you?

  9. What was this CGI script doing? • By default, all output from a CGI script is parsed by the HTTP server demon before it is sent to the client • The HTTP server will add a status-line and any headers necessary to make an appropriate HTTP response • However, the CGI program must indicate what type of content it wants to send back to the client • This is done by prefacing the content with a Content-Type HTTP header line • This header must be followed by a blank line, to tell the HTTP server demon that there are no more header lines • We lied in this script, claiming that the content was text/html, when it was just plain text, in order to fool the browser into displaying it in the browser window

  10. Executing this CGI script (b) • Executing this CGI script from a browser obscures some details of the response • Let’s see them by using telnet • Again, suppose your user-name is xyz1 • Then the URL for your web directory is http://student.cs.ucc.ie/~xyz1 • Telnet to student.cs.ucc.ie, port 80 and send this request GET http://student.cs.ucc.ie/~xyz1/prog1.cgi HTTP/1.1 Host: student.cs.ucc.ie • The response is on the next slide

  11. HTTP/1.1 200 OK Date: Thu, 22 Feb 2001 18:20:14 GMT Server: Apache/1.3.14 (Unix) PHP/4.0.3pl1 Transfer-Encoding: chunked Content-Type: text/html 1a Hello world! How are you? 0 Connection closed by foreign host. interzone.ucc.ie>

  12. Analysis of this response • The HTTP server demon added the status line HTTP/1.1 200 OK • The HTTP server demon added the header lines: Date: Thu, 22 Feb 2001 18:20:14 GMT Server: Apache/1.3.14 (Unix) PHP/4.0.3pl1 Transfer-Encoding: chunked • The HTTP server demon chunked the message content 1a Hello world! How are you? 0 • The chunk size 1a, being 26 decimal, includes the white-space characters

  13. A CGI script producing real HTML • Edit the file, as follows: #!/usr/local/bin/perl print <<END; Content-Type: text/html <H1>Hello, world!</H1> <P>How are you?</P> END • Again, note the blank line above

  14. What would happen if you forgot to output a Content-Type header? • Consider a script called prog2.cgi whose contents are as as follows: #!/usr/local/bin/perl print <<END; Hello, world! How are you? END • Note the absence of a header above

  15. See what happens if we call it telnet student.cs.ucc.ie 80 Trying 143.239.211.125... Connected to student.cs.ucc.ie. Escape character is '^]'. HEAD student.cs.ucc.ie/cs4400/xyz1/prog2.cgi HTTP/1.1 Host: student.cs.ucc.ie HTTP/1.1 400 Bad Request Date: Thu, 22 Feb 2001 21:34:25 GMT Server: Apache/1.3.14 (Unix) PHP/4.0.3pl1 Connection: close Content-Type: text/html; charset=iso-8859-1 Connection closed by foreign host. interzone.ucc.ie>

  16. CGI scripts whose output is not parsed by the HTTP server demon • On student.cs.ucc.ie, CGI scripts whose output is not to be parsed by the server demon (that is, their output is to be passed directly back to the client) are denoted by names which start with nph_ for example nph_prog3.cgi

  17. nph CGI scripts (contd.) • Consider the following nph script: #!/usr/local/bin/perl print <<END; Content-type: text/html <HTML> <HEAD> <TITLE> Short response </TITLE> </HEAD> <BODY> This is a short response produced by nph_prog3.cgi </BODY> </HTML> END • Suppose it’s in a file called nph_prog3.cgi

  18. What happens if we call this ... interzone.ucc.ie> telnet student.cs.ucc.ie 80 Trying 143.239.211.125... Connected to student.cs.ucc.ie. Escape character is '^]'. GET http://student.cs.ucc.ie/cs4400/xyz1/nph-prog3.cgi HTTP/1.1 Host: student.cs.ucc.ie

  19. The response is ... • The response is exactly the text produced by the Perl program, with no status line and no additional headers: Content-type: text/html <HTML> <HEAD> <TITLE> Short response </TITLE> </HEAD> <BODY> This is a short response produced by short.cgi </BODY> </HTML> Connection closed by foreign host. interzone.ucc.ie>

  20. Variables in Perl • The previous Perl scripts were not particularly useful • Useful programs need variables • Perl has four classes of variables: • scalars, • ordinary arrays, and • hash arrays; • objects • Variables need not be declared before they are used • a variable’s class class is recognized from its name • a variable’s base data type is recognized from what is stored in it

  21. Cs 4320 got here on 28 January 2005

  22. Scalar variables • A scalar variable stores a single value. • The names of Perl scalar variables are prefixed with a $ • Examples: $x $y $z $customerName $customerAddress • Example assignment statements: $x = 1 $customerName = ”Eamonn De Valera" $pi = 3.14159 • As can be seen above, a scalar variable can hold an integer, a string or a real

  23. Printing the values of variables • Consider the following program fragment: $x = 1; $customerName = ”Eamonn De Valera"; $pi = 3.14159; print($x); print($customerName); print($pi) • This will produce 1Eamonn De Valera3.14159

  24. Printing white-space characters • As in C, \t denotes a tab and \n denotes a new line • Consider the following modified program fragment: $x = 1; $customerName = ”Eamonn De Valera"; $pi = 3.14159; print($x); print (“\t”); print($customerName); print(“\n”) print($pi) • This will produce 1 Eamonn De Valera 3.14159

  25. Variables in double-quoted strings • Scalar variables can appear in double-quoted strings: $age = 25; $sentence = ”The age of Bob is $age."; • Given the above, the statement print($sentence) would produce the output The age of Bob is 25.

  26. Variables in double-quoted strings (contd.) • We can, of course, mix variables, whitespace characters and other chracters in double-quoted strings • Consider the following modified program fragment: $x = 1; $customerName = ”Eamonn De Valera"; $pi = 3.14159; print(“$x\t$customerName\n$pi Blah blah”) • This will produce 1 Eamonn De Valera 3.14159 Blah blah

  27. Single-quoted strings • The contents of single-quoted strings are not interpreted before being printed • Consider the following modified program fragment: $x = 1; $customerName = ”Eamonn De Valera"; $pi = 3.14159; print(“$x\t$customerName\n$pi Blah blah”); print(“\n”); print (‘$x\t$customerName\n$pi Blah blah’); • This will produce 1 Eamonn De Valera 3.14159 Blah blah $x\t$customerName\n$pi Blah blah

  28. Cs4400 got to here on 23/2/2001

More Related