$scalar, @array, $array[$index], %hash, $hash{$key}
" a is $a "
sub double { my ($n) = @_; return 2*$n; } sub doubleInPlace { return 2*$_[0] }
- scalar vs list context
- debugger: "perl -de1" and "perl -d filename"
- many functions : print, sort, reverse, map, shift, ...
- many operators : comma (yes, it's an operator) , or , and ,
&& , || , eq , le , == , < , => , x , "" , '' , ...
- various control flow : if, unless, for, foreach, while, ...
if ($a>10) { print(" a = $a > 10 \n") } ;
print " a = $a > 10 \n" unless $a <= 10;
- input : <> or <STDIN> in scalar vs list context
- output : STDOUT (by default) with
print or printf
- CPAN :
use Some::Other::Stuff;
myMap {code} @array sub myMap (&@) {...}
Also means defining "sub grabOne($){...}" means
it only takes 1 argument, so that
{grabOne, 1, 2, 3} turns into
{grabOne(1), 2, 3}
"If you take 'text' in the widest possible sense, perhaps 90% of what you do is 90% text processing.You specify a pattern by creating a regular expression (or regex), and Perl's regular expression engine ... then takes that expression and determines whether (and how) that pattern matches your data. While most of your data probably will be text strings, there's nothing stopping you from using regexes to match and replace any byte sequence, even what you'd normally think of as "binary" data.
Perl's patterns provide a powerful way to scan through mountains of mere data and extract useful information from it.
Perl's regular expression are potent, packing a lot of meaning into a small space. They can therefore be daunting..."
- Larry Wall, camel book, chapter 5
$a="Hi Jim"; $a =~ s/J../Bob/; # substitute J followed by any 2 chars with "Bob".
s/r1/r2/ # Substitute r2 for r1 m/regexp/ # Match regexp /regexp/ # same thing. (The "m" is optional.) split /regexp/, $string # split $string into list at places that match regexp
\ | ( ) [ { ^ $ * + ? .
if ($a =~ /Joe|Jim|Fred/) { }
$a =~ s/(Jim|Joe) Mahoney/That Guy/;
# Swap the first two words in text. Preserve white space between them.
# Here \w is a "word character", and \s is a "space chacter".
# The + means "1 or more".
for ($text) {
s/^(\w+)(\s+)(\w+)/$3$2$1/;
}
for ($text) {
s/----+/<hr noshade size=1>/g;
s/====+/<hr noshade size=2>/g;
s/\n\n/\n<p>\n/gm;
}
For example, given a file named contra.html
------ cut here for contra.html ---
<html>
<head><title>Contra Dance</title></head>
<body>
<h1>Contra Dance</h1>
When you're looking for someplace to go on a Friday night,
check out
<a href="http://www.guidingstargrange.org/">Greenfield Contras</a>.
Look <a href="moreinfo.html">here</a> for more info.
<hr>
My homepage is http://me.org/.
Here I am : <img src="jim.gif">
</body>
</html>
------ end contra.html ---
then the command "findlinks contra.html" should print out
at least something like
http://www.guidingstargrange.html
moreinfo.html
and perhaps even something as elaborate as
A tags:
http://wwww.guidingstartgrange.html "Greenfield Contras"
moreinfo.html "here"
IMG tags:
jim.gif
Other urls mentioned outside tags :
http://me.org/
Other images outside tags :
none
Anything along these lines is OK. We'll expand this exercise into something more interesting in further weeks.