in reply to Need help testing to see if a variable is one value or the other
You can generalize the code somewhat:
#!/usr/bin/perl use strict; use warnings; my @options = qw(yes no); my $var = 'Yes'; my $isOption = join '|', @options; if ($var =~ /^($isOption)$/i) { print "Matched '$1'\n"; } else { print "Want one of: @options\n"; }
Prints:
Matched 'Yes'
Not that the match is now case insensitive (the /i on the regex) and it is now trivial to add new options to be matched.
Perl is environmentally friendly - it saves trees
|
---|
In Section
Seekers of Perl Wisdom