Introduction to Perl - PowerPoint PPT Presentation

1 / 31
About This Presentation
Title:

Introduction to Perl

Description:

use strict; #one time statement. my varname ... The operator is the first such feature. ... it will automatically open the first file on the command line, ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 32
Provided by: PaulL155
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Perl


1
Introduction to Perl
  • Practical Extraction and Report Language
  • Pathologically Eclectic Rubbish Lister

2
How to run Perl
  • Perl is an interpreted language. This means you
    run it through an interpreter, not a compiler.
  • 2 methods
  • Run interpreter directly, giving name of
    perlscript as argument
  • /afs/rpi.edu/home/36/lallip (1) perl myfile.pl
  • Run as a Unix shell program. First line of
    program will tell the shell where the Perl
    interpreter is located
  • This line is called the shebang
  • The shebang MUST be the very first line in the
    code

3
The Shebang
  • Our book says to use the shebang of
  • !/usr/bin/perl
  • On a normal system, this would be correct
  • As you all know, RPI is far from normal
  • At an RCS prompt, type
  • setup permanent perl-new
  • This is a one-time command
  • Now perl v gives version 5.6.1 (5.005_03
    otherwise)
  • Then use the shebang of
  • !/usr/bin/env perl

4
One more step
  • If you choose to use the shebang, you must tell
    the OS that this is an executable file.
  • Use chmod (see intro to unix slides)
  • Usually only need to give yourself execute
    permissions.
  • Once its executable, type the filename at a
    prompt, and it runs.
  • This is the preferred method of running Perl

5
Perl Script Structure
  • semicolon ends each simple statement
  • semicolon optional at final line of a block or
    loop
  • optional, but very recommended
  • Functions variables are case sensitive
  • Comments begin with and extend to end of line
  • dont try to use // or / /
  • Perl will try to figure out what you mean
  • wont even give warnings, unless you tell it to
  • either use warnings or put w after shebang or
    command line
  • this is VERY Recommended. Homeworks will lose
    points if they produce warnings.

6
Variable Declarations
  • In Perl, you do not need to declare your
    variables.
  • Unless you declare that you need to declare them
  • This is entirely up to you. How much do you
    trust yourself not to typo a variable name?
  • To force needed declarations
  • use strict one time statement
  • my ltvarnamegt
  • The my keyword will make more sense when we talk
    about scoping in a few weeks.

7
Variables
  • Three (basic) types of variables.
  • Scalar
  • Array
  • Hash
  • There are others, but well talk about them at a
    later time.

8
Scalars
  • Scalar single value
  • In C/C, many many different kinds of single
    values
  • int, float, double, char, bool
  • In Perl, none of these types need to be declared
  • Scalar variable can hold all these types, and
    more.

9
Scalars
  • All Scalar variables begin with a
  • next character is a letter or _
  • remaining characters letters, numbers, or _
  • Variable names can be between 1 and 251
    characters in length
  • Ex foo, a, zebra1, F87dr_df3
  • Wrong 24da, hibye, barfoo

10
Scalar Assignments
  • Scalars hold any data type
  • foo 3
  • d 4.43
  • temp Z
  • My_String Hello, Im Paul.

11
Lists
  • A list (aka list literal) is a sequence of
    scalar values, separated by commas and enclosed
    in parentheses.
  • A list can hold any number or type of scalars
  • (43, Hello World, 3.1415)
  • Lists provide a way of assigning several scalars
    at once
  • (a, b, c) (42, Foo bar, size)
  • a?42, b?Foo bar, c?size
  • List can also be represented with ranges
  • (a, b, c, d, e) (1..4, 10)
  • (x, y, z) (a .. c)

12
List assignments
  • Both sides of the do not necessarily need to
    have the same number of elements.
  • (a, b, c) (5, 10, 15, 20)
  • a?5, b?10, c?15. (20 ignored)
  • (a, b, c) (5, 10)
  • a?5, b?10, c?undef
  • (t1, t2) (t2, t1)
  • temp t1 t1 t2 t2 temp

13
Arrays
  • Arrays are variables that hold a list
  • (analogous to difference between scalar variable
    and string literal)
  • much more dynamic than C/C
  • no declaration of size, type
  • can hold any kind of value, and multiple kinds of
    values
  • All array variables start with the _at_ character
  • _at_array, _at_foo, _at_My_Array, _at_temp34

14
Array assignments
  • _at_foo (1, 2, 3, 4)
  • _at_bar(my,name,is,Paul)
  • _at_temp (34, z, Hi!, 43.12)
  • Arrays are 0-indexed, just as in C/C
  • let temp1 let is now z
  • NOTE This is a single value, hence the
  • bar2 was
  • _at_bar now ? (my, name, was, Paul)

15
Lists of Arrays
  • Arrays within LHS of list will eat remaining
    values on RHS
  • (foo, _at_bar, baz)(1, 2, 3, 4, 5, 6)
  • foo1 _at_bar(2, 3, 4, 5, 6) bazundef
  • Arrays within RHS flatten to a single array.
  • _at_a1 (1, 2, 3) _at_a2 (4, 5, 6)
  • _at_a3 (_at_a1, _at_a2)
  • _at_a3 ? (1, 2, 3, 4, 5, 6)

16
Array vs. Scalar
  • foo 3
  • _at_foo (43.3, 10, 8, 5.12, a)
  • foo and _at_foo are completely unrelated
  • In fact, foo has nothing to do with foo2
  • This may seem a bit weird, but thats okay,
    because it is weird.
  • Programming Perl, pg. 54

