Regular Expressions

Jacinta Richardson jarich at perltraining.com.au
Fri Apr 12 03:00:52 CDT 2002



> my @exceptions; #list of addresses that are exceptions to the rule
> my $in_mail; #the mail you received
> my $in_address; #address of the mail you received
> 
> unless ( { 
> 	my $filter=0; #0 if no exception was found, 1 if the $in_address is
>                     # an exception
> 	foreach (@exceptions) {
> 		if(/^$in_address$/) { #if the in address matches an
>                                     #exception
> 			$filter=1;
> 		}
> 	$filter; #return the value of $dont_filter
> 	} ) {
> 	if(/.*[0-9]{5,10}\@aol.*/) {
> 		throw_away_mail_from $in_mail; #call some function that
>                                              # throws away the e-mail
> 	}
> }

The following improvements can be made.  
- eq is faster than /^something$/
- the foreach is doing a grep.
(- the unless isn't doing anything)

so:

my @exceptions;
my $in_mail;
my $in_address;

	# return 1 if we like the address, return 0 otherwise.
if(grep '$in_mail', @exceptions) 
{
	return 1;	# it's an exception
}
elsif($in_mail =~ /.*[0-9]{5,10}\@aol.*/)
{
	return 0;	# we think it's spam
}

	# do more tests.
	# ...
	# must be good.
return 1;

> the foreach when I set $dont_filter, but I can't remember how (is there a
> break statement in Perl). But that's the idea, I would guess.

The break statement in perl is "last"

	Jacinta




More information about the Melbourne-pm mailing list