my $text = "I am going to walk to the park.";
for ($text =~ /(\w+)/g) {
if (lc $_ eq "walk") { print "Walking is good for you\n"; }
elsif (lc $_ eq "park") { print "Parks are fun\n"; }
}
####
my %react_to = (
walk => sub { print "Walking is good for you\n" },
park => sub { print "Parks are fun\n" },
);
my $text = "I am going to walk to the park.";
for ($text =~ /(\w+)/g) {
if (my $reaction = $react_to{lc $_}) {
$reaction->();
}
}
####
my %react_to = (
a => sub { shift->{b} = sub { print "A before B\n" } },
b => sub { shift->{a} = sub { print "B before A\n" } },
);
my $text = "b is the first word, but a is not the last.";
for ($text =~ /(\w+)/g) {
print "Read word: $_\n";
if (my $reaction = $react_to{$_}) {
$reaction->(\%react_to);
}
}