Random Bits of Perl - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Random Bits of Perl

Description:

None of this stuff is worthy of it's own lecture, but it's all a bunch of things ... end is identifier, signified by '', which are of course optional, but deprecated. ... – PowerPoint PPT presentation

Number of Views:35
Avg rating:3.0/5.0
Slides: 16
Provided by: PaulL155
Category:

less

Transcript and Presenter's Notes

Title: Random Bits of Perl


1
Random Bits of Perl
  • None of this stuff is worthy of its own lecture,
    but its all a bunch of things you should learn
    to use Perl well

2
eval
  • From Camel eval is the way to do all exception
    handling in Perl.
  • Evaluate Perl code
  • make divide-by-zero non-fatal
  • eval answer a / b warn _at_ if _at_
  • trap errors
  • using block notation, if there are any run-time
    errors, eval returns undef and sets error in _at_

3
Back-ticks
  • Third kind of quoted string
  • Executes an external program and returns the
    output
  • _at_files ls .html
  • Your script will run ls .html from the command
    prompt, and store the output of that program in
    _at_files
  • Back-ticks do interpolation just like double
    quotes
  • Note that this is not the same as the system
    function
  • system returns return value of the called
    program, not the output

4
Quoting operators
  • You may get strings that end up looking messy
    because of all the quotes
  • s He said \She said \They said \This is
    bad.\\\
  • can use the quoting operator qq// to choose your
    own quotes.
  • Much like choosing your own delimiters in pattern
    matching
  • s He said qq/She said qq(They said qqThis is
    bad)/
  • You could argue this doesnt look much better
  • You could be right.

5
Many quoting operators
  • In each case below, you can use any
    non-alpha-numeric character for the delimiter,
    just as you can with m// and s///
  • q// - single quoted string
  • qq// - double quoted string
  • does interpolation
  • qx// back-ticked string
  • qw// - quote words
  • qw/Mon Tue Wed/ ? (Mon, Tue, Wed)
  • qr// - quote regular expression
  • evaluate string as though its a RegExp
  • Then use result in an actual pattern match
  • mostly for efficiency

6
map
  • map expression list
  • evaluate expression (or a block) for each value
    of list. Sets _ to each value of list, much
    like a foreach loop
  • Returns a list of all results of the expression
  • _at_words map split _at_lines
  • Set _ to first member of _at_lines, run split
    (split acts on _ if no arg provided), push
    results into _at_words, set _ to next member of
    _at_lines, repeat.

7
grep
  • Similar to map
  • returns a list of all members of the original
    list for which evaluation was true
  • (map returns list of all return values of each
    evaluation)
  • Typically used to pick out lines you want to keep
  • _at_code grep !/\s/, _at_all_lines
  • removes all lines beginning with comments
  • Assigns _ to each member of _at_all_lines, then
    evaluates the pattern match. If pattern match is
    true, the _ is added to _at_code

8
do
  • Similar to C/C do/while construct.
  • Execute entire block following do once. If block
    followed by a while modifier, check the
    conditional, and then redo the loop.
  • Major Difference in Perl, do is not a looping
    structure.
  • Cannot use next, last, or redo.

9
Here Documents (heredocs)
  • My favorite Perl construct..
  • Simple form
  • print ltltEOF
  • print this string
  • EOF
  • ltlt is operator no space between ltltEOF
  • EOF is identifier must be at start of ending line

10
heredocs
  • Less simple, no semi-colon after identifier
  • print ltltNUMBER
  • 1024
  • NUMBER
  • 1024 prints 2048

11
heredocs
  • With backticks around identifier
  • print ltltEOC
  • echo something
  • EOC
  • Will execute command echo and print the results

12
heredocs
  • Double-Quoted identifier same as no quotes,
    single quoted identifier escapes interpolating
  • print ltltEOF
  • do not print this string not interpolated
  • EOF
  • The type of quotes used determines how Perl
    treats text

13
heredocs
  • Lather, rinse and repeat
  • Print ltlt x 10
  • repeat this string ten times!
  • The blank line at end is identifier, signified by
    , which are of course optional, but deprecated.

14
heredoc
  • Sending heredoc string somewhere else besides
    STDOUT
  • print ltltEOF
  • equivalent to
  • print STDOUT ltltEOF
  • So replacing with a file descriptor is obvious
  • print ltltOUTPUT
  • send this output to a file
  • OUTPUT

15
Final heredoc example
  • Heredocs VERY useful in CGI (or javascripting,
    etc)
  • my title this is the title
  • my x_title ltltEOF
  • lttitlegttitlelt/titlegt
  • EOF
  • print x_title
  • Output lttitlegtthis is the titlelt/titlegt
  • Eliminates print lttitlegttitlelt/titlegt\n
Write a Comment
User Comments (0)
About PowerShow.com