Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

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?
          • by emazep (6092) on 2006.10.28 7:01 (#51337) Journal
            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, or superfluous (pick your favorite), since ref never returns undef.

            In other words I mean that there is no reason to say

            if (defined ref $thingy && ref $thingy eq 'HASH') { ... }
            since
            if (ref $thingy eq 'HASH') { ... }
            gives no warnings as well (at least today), and it is less cluttered and more readable (and a little bit faster too ;-)

            Ciao, Emanuele.

            • 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.