Llama to Ram - PowerPoint PPT Presentation

Llama to ram l.jpg
Download
1 / 48

  • 241 Views
  • Uploaded on
  • Presentation posted in: Pets / Animals

Llama to Ram. How to think like a perl weenie Rob Napier 3/18/99. Introduction. In this talk we will move from the basics of perl syntax and grammar (Llama) to the philosophy behind perl and the tools of the trade (Ram).

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

Download Presentation

Llama to Ram

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Llama to ram l.jpg

Llama to Ram

How to think like a perl weenie

Rob Napier

3/18/99


Introduction l.jpg

Introduction

  • In this talk we will move from the basics of perl syntax and grammar (Llama) to the philosophy behind perl and the tools of the trade (Ram).

  • This talk will not cover many advanced perl topics, and in particular won't cover performance issues or advanced data structures

Llama to Ram -- Rob Napier


Topics of discussion l.jpg

Topics of Discussion

  • Background and philosophy

  • Perl basics

  • Tools of the trade

  • Pitfalls

  • Perl rules

  • Gotchas

Llama to Ram -- Rob Napier


Background and philosophy l.jpg

Background and Philosophy

  • Quotes

  • Influences

  • O’Reilly Bestiary

Llama to Ram -- Rob Napier


Quotes l.jpg

Quotes

  • Practical Extraction and Report Language

  • Perl is a language for getting things done.

  • There’s more than one way to do it

Llama to Ram -- Rob Napier


Major influences l.jpg

Major Influences

  • C

  • sh

  • regex

  • unix

  • LISP and COBOL

Llama to Ram -- Rob Napier


O reilly bestiary l.jpg

O’Reilly Bestiary

  • Learning - Llama

  • Programming - Camel

  • Cookbook - Ram

  • Advanced - Leopard/Panther

  • Also Perl/Tk, Nutshell, Win32, and others

Llama to Ram -- Rob Napier


Perl basics l.jpg

Perl Basics

  • Auto-conversion (coercion)

  • The search for Truth

  • Safety nets

  • Data types

Llama to Ram -- Rob Napier


Auto conversion coercion l.jpg

Auto-conversion (coercion)

  • Strings <=> Numbers

  • References => Strings

  • undef => Strings

  • Scalars => Lists

  • Lists => Scalar

    • The dreaded 1’s

Llama to Ram -- Rob Napier


The search for truth l.jpg

The Search for Truth

  • False: “”, “0”

    • False = 0, undef, ()

    • True = 1, ref, “0 but true”, and most anything else

Llama to Ram -- Rob Napier


Safety nets l.jpg

Safety Nets

  • -w

  • use strict

Llama to Ram -- Rob Napier


Data types l.jpg

Data types

  • Scalars

  • Lists

  • Hashes

  • Filehandles

  • References

Llama to Ram -- Rob Napier


Scalars l.jpg

Scalars

  • Numbers

  • Strings

  • References

  • undef

  • Typeglob

  • Filehandle

    $foo = “bar”;

Llama to Ram -- Rob Napier


Lists l.jpg

Lists

  • Heterogeneous

  • Both list-like and array-like, but usually list-like

    @foo = qw(bar baz bang);

    $bing = @foo[4];

    @bang = @foo[4, 6];

Llama to Ram -- Rob Napier


Hashes l.jpg

Hashes

  • Associate arrays

  • keys, values, each, delete, exists

    %foo = (apple => “red”, orange => “orange”, foo => 2);

    $foo{apple} = “orange”;

    @foo{apple, orange};

Llama to Ram -- Rob Napier


Filehandles l.jpg

