LN3 by xiaopangnv


									CST 383             Shell & Script Programming with Unix                              Spring 2011

                                    Brief Lecture Notes 3

                                               CST 383
                                             Spring 2011

This is the beginning of the Perl language notes.

Scalars come in 2 types (generally): numerical and strings
       Numerical is only a number when assigning its value
       Strings (usually ascii) are delimited by quotes (usually double quotes)
       Value assignment syntax is $var = value

There are fundamental, complex objects that are composed of scalars or they may be composed of these
same or other complex objects. The Fundamental complex objects are arrays, lists, and hashes. They look
       Array: @var                     used to statically store lists
       List: ($var1, $var2, . . .)     used to dynamically store arrays
               Array/list elements accessed by index           as $var[index] is a scalar
               May push or pop scalar to/from end of list
               May shift or unshift a scalar onto/from front of list
               May slice a list into or from the middle of a list       use {} to protect the list/array from
               Lists may be sorted by value or reversed
       Hashes: (associative arrays) have keys (index) and value
               Syntax     %var { “key1” => value1,
                                   ‘key2’ => “value2”,
                                            And so on
                                            Is an alias for ,

       Note: all declarations and executable lines are terminated with a semicolon;

                       Access to hash value is via key used as index, e.g., $str = $var{ key2 }
                                                       also can use $var{2} but aren’t sure what you’ll get

       $_ or (for arrays@_) Is an ubiquitous variable scalar, (array, list), type that is always there to hold
value of last operation or to access arguments at the beginning of a program or for parameters to a
CST 383             Shell & Script Programming with Unix                                 Spring 2011

Indices for arrays or lists (or hashes) may be a range , e.g., 1..9 or 3..6 or etc

Pragmas available for use in debugging, etc. Example;
      At beginning of program
              use pragma diagnostic
              use pragma strict to enforce strict use of certain types and scope

There are 4 alternative ways of applying quotes that are especially useful when doing mass applications.
(You may see these in the literature.)
       q/string/ single quotes, usually applied to multiple strings
       qq/ strings/ double quotes
       qw/ words / quote word, applies double quotes to what are normally coma separated words in
               double quotes for a list, array or hash
       qx/program/ applies back quotes for command execution

        The backslashes can be replaced with # or a set of paired characters eg [ ], or { }

Data type examples:
Array @numb = (1, 2, 3);
       @people = qw/Joe Jean, George/;

Multi-dimensional array @matrix = ( [3, 5, 15],
                                    [6, 9, 11],
                       Then could use $matrix[1][2] = 12;          notice indices start at 0 with arrays

Hashes %pet = { “Name” => “Yoda”,
                “type” => “cat
       Then could do $animal = $pet[“type”];                   hash indices officially start at 1

Other possibilities are nested hashes, hashes of arrays, arrays of hashes, etc.

To top