Download 4up
Transcript
perl – predefined variables
perl – file input/output
$_ The “default variable” for many operations, e.g.
print;
tr/a-z/A-Z/;
while (<FILE>) ...
=
=
=
I
open filehandle, expr
open(F1,
open(F2,
open(F3,
open(F4,
open(F5,
print $_;
$_ =~ tr/a-z/A-Z/;
while ($_ = <FILE>) ...
$. Line number of the line most recently read from any file
$? Child process return value from the most recently closed pipe or
`...` operator
$! Error message for the most recent system call, equivalent to C’s
strerror(errno). Example:
open(FILE, 'test.dat') ||
die("Can't read 'test.dat': $!\n");
'test.dat');
'>test.dat');
'>>test.dat');
'date|');
'|mail -s test');
#
#
#
#
#
open file 'test.dat' for reading
create file 'test.dat' for writing
append to file 'test.dat'
invoke 'date' and connect to its stdout
invoke 'mail' and connect to its stdin
I
print filehandle, list
I
close, eof, getc, seek, read, format, write, truncate
I
“<filehandle >” reads another line from file handle FILE and
returns the string. Used without assignment in a while loop, the line
read will be assigned to $_.
I
“<>” opens one file after another listed on the command line (or
stdin if none given) and reads out one line each time.
For many more: man perlvar
110
109
perl – invocation
I
First line of a Perl script: #!/usr/bin/perl (as with shell)
I
Option “-e” reads code from command line (as with sed)
I
Option “-w” prints warnings about dubious-looking code.
I
Option “-d” activates the Perl debugger (see man perldebug)
I
Option “-p” places the loop
perl – a stream editing example
“Spammers” send out unsolicited bulk email (junk email), for marketing
or fraud. They harvest millions of valid email addresses, for example from
web pages.
To make email addresses in your web pages slightly harder to harvest,
you can avoid the “@” sign in the HTML source. For instance, convert
<a href="mailto:[email protected]">[email protected]</a>
while (<>) { ... print; }
into
around the script, such that perl reads and prints every line. This
way, Perl can be used much like sed:
<a href="mailto:jdoe%40acm.org">jdoe@acm.org</a>
The lines
sed -e 's/sed/perl/g'
perl -pe 's/sed/perl/g'
I
Option -n is like -p without the “print;”.
I
Option “-i[backup-suffix ]” adds in-place file modification to
-p. It renames the input file, opens an output file with the original
name and directs the input into it.
perl -pi.bak <<EOT *.html
s/(href=\"mailto:[^@\"]+)@([^@\"]+\")/$1%40$2/ig;
s/([a-zA-Z0-9\.\-\+\_]+)@([a-zA-Z0-9\.\-]+)/$1@$2/ig;
EOT
will do that transformation for all HTML files in your directory.
More sophisticated methods: hide an email address in JavaScript or use a CAPTCHA.
For more information: man perlrun
111
112