Filehandles

  • open (FOO, “foo”);

  • while (<FOO>) {

  • while (<>) {

  • Includes predefined STDOUT, STDIN, STDERR

Llama to Ram -- Rob Napier


Typeglobs l.jpg

Typeglobs

  • Entries in the symbols table

  • Not used very often except for references to filehandles

Llama to Ram -- Rob Napier


References l.jpg

References

  • Hard

  • Symbolic

Llama to Ram -- Rob Napier


Hard references l.jpg

Hard References

  • Similar to C-style pointers.

    $scalarref = \$foo;

    $arrayref = \@ARGV;

    $hashref = \%ENV;

    $coderef = \&handler;

    $globref = \*foo;

    $scalarref = \1;

    $arrayref = [1, 2, [‘a’, ‘b’, ‘c’]];

    $hashref = {‘Adam’ => ‘Eve’, ‘Clyde’ => Bonnie’ };

    $coderef = sub {print “Boink!\n” };

Llama to Ram -- Rob Napier


Symbolic references l.jpg

Symbolic References

  • Indirect references to variable

  • These can be very dangerous (and aren’t allowed under ‘use strict’)

  • $$scalarref;

  • @$arrayref;

  • %$hashref

Llama to Ram -- Rob Napier


Tools of the trade l.jpg

Tools of the trade

  • Lists

  • Hashes

  • Regex

  • Subs

  • Modules

Llama to Ram -- Rob Napier


Lists22 l.jpg

Lists

  • Usually used as lists, instead of arrays

  • qw()

  • Sets

  • Slurping

Llama to Ram -- Rob Napier


Lists seldom arrays l.jpg

Lists, seldom arrays

  • Usually use foreach, rather than subscripting into arrays. Instead of:

    for ($i =0; $i <= $#list; $i++) {

    do_something($list[$i]);

    }

    Do this:

    foreach $elem (@list) {

    do_something($_);

    }

Llama to Ram -- Rob Napier


Slide24 l.jpg

qw()

  • Very good way to set lists of quoted words:

    @foo = qw(this is a test);

Llama to Ram -- Rob Napier


Slide25 l.jpg

Sets

@isect = @diff = @union = ();

foreach $e (@a, @b) { $count{$e}++ }

foreach $e (keys %count) {

push(@union, $e);

push @{ $count{$e} == 2 ? \@isect : \@diff }, $e;

}

Llama to Ram -- Rob Napier


Slurping l.jpg

Slurping

  • Often it’s handy to just slurp a whole file and work on it in memory:

    open(FOO, “foo”);

    @foo = <FOO>;

Llama to Ram -- Rob Napier


Hashes27 l.jpg

Hashes

  • Swiss-army knife of perl

    • Associative arrays

    • Records

    • “In list” applications

Llama to Ram -- Rob Napier


Associate arrays l.jpg

Associate Arrays

%foo = (apple => “red”,

orange => “orange”);

print $foo{apple};

Llama to Ram -- Rob Napier


Records l.jpg

Records

  • The hard way

    @entry = getpwuid($<);

    %user = (name => $entry[0],

    passwd => $entry[1],

    uid => $entry[2],

    gid => $entry[3],

    quota => $entry[4],

    comment => $entry[5],

    […],

    expire => $entry[9]);

    print “name is $user{name}\n”;

Llama to Ram -- Rob Napier


Records cont l.jpg

Records cont

  • The easy way (i.e. the perl way)

    @fields = qw(name passwd uid gid quota comment gcos dir shell expire);

    @user{@fields} = getpwuid $<;

    print “name is $user{name}\n”;

Llama to Ram -- Rob Napier


In list applications l.jpg

“In list” applications

  • Maintaining list order

    sub unique {

    my (@list) = (@_);

    my %seen = (); # Hash to keep track of what we've seen

    my $item; # Current item

    my @uniq; # Unique list

    foreach $item (@list) {

    push (@uniq, $item) unless $seen{$item}++;

    }

    return @uniq;

    }

Llama to Ram -- Rob Napier


In list applications cont l.jpg

“In list” applications cont

  • Trashing list order

    sub unique {

    my (@list) = (@_)

    my %uniq;

    @uniq{@list} = ();

    return keys @uniq;

    }

Llama to Ram -- Rob Napier


Regex l.jpg

Regex

  • Very useful for getting a lot of things done fast.

    Rob Napier: 9408 Erinsbrook Drive, Raleigh, NC 27613 (919)848-9523

    /(.*):\s*([^,]*),\s*([^,]*),\s*(\w+)\s+(\d+)\s+(?=\()(.*)/

    $name = $1;

    $address = $2;

    $city = $3;

    $state = $4;

    $zip = $5;

    $phone = $6;

Llama to Ram -- Rob Napier


Slide34 l.jpg

Subs

  • Passing non-scalars

  • Returning non-scalars

  • Named parameters

Llama to Ram -- Rob Napier


Passing non scalars l.jpg

Passing non-scalars

  • Try to move non-scalar to the end

  • If you can’t, pass a reference

    sub foo {

    my @a = @{shift()};

    my @b = @{shift()};

    print “@a\n”;

    print “@b\n”;

    }

    foo(\@bar, \@baz);

Llama to Ram -- Rob Napier


Returning non scalars l.jpg

Returning non-scalars

  • If you can return it as a flat list (or hash), then just return it.

  • If you have multiple, distinct return values, return a list of references

    sub foo {

    my @a = qw(this is a test);

    my @b = qw(this is a test);

    return (\@a, \@b);

    }

Llama to Ram -- Rob Napier


Named parameters l.jpg

Named parameters

sub thefunc {

my %args = (

INCREMENT => '10s',

FINISH => 0,

START => 0,

@_, # argument pair list goes here

);

if ($args{INCREMENT} =~ /m$/ ) { ..... }

}

thefunc(INCREMENT => "20s", START => "+5m", FINISH => "+30m");

Llama to Ram -- Rob Napier


Modules l.jpg

Modules

  • File::Path

  • File::Find

  • File::Copy

  • Exporter

  • getop

  • sendmail

  • CGI

  • Cwd

Llama to Ram -- Rob Napier


Perl4 pitfalls l.jpg

perl4 pitfalls

  • use strict! (and debatably also use -w)

  • local => my

  • chop => chomp

  • require => use

  • Avoid globals

  • Avoid typeglobs

  • Investigate complex data structures

Llama to Ram -- Rob Napier


Sh pitfalls l.jpg

sh pitfalls

  • use strict!

  • << instead of multiple prints

  • 0=false, 1=true except on system calls

  • Don’t over-fork. Most of what you want is in perl:

    rm/rm -rf, find, ls, echo, grep, awk, sed, pwd, mkdir, mkdir -p, chown, chgrp, cp, ln

  • Avoid globals

Llama to Ram -- Rob Napier


Sh pitfalls cont l.jpg

sh pitfalls (cont)

  • Don’t store lists as strings

  • Avoid temp files

  • Avoid excessive chdir()

Llama to Ram -- Rob Napier


C pitfalls l.jpg

C pitfalls

  • Avoid subscripting lists that you’re iterating over

  • printf -> print

  • Don’t fear labels, especially for using ‘last’ and ‘next’

  • Don’t try to split a string character by character. Use regex.

  • Don’t overlook POSIX

Llama to Ram -- Rob Napier


General perl pitfalls l.jpg

General perl pitfalls

  • Generally you don’t need to add .pl onto script names.

  • Often readdir() is a better tool than glob()

  • tr/a-z/A-Z/ -> uc()

Llama to Ram -- Rob Napier


Perl rules l.jpg

Perl rules

  • Always return()

  • Always check your system return codes and close() returns

  • use strict

  • $# => scalar (or scalar context)

    • Some people may debate this one, but I find it helps a lot.

  • Before writing anything complex, always check CPAN (www.cpan.com)

Llama to Ram -- Rob Napier


Gotchas l.jpg

Gotchas

  • BEGIN { use strict; }

  • There is no real function prototype mechanism (perl prototypes aren’t what you think)

  • << EOF

    • Watch out for spaces before the EOF

  • != vs ne, == vs eq, + vs .

    • number vs. string

Llama to Ram -- Rob Napier


Gotchas cont l.jpg

Gotchas cont

  • &&, ||, and, or

    • && and || bind tightly. “and” and “or” bind loosely

    • Generally, you use && and || in boolean logic, while “and” and “or” are used for “or die” type error checking.

  • print “@foo” vs print @foo

    • “@foo” adds spaces. This includes inside a HERE-docs

Llama to Ram -- Rob Napier


Gotchas cont47 l.jpg

Gotchas cont

  • `foo` or warn

    • This only warns if `foo` returns no output (even if that output is an error)

  • split (‘ ‘, ...)

    • Splits on whitespace /[ \n\t]+/, not just ‘ ‘.

Llama to Ram -- Rob Napier


Wrapup l.jpg

Wrapup

  • Thinking like a perl weenie means working with the language instead of against it. Even though “there’s more than one way to do it,” many of those ways fail to make good use of the power that perl offers.

  • The best way to learn to think in perl is to keep trying to make working scripts more perl-like. The best solution is usually the shortest solution that is still readable.

Llama to Ram -- Rob Napier


  • Login