in reply to Re^2: complaint: always testing defined()
in thread complaint: always testing defined()

OP is checking for defined-ness solely to avoid generation of the warning (if the variable being checked against the empty string happens to be undefined). To me, that is whole reason for existence of this thread (from OP)...

I find myself doing:
if (defined($params->{root}) && $params->{root} eq '')
to avoid a runtime warning.

Replies are listed 'Best First'.
Re^4: complaint: always testing defined()
by ysth (Canon) on Nov 04, 2007 at 03:29 UTC
    OP is checking for defined-ness solely to avoid generation of the warning
    Can I get the name of your crystal ball supplier?

    I would guess that the warning made the OP stop and think what branch to take in the undef case, and that the OP's code has a good chance of reflecting what was actually wanted.

      Quoting OP yet again (emphasis is mine) ...

      Always having to check if the variable is defined before testing its value. It just seems soooo unnecessarily verbose.

      I, personally, do get your point, and (would) have added the defined test myself.

      The urge to silence the uninitialized variable warnings becomes rather strong (in most limited scope, of course), however, when more than a couple of defined tests exists in close proximity just to avoid the damned warnings. Yes, that does prompt me to check why the things are not defined, which sometimes results in anything fruitful.

      But really, equality comparison of an undefined variable to anything else should just merrily produce the darned truth value without fuss.

        But really, equality comparison of an undefined variable to anything else should just merrily produce the darned truth value without fuss.
        Eww. That would be almost as bad as eq returning true, false, or undef (the latter if either operand is undef) ala SQL's NULL handling. I think the existing undef-becomes-""-or-0 is just fine.
Re^4: complaint: always testing defined()
by syphilis (Archbishop) on Nov 03, 2007 at 12:10 UTC
    Has no warnings 'uninitialized'; been mentioned yet ?
    Update: Ooops .. yes, it has. (Sorry, missed it.)

    Cheers,
    Rob