Professional Documents
Culture Documents
Most heavily used input operator is th angle
operator. <STDIN> is used to read line from
standard input.
e.g.
chomp($name=<STDIN>);
Example-
"Hell is filled with ameture musicians."
'Give me $500'
Quoting format
Double quotes do -
Variable interpolation (replacement of variable
name by its value)
Backslash interpretation (escape characters)
\u - Force next character to uppercse
\l - Force next character to lowercase
\U - Force all following characters to uppercase
\L - Force all following characters to lowercase
\Q - backslash all following nonalphanumeric
characters
\E - end \U,\L and \Q
Single and double quotes
$a = 'apples';
$b = 'bananas';
print $a . ' and ' . $b;
− prints: apples and bananas
print '$a and $b \n';
− prints: $a and $b \n
print "$a and $b \n";
− prints: apples and bananas followed by a newline
Single Quotes
Example-
$cwd =`pwd`; #string output from a command
Variables naming
Naming convention -
Unlike C or Java, Perl variables don't have to be
declared before being used. Variables are created
when it's 1st used. They are identified by the "funny
characters" that precede them.
Since reserved words and filehandles are not
preceded by a 'funny character', variable names will
not conflict with reserved words or filehandles.
A variable name starts with a letter, it may consist of
any number of letters (an underscore counts as a
letter) and/or digits.
Types
Perl has mainly these three data types -
Type Symbol
Scalar $
Array @
Hash %
$info = "Caine:Michael:Actor:14, Leafy
Drive";
@personal = split(/:/, $info);
@personal =
("Caine", "Michael", "Actor", "14,
Leafy Drive");
Lists and arrays
Double dot(..) operator for generating numbers
@x = (1..6); # same as (1, 2, 3, 4, 5, 6)
@z =(2..5,8,11..13);#same as (2,3,4,5,8,11,12,13)
qw() "quote word" function
qw(Jan Piet Marie) is a shorter notation for
("Jan","Piet","Marie").
Hash
A hash is an unordered set of scalars, accessed by
some string value that is associated with each scalar.
For this reason hashes are often called "associative
arrays".
Example -
%longday = (
"Sun" => "Sunday",
"Mon" => "Monday",
"Tue" => "Tuesday",
"Wed" => "Wednesday",
"Thu" => "Thursday",
"Fri" => "Friday",
"Sat" => "Saturday",
);
Operations on Hash
To get the value with key Wed we use
$longday{"Wed"}
keys - retrieves all the keys in a hash
values - retrieves all the values in a hash
each - retrieves a key/value pair from a hash
delete - removes a key/value pair
Operations on Hash cont.
@keys=keys(%longday);
@values=values(%longday);
return array of values in random order.
($key,$value)=each(%weekday);
returns, in random order, a two-element array whose
elements are the key and the corresponding value of a
hash.
$del=delete $weekday{"Fri"};
$a = $b; # Assign $b to $a
$a += $b; # Add $b to $a
$a = $b; # Subtract $b from $a
$a .= $b; # Append $b onto $a
Conditional checks
Syntax:-
if (Expression) {Block}
Syntax:
if (Expression) {Block} elsif (Expression)
{Block}... else {Block}
if - elsif statements
if (!$a)
{ print "The string is empty\n"; }
elsif (length($a) == 1)
{ print "The string has one
character\n"; }
elsif (length($a) == 2)
{ print "The string has two
characters\n"; }
else
{ print "The string has many
characters\n"; }
Loop Structures
while(Expression){Block}
for (Expression1;Expression2;Expression3)
{Block}
until ($answer eq "yes"){
sleep(1);
print "Are you o.k. yet? ";
chomp($answer=<STDIN>);
}
do..while loops
#!/usr/local/bin/perl
do
{
print "Password? ";
$a = <STDIN>;
chop $a;
}while ($a ne "fred");
do..until loop
#!/usr/local/bin/perl
do
{
$a=<>;
print $a,"\n";
}until($a==1);
for loops
for loops are just as in C or Java
for ($i = 0; $i < 10; ++$i)
{
print "$i\n";
}
foreach
#Visit each item in turn and call it
$morsel
foreach $morsel (@food)
{
print "$morsel\n";
print "Yum yum\n";
}
Pattern Matching
The s/// operator modifies sequences of characters Substitute
The tr/// operator changes individual characters. Translation
The m// operator checks for matching (or in short //) Matching
The first part between the first two slashes contain a search
pattern
The second part between two final slashes contain the
replacement
Behind the final slash are characters to modify the behavior of the
commands.(modifiers)
Quantifier Meaning
+ Matches the preceding pattern element one or more times.
? Matches zero or one times.
* Matches zero or more times.
Denotes the minimum N and maximum M match count. {N}
{N,M} means exactly N times; {N,} means at least N times.
Basic pattern matching
$sentence =~ /the/
− True if $sentence contains "the"
$sentence = "The dog bites.";
if ($sentence =~ /the/) # is false
− …because Perl is case-sensitive
!~ is "does not contain"
RE special characters
. # Any single character except a newline
m/Good morning/
/Good evening/
/\/usr\/var\/adm/
m#/usr/var/adm#
m(Good evening)
m'$name'
Substitution
s/old/new/;
s/old/new/i;
s/old/new/g;
s+old+new+g;
s(old)/new/; s[old]{new};
s/old/expression to be evaluated/e;
s/old/new/ige;
s/old/new/x;
Transliterate
The tr/// operator allows the modification of character
tr does character-by-character translation