PERL - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

PERL

Description:

http://www.perl.org/ - Perl institute. http://www.perlmonks.com/ - Perl Monks. Books: ... grep Search through lists. print Output information. perldoc manpage ... – PowerPoint PPT presentation

Number of Views:52
Avg rating:3.0/5.0
Slides: 23
Provided by: buck95
Category:
Tags: perl | grep | monks

less

Transcript and Presenter's Notes

Title: PERL


1
PERL
David Buckley - DavidBuckley_at_bigfoot.com
2
Resources
  • Web
  • http//www.perl.com/ - Perl homepage
  • http//www.perl.org/ - Perl institute
  • http//www.perlmonks.com/ - Perl Monks
  • Books
  • Learning Perl (O'Reilly)
  • - Schwartz
  • Programming Perl (O'Reilly)
  • - Wall, Christiansen, Schwartz
  • Usenet
  • comp.lang.perl.misc
  • comp.lang.perl.modules

3
Basic Perl
  • Similar syntax to Java curly brackets for
    scope etc.
  • Perl is NOT strongly typed casts are
    automatically applied by Perl for you.
  • Perl is NOT compiled. This means running a Perl
    program you just wrote is a one-step process.
  • Perl can look rather like line noise if you
    don't try to make your code look nice.

4
Hello, World!
  • !/usr/bin/perl
  • Give the path to Perl, so the operating
  • system can run the Perl script.
  • use strict Force predeclaration of
  • variables
  • use warnings Turn on basic warnings
  • my a Declare a
  • a "Hello, World!\n"
  • Assign the value
  • "Hello, World\n" to a
  • print a Print the value of a to
  • STDOUT (the screen)

5
Data Types
  • Perl has very few data types
  • Scalar
  • Holds a single value
  • Example a
  • Array
  • Holds a list of values
  • Example _at_a
  • Hash
  • Holds a list of "named" values
  • Example a

6
Scalars
  • A scalar variable holds a "single value". This
    could be anything of
  • A string (e.g. "Hello, World!")
  • An integer (e.g. 4)
  • A floating point number (e.g. 3.1415926)
  • A reference to something (See later!)
  • Perl does not mind too much about the range of
    values. Strings can be as long as you like,
    numbers can take pretty much any value, etc.

7
Arrays
  • Arrays hold a list of scalars. The scalars do not
    have to be the same type. Example
  • my _at_a ("Example", 2)
  • print "a0 a1\n"
  • Will output "Example 2".
  • Perl arrays are dynamic they automatically grow
    as you add more elements
  • a2 "Example"
  • a3 3
  • print "a2 a3\n"

8
Hashes
  • Hashes (also called "associative arrays") hold a
    list of "named" values. The previous example
    might have been written something like
  • my a (
  • Text gt "Example",
  • Value gt 4,
  • )
  • print "aText aValue\n"
  • Hashes are also dynamic we add new elements like
    this
  • aNewElement "Example 5"
  • print "aNewElement\n"

9
Array Functions
  • push _at_array, value1, value2, ...
  • Add values to the end of an array.
  • value pop _at_array
  • Remove values from the end of an array.
  • value join(string, _at_array)
  • Join all the values of an array together.
  • "_at_array" eq join(" ", _at_array)

10
Lists
  • Similar to arrays.
  • Can be used to assign to many variables
  • my (a, b, c) ("Something", 1, 2)
  • All functions simply take a list.
  • Arrays can act like lists
  • push _at_array1, _at_array2 Concatenate _at_array2
  • onto _at_array1
  • (a, b) _at_array3

11
Comparisons
  • Because there's no distinction between a string
    and an integer, Perl has two sets of comparison
    operators
  • Strings eq ne ge gt le lt cmp
  • Integers ! gt gt lt lt ltgt
  • Note that
  • 0.0 ne 0 Same as "0.0" ne "0"
  • "foobar" "notfoobar" Same as 0 0

12
Flow Control
  • First, conditionals
  • if (a 1)
  • print "\a is 1\n"
  • else
  • print "\a is not 1\n"
  • unless (a 2)
  • print "\a is not 2\n"

13
Loops - While
  • Perl has a while that works just like the one in
    most other languages
  • while (a lt 5)
  • a a 1
  • print "a\n"
  • Equivalently
  • until (a 5)
  • a a 1
  • print "a\n"

14
Loops For
  • Perl has a for statement that works just like for
    in a lot of other languages
  • for (a 0 a lt 5 a)
  • print "a\n"

15
Loops Foreach
  • Perl also has an additional constuct, called
    foreach. It loops over each element of a list
  • foreach a (0 .. 4)
  • print "a\n"
  • If you omit the loop variable (a), _ is
    automatically used
  • foreach (_at_array)
  • print "_\n"

16
Short Syntax
  • Shorthand for simple loops.
  • Loop goes after statement.
  • Examples
  • print "\a is not 1\n" unless a 1
  • print "\a is \"foo\"\n" if a eq "foo"
  • print "a\n" while a lt 5
  • print "_\n" foreach (_at_array)

17
References
  • Damn useful.
  • Like C pointers and Java references.
  • References are scalar values.
  • \ symbol
  • You can make a reference to anything
  • ref1 \hash hash ref1
  • ref2 \_at_array _at_array _at_ref2
  • ref3 \scalar scalar ref3
  • A core part of object orientation and data
    structures in Perl.

18
Data Structures
  • Implemented using references.
  • Can use _at_ etc to dereference, or var-gt.
  • Use list and key gt value,key gt value
    to create "anonymous" arrayrefs and hashrefs.
  • Example
  • my a
  • Key gt "This is" ,
  • 4,
  • "Example", 6 ,
  • print "a-gt0Key _at_a-gt2\n"

19
Subroutines
  • Simple to use.
  • Parameters passed as an array called _at__.
  • sub doStuff
  • print "I was passed\n"
  • print "\t_\n" foreach(_at__)
  • doStuff("Parameter 1", "Another Parameter")
  • Parameters are lvalues
  • sub addOne
  • _0 1 return _0
  • a 0
  • print addOne(a) print a
  • print addOne(0) error!

20
A more complex example !/usr/bin/perl use
warnings use strict use DataDumper Include
data structure dumper my players Bucko
gt RealName gt "David Buckley",
Score gt 20, , DeathKiller gt
RealName gt "Fred Bloggs", Score gt
100000, , my _at_inf grep _-gtRealName
/David/ values players print
DataDumperDumper( \_at_inf )
21
Perl -e
  • Immensely useful for testing things.
  • Examples
  • perl -e 'print "_\n" foreach (0 .. 4)'
  • perl -e 'print chr(_)."\n" foreach(0..255)'
  • perl -e 'sub a print "Hello, World" a()'
  • Also -n
  • ls perl -ne 'print if /a/'
  • And -p
  • ls perl -pe 's/foo/bar/'

22
Manual Pages
  • perldoc ltmodulegt
  • DataDumper Dumps of data structures
  • perldoc -f ltfunctiongt
  • grep Search through lists
  • print Output information
  • perldoc ltmanpagegt
  • perlre Regular expressions
  • perldsc Data structures
  • perlreftut References
Write a Comment
User Comments (0)
About PowerShow.com