If you're doing a lot of these tests for different values of "abc", I'd suggest just trimming the whitespace once and then working with the trimmed string:
# ...
use String::Trim;
while(<>) {
trim;
given($_) {
when("abc") {
say "Got 'abc'!";
}
when("cmc") {
say "Got 'cmc'!";
}
when("uep") {
say "Got 'uep'!";
}
}
}
If you're looking to do more complicated processing that nonetheless involves a lot of boilerplate, you could build a list of regular expressions on-the-fly:
sub got_abc {
say "Got 'abc'!";
}
sub got_cmc {
say "Got 'cmc'!";
}
sub got_uep {
say "Got 'uep'!";
}
sub got_a3 {
say "Got 'a{3}'!";
}
my %interesting = (
"abc" => \&got_abc,
"cmc" => \&got_cmc,
"uep" => \&got_uep,
"a{3}" => \&got_a3,
);
# add boilerplate
my %tests = map {
qr/^\s*$_\s*$/ => $interesting{$_}
} keys %interesting;
# process data and apply tests
while(<>) {
chomp;
foreach my $test (keys %tests) {
$_ =~ $test and &{ $tests{$test} };
}
}
All