I have a list of conditions like this:
if ($_GET{'foo'} eq 'bar') { bar(); }
elsif ($_GET{'foo'} eq 'example') { example(); }
else { do_error_stuff; }
My real lists can have up to 20 conditions like that.
I'd like to shrink that down to something like:
if ($_GET{'foo'} && defined(&$_GET{'foo'})) { $GET{'foo'}(); }
else { do_error_stuff; }
What I found is that if I change the array value to a scalar then it works:
# This throws an error on the "defined" function
$_GET{'foo'} = 'bar';
if (
$_GET{'foo'} &&
defined(&$_GET{'foo'})
) {
&$_GET{'foo'};
exit;
}
else { print "no"; }
sub bar {
print "yes";
}
# But this works
$_GET{'foo'} = 'bar';
$foo = $_GET{'foo'};
if (
$foo &&
defined(&$foo)
) {
&$foo;
exit;
}
else { print "no"; }
sub bar {
print "yes";
}
Is there some other magical combination to use an array value here? I tried these variations, but none worked:
defined(&$_GET{'foo'})
defined &$_GET{'foo'}
defined($_GET{'foo'}())
defined $_GET{'foo'}()