NOTE: Failed matches in Perl do not reset the match variables, which makes it easier to write code that tests for a series of more specific cases and remembers the best match.
####
#!/usr/bin/perl
use strict;
use warnings;
my $status = "it's a beautiful day but I am feeling crapy"; # note typo
$status =~ /(beautiful|average|tough)/;
print "Day: $1\n";
$status =~ /(happy|alright|crappy)/;
print "Feeling: $1\n";
__END__
####
[22:08][nick:~/monks]$ perl 1139246.pl
Day: beautiful
Feeling: beautiful
[22:08][nick:~/monks]$
####
#!/usr/bin/perl
use strict;
use warnings;
my $status = "it's a beautiful day but I am feeling crapy"; # note typo
if ( $status =~ /(beautiful|average|tough)/ ) {
print "Day: $1\n";
} else {
print "No match for day\n";
}
if ( $status =~ /(happy|alright|crappy)/ ) {
print "Feeling: $1\n";
} else {
print "No match for feeling\n";
}
__END__
####
[22:13][nick:~/monks]$ perl 1139246.pl
Day: beautiful
No match for feeling
[22:13][nick:~/monks]$
####