Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: detecting an undefined variable

by Athanasius (Archbishop)
on Sep 21, 2019 at 13:21 UTC ( [id://11106482]=note: print w/replies, xml ) Need Help??


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,

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11106482]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-04-23 23:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found