Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
A Perl Tutorial
NLP Course - 2006
What is Perl?
Windows
UNIX Cygwin
Basic Syntax
Variables
Identifiers
A variable name
Case sensitive
Scalar
Strings
Quoting Strings
Comparison Operators
String
Operation
Arithmetic
lt
less than
<
gt
greater than
>
eq
equal to
==
le
<=
ge
>=
ne
not equal to
!=
cmp
compare, return 1, 0, -1
<=>
Logical Operators
Operator
Operation
||, or
logical or
&&, and
logical and
!, not
logical not
xor
logical xor
String Operators
Operator
Operation
string concatenation
string repetition
.=
$string1 = "potato";
$string2 = "head";
$newstring = $string1 . $string2; #"potatohead"
$newerstring = $string1 x 2; #"potatopotato"
$string1 .= $string2; #"potatohead"
Check concat_input.pl
Perl Functions
Check 02_unreadable.pl
Lists
List Operators
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
Array Operators
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
Key-Value pairs
Use the "Key" to find the element that has the
"Value"
Hash Operators
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
# Simple List operations
# Address an element in the list
@stringInstruments =
("violin","viola","cello","bass");
@brass =
("trumpet","horn","trombone","euphonium",
"tuba");
$biggestInstrument = $stringInstruments[3];
print("The biggest instrument: ",
$biggestInstrument);
# 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
print("Numbers (Sorted, 1-5): ",
@sorted_num,
"\n");
Hashes Example
#!/usr/bin/perl
# Simple List operations
$player{"clarinet"} = "Susan Bartlett";
$player{"basson"} = "Andrew Vandesteeg";
$player{"flute"} = "Heidi Lawson";
$player{"oboe"} = "Jeanine Hassel";
@woodwinds = keys(%player);
@woodwindPlayers = values(%player);
# Who plays the oboe?
print("Oboe: ", $player{'oboe'}, "\n");
$playerCount = scalar(@woodwindPlayers);
while (($instrument, $name) =
each(%player))
{
print( "$name plays the
$instrument\n" );
}
Pattern Matching
/pattern/
Match operators
Patterns
Pattern
Matches
Pattern
Matches
/def/
"define"
/d.f/
dif
/\bdef\b/
a def word
/d.+f/
dabcf
/^def/
df, daffff
/^def$/
/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
deef, deeef
Character Ranges
Escape
Pattern
Sequence
\d
[0-9]
Description
Any digit
\D
[^0-9]
\w
[_0-9A-Za-z]
\W
[^_0-9A-Za-z]
\s
[ \r\t\n\f]
White-space
\S
[^\r\t\n\f]
Backreferences
Memorize the matched portion of input
Use of parentheses.
/[a-z]+(.)[a-z]+\1[a-z]+/
asd-eeed-sdsa, sd-sss-ws
NOT as_eee-dfg
Ignore case
Substitutions
Substitution operator
s/pattern/substitution/options
If $string = "abc123def";
$string =~ s/123/456/
Result: "abc456def"
$string =~ s/123//
Result: "abcdef"
$string =~ s/(\d+)/[$1]/
Result: "abc[123]def
Use of backreference!
$`
$'
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.
Join Example
$bigstring = join($glue,@list);
For example to rebuilt the password file
try
something like:
$outline = join(":", @fields);