Академический Документы
Профессиональный Документы
Культура Документы
A Perl Tutorial
Check 01_printDate.pl
Comparison Operators
String Operation Arithmetic
lt less than <
eq equal to ==
ne not equal to !=
Operator Operation
||, or logical or
&&, and logical and
!, not logical not
xor logical xor
String Operators
Operator Operation
. string concatenation
x string repetition
.= concatenation and assignment
$string1 = "potato";
$string2 = "head";
Check concat_input.pl
Perl Functions
Perl functions are identified by their unique names
(print, chop, close, etc)
Function arguments are supplied as a comma
separated list in parenthesis.
The commas are necessary
The parentheses are often not
Be careful! You can write some nasty and unreadable
code this way!
Check 02_unreadable.pl
Lists
Ordered collection of scalars
Zero indexed (first item in position '0')
Elements addressed by their positions
List Operators
(): list constructor
, : element separator
[]: take slices (single or multiple element chunks)
List Operations
sort(LIST)
a new list, the sorted version of LIST
reverse(LIST)
a new list, the reverse of LIST
join(EXPR, LIST)
a string version of LIST, delimited by EXPR
split(PATTERN, EXPR)
create a list from each of the portions of EXPR that
match PATTERN
Check 03_listOps.pl
Arrays
A named list
Dynamically allocated, can be saved
Zero-indexed
Shares list operations, and adds to them
Array Operators
@: reference to the array (or a portion of it, with [])
$: reference to an element (used with [])
Array Operations
push(@ARRAY, LIST)
add the LIST to the end of the @ARRAY
pop(@ARRAY)
remove and return the last element of @ARRAY
unshift(@ARRAY, LIST)
add the LIST to the front of @ARRAY
shift(@ARRAY)
remove and return the first element of @ARRAY
scalar(@ARRAY)
return the number of elements in the @ARRAY
Check 04_arrayOps.pl
Associative Arrays - Hashes
Arrays indexed on arbitrary string values
Key-Value pairs
Use the "Key" to find the element that has the
"Value"
Hash Operators
% : refers to the hash
{}: denotes the key
$ : the value of the element indexed by the key
(used with {})
Hash Operations
keys(%ARRAY)
return a list of all the keys in the %ARRAY
values(%ARRAY)
return a list of all the values in the %ARRAY
each(%ARRAY)
iterates through the key-value pairs of the %ARRAY
delete($ARRAY{KEY})
removes the key-value pair associated with {KEY} from
the ARRAY
Arrays Example
#!/usr/bin/perl #Add a few more numbers
# Simple List operations @numbers_10 = @sorted_num;
push(@numbers_10, ('6','7','8','9','10'));
# Address an element in the list
@stringInstruments = print("Numbers (1-10): ",
("violin","viola","cello","bass"); @numbers_10,
@brass = "\n");
("trumpet","horn","trombone","euphonium",
"tuba"); # Remove the last
$biggestInstrument = $stringInstruments[3]; print("Numbers (1-9): ",
pop(@numbers_10),
print("The biggest instrument: ",
$biggestInstrument); "\n");
# Remove the first
# Join elements at positions 0, 1, 2 and 4 into a print("Numbers (2-9): ",
white-space delimited string
shift(@numbers_10),
print("orchestral brass: ",
join(" ",@brass[0,1,2,4]), "\n");
"\n"); # Combine two ops
print("Count elements (2-9): ",
@unsorted_num = ('3','5','2','1','4');
$#@numbers_10;
@sorted_num = sort( @unsorted_num );
# scalar( @numbers_10 ),
# Sort the list "\n");
print("Numbers (Sorted, 1-5): ", print("What's left (numbers 2-9): ",
@sorted_num, @numbers_10,
"\n");
"\n");
Hashes Example
#!/usr/bin/perl
# Simple List operations
$playerCount = scalar(@woodwindPlayers);
$' is the part of the string after the part that matched
EXAMPLE
$_ = "this is a sample string";
/sa.*le/; # matches "sample" within the string
# $` is now "this is a "
# $& is now "sample"
# $' is now " string"
Because these variables are set on each successful match, you should save the
values elsewhere if you
need them later in the program.
The split and join Functions
The split function takes a regular expression and a string, and looks for all
occurrences of the regular expression within that string. The parts of the string
that don't match the regular expression are returned in sequence as a list of
values.
The join function takes a list of values and glues them together with a glue string
between each list element.
Split Example Join Example
$line = $bigstring = join($glue,@list);
"merlyn::118:10:Randal:/home/merlyn:
/usr/bin/perl"; For example to rebuilt the password file
@fields = split(/:/,$line); # split $line, try something like:
using : as delimiter $outline = join(":", @fields);
# now @fields is
("merlyn","","118","10","Randal",
# "/home/merlyn","/usr/bin/perl")
String - Pattern Examples
A simple Example
#!/usr/bin/perl
print ("Ask me a question politely:\n");
$question = <STDIN>;