in reply to "defined" function fails to evaluate expression; feature or fault?
It's not a feature or bug, it just is :)
defined() returns a bool, (either false/true) when it evaluates something, so when you do this: defined ($x || $y);, it's not doing what you think it does.
Here's an example that illustrates the issue:
perl -wMstrict -E 'my $x = defined $ARGV[5]; say defined $x' 1
Now, $ARGV[5] is not set in any way, but since we're returning the value of the call to $x, $x will most definitely be defined, because it now evaluates to false as the variable was not defined (define() returns false, if something wasn't defined).
You need to call a define on each side of the comparison operator:
my $x = defined $x || defined $y;
...as per your solutions that work. Then, check $x for truth:
die "blah...\n" if ! $x;
...or combined:
die "blech...\n" if ! defined $x || defined $y;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: "defined" function fails to evaluate expression; feature or fault?
by AnomalousMonk (Archbishop) on Jan 26, 2017 at 21:08 UTC | |
by haukex (Archbishop) on Jan 27, 2017 at 07:44 UTC | |
by stevieb (Canon) on Jan 26, 2017 at 21:13 UTC |