Thursday, February 3, 2011

Perl Code Examples

Here's some code I just wrote:


#! /usr/bin/perl -w

undef $/ ;

my $email = <ARGV> ;

if ($email =~ m|(bananas)|is)
{
print $1, "\n" ;
}

Here are some of the things going
on here:

  • The scripts slurps the entire
    file into one string
  • The name of the string
    it puts everything into is called
    $email
  • The file that is being
    slurped in is named on the
    command line

I use this script to test whether or not a
pattern I'm thinking of using is going to
work with Spamassassin. Spamassassin
filters email for spam.

Let's say, for example, there were a drug
that was being promoted through spam
email called longify. I'm hoping
there is no such drug. I'm trying to come
up with a name that is purely hypothetical.

Let further say that the words last longer
with longify
have appeared in a spam
email. I might test this pattern by doing the
following:

#! /usr/bin/perl -w

undef $/ ;

my $email = <ARGV> ;

if ($email =~ m|(longify)|is)
{
print $1, "\n" ;
}

I've altered the above script so that
any text file I place on the command
line will be scanned for the word
longify.

Let's say I save my spam email as a
text file called spam.txt. Let's
further say that I call the above script
checkspam. This being the
case, I will invoke my script as follows:

$ checkspam spam.txt

If the script replies with the word
longify, the script found
the pattern. If it is silent, it did not.

Ed Abbott