|
Conditional Constructs Scalar Operators |
Regular Expressions Functions |
String Test Meaning
eq Equal to
ne Not equal
gt Greater then
le Greater then or equal to
lt Less than
le Less than or equal to
cmp Not equal to, with signed return
while (<>) { #Print lines that weren't blank.
chop;
if ($_ ne "") {
print $_,"/n";
}
}
Pattern Matching Result
$a =~/pat/ Match True if $a contains pattern
$a =~s/p/r/ Substitution Replace occurrences of p with r in $a
$a =~tr/a-z/A_Z/ Translation Translate to corresponding characters
Logical Operators Result
$a && $b And True if $a is true and $b is true
$a || $b Or $a if $a is true, otherwise $b
! $a Not True if $a is not true
String Operations Result
$a . $b Concatenation Values of $a and $b as one long string
$a x $b Repeat Value of $a strung together $b times
substr($a,$o,$1) Substring Substring at offset $o of length $1
index($a,$b) Index Offset of string $b in string $a
$/ = ""; #Enable paragraph mode.
$* = 1; #Enable multi-line patterns.
# Now read each paragraph and split into words. Record each instance
# of a word in the %wordcount associative array.
while <> {
s/-\n//g; #Dehyphenate hyphenations.
tr/A-Z/a-z; #Canonicalize to lowercase.
&words = split(/\W*\s+\W*/, $_);
foreach $word (@words) {
$wordcount($word)++; #Increment the entry.
}
}
^ Matches the beginnig of the string (or line, if $* set)
$ Matches the end of the string ( or line, if $* set)
\b Matches on word boundary (between \w and \W)
\B Matches on non-word boundary
{n,m} Must occur at least n times but no more than m times
{n,} Must occur at least n times
{n} Must match exactly n times
* 0 or more times (same as {0})
+ 1 or more times (same as {1,})
? 0 or 1 time (same as {0,1})
\n Newline
\r Carriage return
\t Tab
\f Formfeed
\d A digit, same as [0-9]
\D A non-digit
\w A word character (alphanumeric), same as [0-9a-z_A-Z]
\W A non-word character
\s A whitespace character, same as [\t\n\r\f]
\S A non-whitespace character
$ Matches the end of a string
$_ Returns current string
$& Returns the intire matched string
$^ Holds everything before matching
$' Holds everything after the matched string
s/^([^ ]*) *([^ ]*)/$2 $1/; #swap first two words
/{\w*)\s*=\s*\1/; #match "foo = foo"
/.(80,}; #match line at least 80 chars
/^(\d+\.?\d*|\.\d+)$/; #match valid Perl number
if (/Time: (..):(..):(..)/) { #pull fields out of line
$hours = $1;
$minutes = $2;
$seconds = $3;
}
chop(LIST)
chop(VARIABLE)
chop VARIABLE
chop
Example:
while {
shop; # avoid \n on last field
@array = split (/:/);
....
}
Example:
if ( crypt($quess, $pass) eq $pass) {
# guess is correct
}
defined(EXPR)
defined EXPR
Example:
print if defined $switch{'D'}; #Test a scalar value from associative array
@foo = grep(!/^/#/, @bar); #@bar containes lines of code. Weed out comment lines.
index(STR,SUBSTR,POSITION)
index(STR,SUBSTR)
Example:
$pos = $1;
while (( $pos = index($string, $lookfor, $pos)) >= $[) {
print "Found at $pos\n"; #look for occurence of $lookfor from posistion
#$[ in a $string and print it
$pos ++;
}
S_ = join(':', $login,$passwd,$uid,$gid,$gcos,$home,$shell);
length(EXPR)
length EXPR
print(FILEHANDLE LIST)
print(LIST)
print FILEHANDLE LIST
print LIST
print
printf(FILEHANDLE FORMAT,LIST)
printf(FORMAT,LIST)
printf FILEHANDLE FORMAT,LIST
printf FORMAT,LIST
reverse(LIST)
reverse LIST
Example:
%barfoo = reverse %foobar;
g option indicates that all occurences of the pattern
are to be replaced;
i option indicates that matching is to be done in a
case-senesitive manner.
e option indicates that replacement string is to be
evaluted as an expression rather than just as a
double-quoted string.
for (;;) { #The substitute takes each line and replaces it
print "$_\$"; #it with the value of $1 after processing it
last unless $_ = ; #through backquotes(running a /bin/sh on it)
s`^([^#)(#.*)?\n$`$1`;
}
splice(ARRAY,OFFSET,LENGTH,LIST)
splice(ARRAY,OFFSET,LENGTH)
splice(ARRAY,OFFSET)
Example:
sub aeq { #compare two array values
local(&a) = splice(@_,0,shift);
local(@b) = splice(@_,0,shift);
return 0 unless @a == @b; # same length?
while (@a) {
return 0 if pop(@a) ne pop(@b);
}
return 1;
} #assuming array lengths before arrays
split(/PATTERN/,EXP,LIMIT)
split(/PATTERN/,EXP)
split(/PATTERN/
split
Example:
($login, $passwd, $remainder ) = split(/:/,$_,3);
substr(EXPR,OFFSET,LENGTH)
substr(EXPR,OFFSET)
Example:
substr($_,-1,1) = "Curly"; # replace the last character of $_ with "Curly"
d option deletes all characters within SEARCHLIST
that are not found in REPLACEMENTLIST.
s option causes any substitutions that would result
in multiple identical REPLACEMENTLIST characters
to be output consecutively to be replaced with just
a single occurrence of that character.
c option complements the SEARCHLIST; any caharacters
mentioned in SEARCHLIST are removed from string and
the resulting string is used in place of SEARCHLIST.
tr/SEARCHLIST/REPLACEMENTLIST/[c][d][s]
y/SEARCHLIST/REPLACEMENTLIST/[c][d][s]
Example:
($HOST = $host) =~ tr/a-z/A-Z/; #Translate while copying document
undef(EXPR)
undef EXPR
undef