see also:
Scan a string for email addresses
Say you have a web page, loaded into the variable
theFile, and want to find all email links, and put them into an array...
foreach( split( /\n/, $theFile ) ) {
# note, the $_ variable, the element for each iteration of
# foreach is implied in the regex substitution statement below
# Find all email addresses
if (s!(([a-zA-Z0-9_\-\.]+)@((\[[0-2]{1}[0-5]{1}[0-5]{1}\.[0-2]{1}[0
-5]{1}[0-5]{1}\.[0-2]{1}[0-5]{1}[0-5]{1}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}
|[0-2]{1}[0-5]{1}[0-5]{1})(\]?))!$1!i) {
# Stick each in an array
push(@names, $1);
}
}
# Remove duplicates - weird but effective way
@uniq = sort keys %{ { map { $_, 1 } @names } };
Strip out selected lines from a string
Now, I'm sure there's a better perl 1-liner way to do this, but here I split the string line by line, and if the line is wanted, I stick it in a new string. I got a 1-liner regex substitution to almost work, but couldn't prevent it from sticking in blank lines for the unwanted lines.
foreach( split( /\n/, $changesIn ) ) {
if (m!^$theTopic!g) {
$changes = "$changes$_\n";
}
}
How to validate a kredit kard
http://argentavis.hypermart.net/perl/ccvalidation.html
How to parse command line options
#!/usr/bin/perl
use strict;
use Getopt::Std;
our($opt_w, $opt_h);
getopts('hw:');
# no-parameter switch
print "doing help dump\n" if ($opt_h);
# parameter switch
if ($opt_w) {
print "got $opt_w: for 'w' switch\n";
} else {
print "no 'w' switch\n";
}
How to recursively get all files starting in a directory
How to do file uploading via perl
see
http://www.webmasterbase.com/article/474
Perl Switches
| -w |
use warnings |
| -e |
the next arg is the script to run (e.g perl -e 'print "hello world\n"' ) |
| -p |
walks through the file argument (?) see 'global file substitution' example |
Doing a global file substitution (example: perl -p -i -e 's/from/to/g' filename)
Writing to a file
# the '>' indicates I want to write to the file
open(theFile, ">dump.txt") || die "can't open the file";
print theFile "I've written to the file!\n";
close(theFile);
Reading from a file line by line
Note the <> construct when blank meant from stdin. Now, it means from the open file handle.
open(theDict, "/usr/share/dict/linux.words") || die "Can't open dict\n";
while (<theDict>) {
print;
}
close(theDict);
Reading from stdin
Examples to read in each line and print it out, pre-pended with a -*
while ($line = <>) { print "-*$line"; } |
basic example, but we can be more succint |
...or simpler: while (<>) { $_ =~ s!^!-*!; print $_; } |
works because $_ means 'current line read from <> |
...or simpler still: while (<>) { s!^!-*;!; print; } |
works beacuse $_ is assumed in the print and the substitution |
Checking if a variable is defined
# '$/' is the Optional 'chunk mode' parameter. Designates what the <> operator
# looks for to terminate each read operation. Default is '\n' (??)
$/ = "\n";
while (1) {
if (not defined($junk)) {
print "not defined\n";
$junk = "junk";
} else {
print "now it's defined\n";
last
}
}
--
MattWalsh - 15 Aug 2002