What is Perl? Optimized for String Manipulation and File I/O Full support for Regular Expressions... String Operation Arithmeticle less than or equal to = cmp compare, return 1, 0, -1
Trang 1Perl Basics
A Perl Tutorial
NLP Course - 2006
Trang 2What is Perl?
Optimized for String Manipulation and File I/O
Full support for Regular Expressions
Trang 3Running Perl Scripts
Download ActivePerl from ActiveState
Just run the script from a 'Command Prompt'
Trang 4Basic Syntax
Only single line comments
Trang 5Scalars and Identifiers
A variable name
Case sensitive
A single value (string or numerical)
Accessed by prefixing an identifier with '$'
Assignment with '='
$scalar = expression
Trang 6 With ' (apostrophe)
Everything is interpreted literally
With " (double quotes)
Variables get expanded
With ` (backtick)
The text is executed as a separate process, and the output of the command is returned as the value of the string
Check 01_printDate.pl
Trang 7String Operation Arithmetic
le less than or equal to <=
ge greater than or equal to >=
cmp compare, return 1, 0, -1 <=>
Comparison Operators
Trang 8Operator Operation
||, or logical or
&&, and logical and
!, not logical not
xor logical xor
Logical Operators
Trang 9$newstring = $string1 $string2; #"potatohead"
$newerstring = $string1 x 2; #"potatopotato"
$string1 = $string2; #"potatohead"
String Operators
Check concat_input.pl
Trang 10Perl 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
Trang 11 Ordered collection of scalars
Zero indexed (first item in position '0')
Elements addressed by their positions
Trang 13 @: reference to the array (or a portion of it, with [])
$: reference to an element (used with [])
Trang 15Associative Arrays - Hashes
Arrays indexed on arbitrary string values
Key-Value pairs
Use the "Key" to find the element that has the
"Value"
% : refers to the hash
{}: denotes the key
$ : the value of the element indexed by the key
(used with {})
Trang 17Arrays Example
#!/usr/bin/perl
# Simple List operations
# Address an element in the list
# Join elements at positions 0, 1, 2 and 4 into a
white-space delimited string
print("orchestral brass: ",
join(" ",@brass[0,1,2,4]),
"\n");
@unsorted_num = ('3','5','2','1','4');
@sorted_num = sort( @unsorted_num );
# Sort the list
@numbers_10, "\n");
# Remove the last print("Numbers (1-9): ", pop(@numbers_10), "\n");
# Remove the first print("Numbers (2-9): ", shift(@numbers_10), "\n");
# Combine two ops print("Count elements (2-9): ",
$#@numbers_10;
# scalar( @numbers_10 ), "\n");
print("What's left (numbers 2-9): ",
@numbers_10, "\n");
Trang 18Hashes Example
#!/usr/bin/perl
# Simple List operations
$player{"clarinet"} = "Susan Bartlett";
$player{"basson"} = "Andrew Vandesteeg";
$player{"flute"} = "Heidi Lawson";
$player{"oboe"} = "Jeanine Hassel";
Trang 19Pattern Matching
searched for in a character string
/pattern/
=~: tests whether a pattern is matched
!~: tests whether patterns is not matched
Trang 20Pattern Matches Pattern Matches /def/ "define" /d.f/ dif
/\bdef\b/ a def word /d.+f/ dabcf
/^def/ def in start of
line
/d.*f/ df, daffff
/^def$/ def line /de{1,3}f/ deef, deeef
/de?f/ df, def /de{3}f/ deeef
/d[eE]f/ def, dEf /de{3,}f/ deeeeef
/d[^eE]f/ daf, dzf /de{0,3}f/ up to deeef
Patterns
Trang 21Character Ranges
Escape
Sequence Pattern Description
\d [0-9] Any digit
\D [^0-9] Anything but a digit
\w [_0-9A-Za-z] Any word character
\W [^_0-9A-Za-z] Anything but a word char
\s [ \r\t\n\f] White-space
\S [^\r\t\n\f] Anything but white-space
Trang 23Pattern Matching Options
Escape Sequence
Trang 25Predefined Read-only Variables
EXAMPLE
$_ = "this is a sample string";
/sa.*le/; # matches "sample" within the string
Trang 26The 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.
Trang 27String - Pattern Examples
Trang 28String – Pattern Example