Llama to Ram - PowerPoint PPT Presentation

1 / 48
About This Presentation
Title:

Llama to Ram

Description:

O'Reilly Bestiary. 3/18/99. Llama to Ram -- Rob Napier. 5. Quotes ... O'Reilly Bestiary. Learning - Llama. Programming - Camel. Cookbook - Ram ... – PowerPoint PPT presentation

Number of Views:45
Avg rating:3.0/5.0
Slides: 49
Provided by: roberta120
Learn more at: http://www.employees.org
Category:
Tags: bestiary | llama | ram

less

Transcript and Presenter's Notes

Title: Llama to Ram


1
Llama to Ram
  • How to think like a perl weenie
  • Rob Napier
  • 3/18/99

2
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

3
Topics of Discussion
  • Background and philosophy
  • Perl basics
  • Tools of the trade
  • Pitfalls
  • Perl rules
  • Gotchas

4
Background and Philosophy
  • Quotes
  • Influences
  • OReilly Bestiary

5
Quotes
  • Practical Extraction and Report Language
  • Perl is a language for getting things done.
  • Theres more than one way to do it

6
Major Influences
  • C
  • sh
  • regex
  • unix
  • LISP and COBOL

7
OReilly Bestiary
  • Learning - Llama
  • Programming - Camel
  • Cookbook - Ram
  • Advanced - Leopard/Panther
  • Also Perl/Tk, Nutshell, Win32, and others

8
Perl Basics
  • Auto-conversion (coercion)
  • The search for Truth
  • Safety nets
  • Data types

9
Auto-conversion (coercion)
  • Strings ltgt Numbers
  • References gt Strings
  • undef gt Strings
  • Scalars gt Lists
  • Lists gt Scalar
  • The dreaded 1s

10
The Search for Truth
  • False , 0
  • False 0, undef, ()
  • True 1, ref, 0 but true, and most anything
    else

11
Safety Nets
  • -w
  • use strict

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

13
Scalars
  • Numbers
  • Strings
  • References
  • undef
  • Typeglob
  • Filehandle
  • foo bar

14
Lists
  • Heterogeneous
  • Both list-like and array-like, but usually
    list-like
  • _at_foo qw(bar baz bang)
  • bing _at_foo4
  • _at_bang _at_foo4, 6

15
Hashes
  • Associate arrays
  • keys, values, each, delete, exists
  • foo (apple gt red, orange gt orange, foo
    gt 2)
  • fooapple orange
  • _at_fooapple, orange

16
Filehandles
  • open (FOO, foo)
  • while (ltFOOgt)
  • while (ltgt)
  • Includes predefined STDOUT, STDIN, STDERR

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

18
References
  • Hard
  • Symbolic

19
Hard References
  • Similar to C-style pointers.
  • scalarref \foo
  • arrayref \_at_ARGV
  • hashref \ENV
  • coderef \handler
  • globref \foo
  • scalarref \1
  • arrayref 1, 2, a, b, c
  • hashref Adam gt Eve, Clyde gt Bonnie
  • coderef sub print Boink!\n

20
Symbolic References
  • Indirect references to variable
  • These can be very dangerous (and arent allowed
    under use strict)
  • scalarref
  • _at_arrayref
  • hashref

21
Tools of the trade
  • Lists
  • Hashes
  • Regex
  • Subs
  • Modules

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

23
Lists, seldom arrays
  • Usually use foreach, rather than subscripting
    into arrays. Instead of
  • for (i 0 i lt list i)
  • do_something(listi)
  • Do this
  • foreach elem (_at_list)
  • do_something(elem)

24
qw()
  • Very good way to set lists of quoted words
  • _at_foo qw(this is a test)

25
Sets
  • _at_isect _at_diff _at_union ()
  • foreach e (_at_a, _at_b) counte
  • foreach e (keys count)
  • push(_at_union, e)
  • push _at_ counte 2 ? \_at_isect \_at_diff ,
    e

