in reply to detecting an undefined variable
Hello LloydRice,
What you are trying to do is quite possible:
use strict; use warnings; #my $scale = 42; my $myscale = init_myscale(1); print "\$myscale = >$myscale<\n"; sub init_myscale { my ($default) = @_; no strict qw( vars ); no warnings qw( uninitialized ); return eval $scale ? $scale : $default; }
Output:
23:16 >perl 2018_SoPW.pl $myscale = >1< 23:18 >
But, as haukex and LanX have said, the fact that you want to do this strongly suggests that there is a flaw in your design.
Update: return eval $scale ? $scale : $default; should be return eval defined $scale ? $scale : $default; to catch the case where $scale exists but has a false value ($state = 0). Note that this approach can’t be used to catch the case where $state exists but has the value of undef.
Hope that helps,
| Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
|
|---|