in reply to How to generate Perl statements on the fly and execute them when they are synthesized.
For instance in this case you can use some loop control to solve the actual problem. Consider:
Note the my in there. That is necessary if you are working with strict.pm, which is a habit I strongly recommend. The time spent not tracking down typos makes it highly worthwhile.# Takes a string and a list of REs. Returns true or false # depending on whether all of them match. sub match_all { my $str = shift; foreach my $re (@_) { return 0 unless $str =~ /$re/; } return 1; }
And then you would use this as follows:
(Note. Using /\Q$re\E/ for the match will handle strange characters in it, but would block using REs compiled with qr//. Choose, freedom or error handling?)if (match_all($str, split(/\s+/, $search_str))) { print "All of them matched\n"; }
Also you could achieve the same effect with nested loop and loop control:
OUTER: { foreach my $animal (@animals) { last OUTER if $str !~ /$animal/; } print "Matched all of them!"; }
|
|---|