llama to ram l.
Download
Skip this Video
Download Presentation
Llama to Ram

Loading in 2 Seconds...

play fullscreen
1 / 48

Llama to Ram - PowerPoint PPT Presentation


  • 275 Views
  • Uploaded on

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).

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

PowerPoint Slideshow about 'Llama to Ram' - arleen


Download Now 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

Llama to Ram

How to think like a perl weenie

Rob Napier

3/18/99

introduction
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
Topics of Discussion
  • Background and philosophy
  • Perl basics
  • Tools of the trade
  • Pitfalls
  • Perl rules
  • Gotchas

Llama to Ram -- Rob Napier

background and philosophy
Background and Philosophy
  • Quotes
  • Influences
  • O’Reilly Bestiary

Llama to Ram -- Rob Napier

quotes
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
Major Influences
  • C
  • sh
  • regex
  • unix
  • LISP and COBOL

Llama to Ram -- Rob Napier

o reilly bestiary
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
Perl Basics
  • Auto-conversion (coercion)
  • The search for Truth
  • Safety nets
  • Data types

Llama to Ram -- Rob Napier

auto conversion coercion
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
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
Safety Nets
  • -w
  • use strict

Llama to Ram -- Rob Napier

data types
Data types
  • Scalars
  • Lists
  • Hashes
  • Filehandles
  • References

Llama to Ram -- Rob Napier

scalars
Scalars
  • Numbers
  • Strings
  • References
  • undef
  • Typeglob
  • Filehandle

$foo = “bar”;

Llama to Ram -- Rob Napier

lists
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
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
Filehandles
  • open (FOO, “foo”);
  • while (<FOO>) {
  • while (<>) {
  • Includes predefined STDOUT, STDIN, STDERR

Llama to Ram -- Rob Napier

typeglobs
Typeglobs
  • Entries in the symbols table
  • Not used very often except for references to filehandles

Llama to Ram -- Rob Napier

references
References
  • Hard
  • Symbolic

Llama to Ram -- Rob Napier

hard references
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
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
Tools of the trade
  • Lists
  • Hashes
  • Regex
  • Subs
  • Modules

Llama to Ram -- Rob Napier

lists22
Lists
  • Usually used as lists, instead of arrays
  • qw()
  • Sets
  • Slurping

Llama to Ram -- Rob Napier

lists seldom arrays
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
qw()
  • Very good way to set lists of quoted words:

@foo = qw(this is a test);

Llama to Ram -- Rob Napier

slide25
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
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
Hashes
  • Swiss-army knife of perl
    • Associative arrays
    • Records
    • “In list” applications

Llama to Ram -- Rob Napier

associate arrays
Associate Arrays

%foo = (apple => “red”,

orange => “orange”);

print $foo{apple};

Llama to Ram -- Rob Napier

records
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
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
“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
“In list” applications cont
  • Trashing list order

sub unique {

my (@list) = (@_)

my %uniq;

@uniq{@list} = ();

return keys @uniq;

}

Llama to Ram -- Rob Napier

regex
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
Subs
  • Passing non-scalars
  • Returning non-scalars
  • Named parameters

Llama to Ram -- Rob Napier

passing non scalars
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
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
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
Modules
  • File::Path
  • File::Find
  • File::Copy
  • Exporter
  • getop
  • sendmail
  • CGI
  • Cwd

Llama to Ram -- Rob Napier

perl4 pitfalls
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
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
sh pitfalls (cont)
  • Don’t store lists as strings
  • Avoid temp files
  • Avoid excessive chdir()

Llama to Ram -- Rob Napier

c pitfalls
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
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
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
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
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
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
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