Perl: Lecture 3 - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

Perl: Lecture 3

Description:

ACTION='/cgi-bin/upload.pl' METHOD='POST' Please select a file to ... grep EXPR, LIST. returns list elements for which the EXPR evaluated as true. sort LIST ... – PowerPoint PPT presentation

Number of Views:35
Avg rating:3.0/5.0
Slides: 24
Provided by: trA56
Category:
Tags: grep | lecture | perl

less

Transcript and Presenter's Notes

Title: Perl: Lecture 3


1
Perl Lecture 3
  • Organization Modern Perl

2
File Upload using CGI.pm
  • ltFORM ENCTYPE"multipart/form-data"
  • ACTION"/cgi-bin/upload.pl" METHOD"POST"gt
  • Please select a file to upload ltBRgt
  • ltINPUT TYPE"FILE" NAME"file"gt ltpgt
  • ltINPUT TYPE"submit"gt
  • lt/FORMgt

3
File Upload using CGI.pm (2)
  • use CGI
  • file param('file')
  • filem/.(\\\/)(.)/ strip the remote path
    and keep the filename
  • name 2
  • open(LOCAL, "gtname")
  • while(ltfilegt)
  • print LOCAL _
  • print header()
  • print "file has been successfully uploaded...
    thank you.\n"

4
CPAN
  • Comprehensive Perl Archive Network
  • http//search.cpan.org
  • Contains libraries for just about anything
  • Installing libraries
  • CPAN module (Perl general)
  • PPM (Windows, Activestate)

5
PPM
  • Perl Package Manager
  • Command PPM
  • Interactive shell
  • install TimeHiRes
  • quit
  • perldoc TimeHiRes

6
Example Module MailPOP3Client
  • Gives functionality of a POP3 client to a script
  • new (USERgtuser, PASSWORDgtpassword,
    HOSTgthost)
  • New connection
  • head(message_number)
  • body(message_number)
  • delete(message_number)
  • Count
  • Get number of messages
  • ListArray
  • Get list of sizes of messages

7
Invoking other processes
  • TMTOWTDI
  • exec
  • system
  • open
  • backtick

exec dir
system dir
open DIR, dir while (ltDIRgt) print
_at_entries dir print _at_entries
8
Security Taint mode
  • Problem Data input by user used to build command
    line
  • perl -T command line switch
  • External data is marked as tainted, unsafe
    operations are impossible
  • Laundering data by e.g regular expressions

if (data /(\w)/) data1 else
die
9
Array Processing (1)
  • push ARRAY, LIST
  • Treats ARRAY as a stack, and pushes the values of
    LIST onto the end of ARRAY
  • pop ARRAY
  • Pops and returns the last value of the array,
    shortening the array by one element

new
new
10
Array Processing (2)
  • unshift ARRAY, LIST
  • Prepends list to the front of the array.
  • shift ARRAY
  • Shifts the first value of the array off and
    returns it, shortening the array by 1

new
new
11
Array Processing (3)
  • splice ARRAY, OFFSET ,LENGTH ,LIST
  • removes and returns LENGTH items from ARRAY
    starting at OFFSET and replaces them with LIST
  • grep EXPR, LIST
  • returns list elements for which the EXPR
    evaluated as true
  • sort LIST
  • sorts and returns LIST

12
Hash functions
  • delete EXPR
  • deletes an element of the hash
  • each HASH
  • Return next ordered PAIR of key, value or key
    only if in scalar context
  • exists EXPR
  • True if hash has an element with key EXPR
  • keys HASH
  • Return list of keys of a hash
  • Values HASH
  • Returns list of values in hash

13
References
  • Variable holding address of other variable
  • Target may be scalar, hash, array ...
  • Automatic memory management
  • No malloc / free
  • Automatic garbage collection

14
References (1)
  • \ operator
  • Analogue to Cs operator
  • scalarref \scalar
  • arrayref \_at_ARGV
  • Anonymous references
  • arrayref 1,2, a, b, c
  • hashref A gt 1, B gt 2

15
References (2)
  • dereferencing operator
  • scalar text
  • scalarref \scalar
  • scalar2 scalarref
  • print _at_arrayref
  • arrayref0 January

16
Reference (3)
  • -gt dereference operator
  • arrayref0 January
  • arrayref-gt0 January
  • hashrefkey February
  • hashref-gtkey February

17
Manipulating Lists of Lists
  • _at_twodim ( a1, a2 ,
  • b1, b2, b3 ,
  • c1, c2, c3 )
  • print twodim22
  • ref_to_twodim a1, a2 ,
  • b1, b2, b3 ,
  • c1, c2, c3

18
References Overview Table
  • Setting Using
  • (reference) (dereference)
  • Scalar r \s r
  • Array ra \_at_a _at_ra, ra2
  • Hash rh \h rh,
  • rhkey

19
Nested Data Structures
  • h1 ( "user" gt "u1", "password" gt "p1" )
  • h2 ( "user" gt "u2", "password" gt "p2" )
  • _at_datastr ( \h1, "a1", "b1" ,
  • \h2, "a2", "b2" )
  • foreach entry (_at_datastr)
  • print entry0user . "" .
    entry0password . "\n"
  • print entry1 . "\n" . entry2 . "\n"

20
Packages / Perl Modules (1)
  • A package is a namespace
  • Package usually one file
  • Packagename.pm
  • To use a module
  • use module
  • Import functions and variables

21
Packages / Perl Modules (2)
  • Always inside a package, default main
  • package directive
  • Current block gets a new namespace
  • Specifying namespaces for identifiers -
  • PackageVariable
  • BEGIN / END subroutines

22
Perl Plain Old Documentation
  • Paragraph styles
  • Verbatim
  • Indented
  • Command
  • head1, head2 Headings
  • over, back, item List
  • cut End of POD
  • Ordinary

23
Finish
Write a Comment
User Comments (0)
About PowerShow.com