Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Annoying warnings...

by Smylers (Pilgrim)
on Jul 16, 2005 at 10:03 UTC ( [id://475426]=note: print w/replies, xml ) Need Help??


in reply to Annoying warnings...

( reftype $x or '' ) eq 'HASH'

Has any of you, by any chance, found a more clueful approach to dealing with these annoying warnings?

Ugg — that's ugly, and quite obfuscated: it isn't apparent from glancing at that code exactly what it does or that it's written like that to avoid a warning. Creating a new block with that particular warning disabled at least has the advantage of making it absolutely clear what you're doing.

A clearer way of doing the above without a block is simply to test $x for being true (since all refs are true) first — this avoids the mysterious empty string constant and translates quite well into the equivalent English:

$x && reftype $x eq 'HASH'

(By the way I think it's slightly insulting of you to ask if any of us have suggestions for this "by any chance" — implying that we only discover things randomly rather than through having intelligence or talent!)

Smylers

Replies are listed 'Best First'.
Re^2: Annoying warnings...
by adrianh (Chancellor) on Jul 16, 2005 at 14:51 UTC
    Ugg — that's ugly, and quite obfuscated: it isn't apparent from glancing at that code exactly what it does or that it's written like that to avoid a warning

    I disagree. That's a common idiomatic way of resolving the problem of false/undef in some expressions and I see it in lots of code.

    The only problem with this strategy is that it sometimes gets used in places where non-undef false values like '0' shouldn't be ignored, which can lead to odd bugs. Not a problem in this instance.

    $x && reftype $x eq 'HASH'

    As already pointed out this breaks for true non-reference values.

    In this particular instance I'd probably make it a bit more explicit the exact thing I'm looking for:

    # I want a reference whose type is 'HASH' ref $x && reftype $x eq 'HASH';
Re^2: Annoying warnings...
by tlm (Prior) on Jul 16, 2005 at 13:07 UTC

    A clearer way of doing the above without a block is simply to test $x for being true (since all refs are true) first...

    Well, not quite. The code you proposed would still result in a warning if $x is 1, for example:

    % perl -MScalar::Util=reftype -we '$x=1; my $y=($x and reftype $x eq q +(HASH))' Use of uninitialized value in string eq at -e line 1.
    What needs to be tested is that the value returned by reftype $x is defined. So your solution should be cast as
    ...( reftype $x and reftype $x eq 'HASH' )...
    or, two avoid the duplicate calls to reftype:
    { my $temporary_variable = reftype $x; ...( $temporary_variable and $temporary_variable eq 'HASH' )... }

    Update: Fixed missing "$x and " in the one-liner.

    the lowliest monk

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (7)
As of 2024-03-28 08:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found