26
Slurping
  • Often its handy to just slurp a whole file and
    work on it in memory
  • open(FOO, foo)
  • _at_foo ltFOOgt

27
Hashes
  • Swiss-army knife of perl
  • Associative arrays
  • Records
  • In list applications

28
Associate Arrays
  • foo (apple gt red,
  • orange gt orange)
  • print fooapple

29
Records
  • The hard way
  • _at_entry getpwuid(lt)
  • user (name gt entry0,
  • passwd gt entry1,
  • uid gt entry2,
  • gid gt entry3,
  • quota gt entry4,
  • comment gt entry5,
  • ,
  • expire gt entry9)
  • print name is username\n

30
Records cont
  • The easy way (i.e. the perl way)
  • _at_fields qw(name passwd uid gid quota comment
    gcos dir shell expire)
  • _at_user_at_fields getpwuid lt
  • print name is username\n

31
In list applications
  • Maintaining list order
  • sub unique
  • my (_at_list) (_at__)
  • my seen () Hash to keep track of what
    we've seen
  • my item Current item
  • my _at_uniq Unique list
  • foreach item (_at_list)
  • push (_at_uniq, item) unless seenitem
  • return _at_uniq

32
In list applications cont
  • Trashing list order
  • sub unique
  • my (_at_list) (_at__)
  • my uniq
  • _at_uniq_at_list ()
  • return keys _at_uniq

33
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

34
Subs
  • Passing non-scalars
  • Returning non-scalars
  • Named parameters

35
Passing non-scalars
  • Try to move non-scalar to the end
  • If you cant, pass a reference
  • sub foo
  • my _at_a _at_shift()
  • my _at_b _at_shift()
  • print _at_a\n
  • print _at_b\n
  • foo(\_at_bar, \_at_baz)

36
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 _at_a qw(this is a test)
  • my _at_b qw(this is a test)
  • return (\_at_a, \_at_b)

37
Named parameters
  • sub thefunc
  • my args (
  • INCREMENT gt '10s',
  • FINISH gt 0,
  • START gt 0,
  • _at__, argument pair list goes
    here
  • )
  • if (argsINCREMENT /m/ ) .....
  • thefunc(INCREMENT gt "20s", START gt "5m",
    FINISH gt "30m")

38
Modules
  • FilePath
  • FileFind
  • FileCopy
  • Exporter
  • getop
  • sendmail
  • CGI
  • Cwd

39
perl4 pitfalls
  • use strict! (and debatably also use -w)
  • local gt my
  • chop gt chomp
  • require gt use
  • Avoid globals
  • Avoid typeglobs
  • Investigate complex data structures

40
sh pitfalls
  • use strict!
  • ltlt instead of multiple prints
  • 0false, 1true except on system calls
  • Dont 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

41
sh pitfalls (cont)
  • Dont store lists as strings
  • Avoid temp files
  • Avoid excessive chdir()

42
C pitfalls
  • Avoid subscripting lists that youre iterating
    over
  • printf -gt print
  • Dont fear labels, especially for using last
    and next
  • Dont try to split a string character by
    character. Use regex.
  • Dont overlook POSIX

43
General perl pitfalls
  • Generally you dont need to add .pl onto script
    names.
  • Often readdir() is a better tool than glob()
  • tr/a-z/A-Z/ -gt uc()

44
Perl rules
  • Always return()
  • Always check your system return codes and close()
    returns
  • use strict
  • gt 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)

45
Gotchas
  • BEGIN use strict
  • There is no real function prototype mechanism
    (perl prototypes arent what you think)
  • ltlt EOF
  • Watch out for spaces before the EOF
  • ! vs ne, vs eq, vs .
  • number vs. string

46
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 _at_foo vs print _at_foo
  • _at_foo adds spaces. This includes inside a
    HERE-docs

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

48
Wrapup
  • Thinking like a perl weenie means working with
    the language instead of against it. Even though
    theres 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.
Write a Comment
User Comments (0)
About PowerShow.com