17
More about arrays
  • special variable for each array
  • _at_foo (6, 25, 43, 31)
  • foo ? 3. Last index of _at_foo.
  • foofoo ? 31
  • This can be used to dynamically alter the size of
    an array
  • foo 5
  • creates two undefined values on the end of _at_foo
  • foo 2
  • destroys all but the first three elements of _at_foo

18
Even more about arrays
  • Arrays can take a negative index as well. (since
    0 is first, -1 is last, -2 is second-to-last,
    etc)
  • foofoo and foo-1 always refer to same
    element
  • Slices piece of an array (or list) (or hash)
  • _at_bar _at_foo1..3 _at_bar ?(25, 43, 31)
  • _at_bar _at_foo0,2 _at_bar ?(3, 43)
  • _at_bar _at_foo1 _at_bar ?(25)
  • You probably dont want that

19
Join/Split
  • Built-in Perl functions
  • split split a string into a list of values
  • BigString Hello,_I_am_Paul
  • _at_strings split _, BigString
  • _at_strings ? (Hello,, I, am, Paul)
  • join join a list/array of values together
  • BigString join , _at_strings
  • BigString ? Hello, I am Paul

20
Hash
  • (somewhat) Analogous to datatype hashtable.
  • More closely resembles STL map
  • aka Associative Array ie, array not indexed
    by numerical sequence.
  • list of keys and values.
  • All hash variables start with
  • Use to keep list of corresponding values
  • TIP Any time you feel the need to have two
    separate arrays, and do something with elements
    at corresponding positions in the arrays (but
    dont care where in array elements actually are),
    USE A HASH

21
Hash example
  • Want a list of short names for months
  • months (
  • Jan gt January,
  • Feb gt February,
  • Mar gt March,
  • )
  • reference by curly brackets
  • Avoid confusion with array notation
  • monthJan ? January

22
More Hash Examples
  • Hash elements can be dynamically created (in
    fact, so can entire hashes)
  • profsPerl Paul Lalli
  • profsOp Sys Robert Ingalls
  • profsCS1 David Spooner
  • profs ? (Perl gt Paul Lalli,
  • Op Sys gt Robert Ingalls,
  • CS1 gt David Spooner)
  • Hashes will flatten into normal lists
  • _at_p_arr profs
  • _at_p_arr ?(Perl, Paul Lalli, Op Sys, Robert
    Ingalls, CS1, David Spooner)

23
Special Variables
  • Perl pre-defines some special variables
  • See Chapter 28 of Camel for full list
  • ! last error received by operating system
  • , string used to separate items in a printed
    list
  • string to use to separate items in an
    interpolated array (this makes sense next week)
  • _ - default variable, used by several
    functions
  • ENV Environment variables
  • _at_INC directories Perl looks for include files
  • 0 name of currently running script
  • _at_ARGV command line arguments

24
Very basic I/O
  • simple introduction to reading/writing from
    keyboard/terminal.
  • More advanced (ie, File) I/O will come around
    next week.
  • This will be just enough to allow us to do some
    examples, if necessary.

25
Output to terminal
  • the print statement.
  • Takes a list of arguments to print out
  • Before the list of arguments, optionally specify
    a filehandle to which to print
  • If omitted, default to STDOUT
  • If the list of arguments is omitted, print
    whatever value is currently in variable _

26
Output examples
  • Hello World program
  • !/usr/bin/env perl
  • print Hello World\n
  • as this is Perl, you can put string in
    parentheses, but you dont need to (usually
    because this is Perl).
  • more examples
  • print My name is name\n
  • print Hi , whats , yours?\n
  • print 5 3
  • print ((4 4). \n)

27
One catch
  • Recall that print takes a list of arguments.
  • By default, print outputs that list to the
    terminal one right after another
  • _at_nums (23, 42, 68)
  • print _at_nums, \n
  • 234268
  • To change string printed between list items, set
    the , variable
  • , ,
  • print _at_nums, \n
  • 23, 42, 68

28
Input from keyboard
  • read line operator ltgt
  • aka angle operator, diamond operator
  • Encloses file handle to read from. Defaults to
    STDIN, which is what we want.
  • input ltgt
  • read one line from STDIN, and save in input
  • _at_input ltgt
  • read all lines from STDIN, and save as array in
    _at_input

29
Our First Bit of Magic
  • The Camel will describe several Perl features as
    magical.
  • The ltgt operator is the first such feature.
  • If you pass the name of a file (or files) as
    command line arguments to your script, ltgt does
    not read from STDIN.
  • Instead, it will automatically open the first
    file on the command line, and read from that
    file.
  • When first file exhausted, it opens and reads
    from next file.
  • When all files exhausted, THEN ltgt reads from
    STDIN
  • If you want to read from STDIN before files have
    been read, must do it explicitly
  • line ltSTDINgt

30
Chop Chomp
  • When reading in a line, newline (\n) is
    included.
  • Usually dont want that.
  • chomp will remove the newline from the end of a
    string
  • chop takes off last character of a string,
    regardless of what it is.
  • Hence, chomp is safer.
  • chomp (foo ltgt)
  • Very common method of reading in one string from
    input.
  • chomp actually takes a list, and will chomp each
    element of that list
  • chomp (_at_s (foo\n,bar\n,baz\n))
  • _at_s ? (foo, bar, baz)

31
And were done
  • Thats quite enough for our first lesson.
  • When you go home tonight, I strongly suggest you
    make a few very small sample scripts and try your
    hand at Perl
  • (strongly suggested since next week, you get your
    first homework)
  • Dont forget Fill out the signup form from the
    class webpage by the end of the week!
Write a Comment
User Comments (0)
About PowerShow.com