Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The Joy of Perl

The Joy of Perl

I gave this talk to the youngsters at work today. They LOL'd.

Simon Whitaker

November 27, 2017
Tweet

More Decks by Simon Whitaker

Other Decks in Programming

Transcript

  1. The basics Perl has three main "types". • $scalar •

    @array • #hash (Plus a couple more, like references and file handles.)
  2. When you expect a scalar value, use $. When you

    expect a list value, use @. When you expect a hash value, use #.
  3. @array = (1, 2, 3); # Indexing into an array

    returns a single element, so use $ $array[0]; # An array slice is a list, so use @ @array[0..1];
  4. Introducing the magic variable ($_) Perl has lots of magic

    variables (see perldoc perlvar), but $_ is the Daddy. Think of $_ as "the thing most likely to be the thing I want to do stuff with right now".
  5. With an explicit loop variable: foreach $elem (@array) { print

    $elem; } With an implicit loop variable foreach (@array) { # foreach uses $_ as default loop variable print $_; }
  6. Args passed in @_ sub greet { ($time_of_day, $name) =

    @_; print "Good $time_of_day, $name"; } greet("morning", "Simon");
  7. Idiomatic Perl: process @_ with shift (shift operates on @_

    by default) sub greet { $time_of_day = shift; $name = shift or die "Insufficient args"; print "Good $time_of_day, $name"; } greet("morning", "Simon");
  8. Simple example, explicit variables $words = "/usr/share/dict/words"; open (FH, $words)

    or die "On opening $words for reading: $!"; while (defined($line = <FH>)) { chomp $line; # strip trailing newline print "$line\n" if length($line) > 15; } close FH;
  9. Same code, implicit use of $_ $words = "/usr/share/dict/words"; open

    (FH, $words) or die "On opening $words for reading: $!"; while (<FH>) { chomp; print "$_\n" if length > 15; } close FH;
  10. Writing to a file handle open (IN, "/usr/share/dict/words") or die;

    open (OUT, "> long-words.txt") or die; while (<IN>) { chomp; print OUT "$_\n" if length > 15; } close IN; close OUT or die "On closing file handle after writing: $!";
  11. Testing file attributes -X FILEHANDLE -X $path Where X is

    one of a few dozen different characters. For example: -e File exists -d File is a directory -z File has zero size (is empty) -t Filehandle is opened to a tty -M Get time since modification of file, in days See perldoc perlfunc for more
  12. Example 1 if (-t STDIN && -t STDOUT) { #

    running in interactive mode } Example 2 if (-M '/path/to/backup' > 3) { # No backup in 3 days! Send an email or something. }
  13. Regular expressions in Perl 1. Match with /PATTERN/ 2. Substitute

    with s/PATTERN/REPLACEMENT/ 3. Invoke with =~
  14. Matching $words = "/usr/share/dict/words"; open (FH, $words) or die "On

    opening $words for reading: $!"; while (defined($line = <FH>)) { print $line if $line =~ /aa/; } close FH;
  15. Same, using $_ $words = "/usr/share/dict/words"; open (FH, $words) or

    die "On opening $words for reading: $!"; while (<FH>) { print if /aa/; } close FH;
  16. $ wc -l /usr/share/dict/words | \ perl -e '$n =

    <STDIN>; print int($n/1000), "K words"'
  17. -n: loop over STDIN $ cat /usr/share/dict/words | \ perl

    -ne 'chomp; print "$_\n" if length > 15'
  18. NB: this works too, of course: $ perl -ne 'chomp;

    print "$_\n" if length > 15' /usr/share/dict/words But I'm using cat as a simple placeholder for "a thing that writes to STDOUT" !
  19. -l: enable automatic line ending processing Implicitly chomps input, adds

    newline to output. $ cat /usr/share/dict/words | perl -nle 'print if length > 15' (-l[octnum] to use character other than \n)
  20. -p: loop over STDIN and print each line $ cat

    /usr/share/dict/words | perl -pe '$_ = uc $_'
  21. BEGIN and END blocks Let's re-implement wc -l: $ cat

    /usr/share/dict/words | perl -ne '$n++; END { print $n }'
  22. Perl creates variables on the fly when you reference them.

    Can be handy in one-liners, but easy to mess up. $value = 1 + 2; print $Value; # prints 0
  23. strict pragma to the rescue use strict; # <-- use

    the strict pragma my $value = 1 + 2; # <-- now vars have to be declared with `my` print $Value; On running this: Global symbol "$Value" requires explicit package name at test.pl line 4. Execution of test.pl aborted due to compilation errors.
  24. warnings pragma also useful use strict; use warnings; my $num

    = '3:' + 4; print $num; # prints 7 On interpreting: Argument "3:" isn't numeric in addition (+) at test.pl line 6.
  25. Where next? • perldoc perl • perldoc perlrun - command-line

    flags • perldoc perlre - regular expressions • perldoc perlvar - for more magic variables