Stories
Slash Boxes
Comments

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

emazep (6092)

emazep
  (email not shown publicly)

I'm from Roma, Italy and, yes, I'm a male.

Journal of emazep (6092)

Thursday October 26, 2006
10:33 PM

ref never returns undef!

[ #31432 ]
Since I keep on finding here and there tests like this:

if (defined ref $thingy ...

let me point out that ref never returns undef. If its argument is not a reference, ref simply returns the empty string (even if called on undef!)

To have an idea on what I'm talking about (and on how common this redundant - and sometimes completely wrong - test is), have a look at here.
And to have a confirmation that a test like this is really superfluous (at best), have a look at the docs.

Update

Here is a slightly better crafted search - which searches just for the pattern defined\s*\(?\s*ref - which gives more results (though some of them are not pertinent to be honest).

Anyway thank everyone for the clarification about the origin of this meme (though now I have the same n1vux's curiosity ;-)

Ciao, Emanuele.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • I think it used to return undef on older Perls, which is where that meme came from.

    • It used to be documented as

      Returns a TRUE value if EXPR is a reference, FALSE otherwise.

      which could mean it returns "" or undef or alternates between them.
      • which means removing the defined() without putting in a require 5.8.0; would potentially create a compatibility regression, on any implementation/releases that actually returned undef when FALSE was documented?

        Do we know if it actually returned 'undef' or if it always returned q{} as it's FALSE (but defined) value?
        --
        Bill
        # I had a sig when sigs were cool
        use Sig;
        • Since undef is false, wouldn't if (ref $thingy work in all cases?
          • Sure it would. At least on the first page of results emazep's Code Search query, all code snippets take the form of

            if(defined ref $x && ref $foo eq 'X') ...

            This can be reduced to

            if(ref $x && ref $foo eq 'X') ...

            even if ref ever returned undef (which, last I heard, has always been considered false). Which it doesn't, even

            ref undef

            returns a true boolean, a value like !1. And then,

            if(ref $foo eq 'X') ...

            suffices. No warnings, now, ever. But maybe it did warn, in the past.

          • Well, not always.

            Under the false assumption that if the argument is not a reference ref returns undef, a test like this

            if (defined ref $thingy) { ... }

            to check if $thingy is a reference or not, would clearly be wrong, non just redundant (you can find an example of this here [google.com]).

            Anyway my main complaint was about the fact that, even if the code is formally correct (whereas the test is used to supposedly avoid the possible warning from a subsequent test), such defined-ness test is redundant, or unnecessary,

            • Update

              Please forget my previous pointless post: I wrongly assumed that DAxelrod was talking about

              if (defined ref $thingy) { ... }
              while he clearly said
              if (ref $thingy) { ... }
              Ciao, Emanuele.