Introduction to Perl

Document Sample
Introduction to Perl Powered By Docstoc
					        Introduction to Perl

“Practical Extraction and Report Language”
  “Pathologically Eclectic Rubbish Lister”
                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
                 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
           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 it‟s executable, type the filename at a
  prompt, and it runs.
• This is the preferred method of running Perl
           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
  – don‟t try to use // or /* … */
• Perl will try to figure out what you mean
  – won‟t 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.
            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 <varname>;
• The my keyword will make more sense when
  we talk about scoping in a few weeks.
                Variables
• Three (basic) types of variables.
  – Scalar
  – Array
  – Hash
• There are others, but we‟ll talk about them
  at a later time….
                  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.
                  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, $hi&bye, $bar$foo
      Scalar Assignments
• Scalars hold any data type:
• $foo = 3;
• $d = 4.43;
• $temp = „Z‟;
• $My_String = “Hello, I‟m Paul.”
                        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”);
            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;
                  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 @
  character
  – @array, @foo, @My_Array, @temp34
             Array assignments
• @foo = (1, 2, 3, 4);
• @bar=(“my”,“name”,“is”,“Paul”);
• @temp = (34, „z‟, “Hi!”, 43.12);
• Arrays are 0-indexed, just as in C/C++
• $let = $temp[1]; # $let is now „z‟
   – NOTE: This is a *single value*, hence the $
• $bar[2] = “was”;
   – @bar now  (“my”, “name”, “was”, “Paul”);
                Lists of Arrays
• Arrays within LHS of list will „eat‟ remaining
  values on RHS:
  – ($foo, @bar, $baz)=(1, 2, 3, 4, 5, 6);
  – $foo=1; @bar=(2, 3, 4, 5, 6);
    $baz=undef;
• Arrays within RHS „flatten‟ to a single array.
  – @a1 = (1, 2, 3); @a2 = (4, 5, 6);
  – @a3 = (@a1, @a2);
  – @a3  (1, 2, 3, 4, 5, 6)
            Array vs. Scalar
• $foo = 3;
• @foo = (43.3, 10, 8, 5.12, “a”);
• $foo and @foo are *completely unrelated*
• In fact, $foo has nothing to do with $foo[2];
• “This may seem a bit weird, but that‟s okay,
  because it is weird.”
  – Programming Perl, pg. 54
              More about arrays
• special variable for each array:
  – @foo = (6, 25, 43, 31);
  – $#foo  3. Last index of @foo.
  – $foo[$#foo]  31;
• This can be used to dynamically alter the size
  of an array:
  – $#foo = 5;
     • creates two undefined values on the end of @foo
  – $#foo = 2;
     • destroys all but the first three elements of @foo
        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)
  – $foo[$#foo] and $foo[-1] always refer to
    same element
• “Slices” – piece of an array (or list) (or hash)
  – @bar = @foo[1..3];            @bar (25, 43, 31)
  – @bar = @foo[0,2];             @bar (3, 43)
  – @bar = @foo[1];               @bar (25);
     • You probably don‟t want that
                 Join/Split

• Built-in Perl functions
• split: split a string into a list of values
  – $BigString = “Hello,_I_am_Paul”;
  – @strings = split „_‟, $BigString;
  – @strings  (“Hello,”, “I”, “am”, “Paul”);
• join: join a list/array of values together
  – $BigString = join „ ‟, @strings;
  – $BigString  “Hello, I am Paul”;
                       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 don‟t care
    where in array elements actually are), USE A HASH
           Hash example
• Want a list of short names for months:
%months = (
  “Jan” => “January”,
  “Feb” => “February”,
  “Mar” => “March”,
  …
);
• reference by *curly* brackets…
  – Avoid confusion with array notation
• $month{“Jan”}  “January”;
            More Hash Examples
• Hash elements can be dynamically created (in fact, so
  can entire hashes)
• $profs{“Perl”} = “Paul Lalli”;
• $profs{“Op Sys”} = “Robert Ingalls”;
• $profs{“CS1”} = “David Spooner”;
• %profs  (“Perl” => “Paul Lalli”,
•             “Op Sys” => “Robert Ingalls”,
•             “CS1” => “David Spooner”);
• Hashes will “flatten” into normal lists:
• @p_arr = %profs;
• @p_arr (“Perl”, “Paul Lalli”, “Op Sys”, “Robert
  Ingalls”, “CS1”, “David Spooner”)
               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
• @INC – directories Perl looks for include files
• $0 – name of currently running script
• @ARGV – command line arguments
            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.
          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 $_
              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 don‟t need to (usually –
  because this is Perl).
• more examples:
  –   print   “My   name is $name\n”;
  –   print   “Hi   ”, “what‟s ”, “yours?\n”;
  –   print   5 +   3;
  –   print   ((4   * 4). “\n”);
                 One catch
• Recall that print takes a list of arguments.
• By default, print outputs that list to the
  terminal one right after another
  @nums = (23, 42, 68);
  print @nums, “\n”;
• 234268
• To change string printed between list items,
  set the $, variable:
 $, = “, ”;
 print @nums, “\n”;
• 23, 42, 68
           Input from keyboard
• read line operator: <>
  – aka “angle operator”, “diamond operator”
  – Encloses file handle to read from. Defaults to
    STDIN, which is what we want.
• $input = <>;
  – read one line from STDIN, and save in $input
• @input = <>;
  – read all lines from STDIN, and save as array in
    @input
           Our First Bit of Magic
• The Camel will describe several Perl features as
  “magical”.
• The <> operator is the first such feature.
• If you pass the name of a file (or files) as command line
  arguments to your script, <> 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 <> reads from STDIN
• If you want to read from STDIN before files have been
  read, must do it explicitly:
   – $line = <STDIN>;
                   Chop & Chomp
• When reading in a line, newline (“\n”) is included.
   – Usually don‟t 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 = <>);
   – Very common method of reading in one string from input.
• chomp actually takes a list, and will chomp each
  element of that list
• chomp (@s = (“foo\n”,“bar\n”,“baz\n”));
• @s  (“foo”, “bar”, “baz”);
            And we‟re done
• That‟s 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)
• Don‟t forget – Fill out the signup form from
  the class webpage by the end of the week!

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:5
posted:1/30/2012
language:
pages:31