While programming stuff last night, I ran into something that correlated with this node on return values & exceptions that caught me a bit off guard.

Specifically, this code:

#!/usr/bin/perl -w use strict; my %hash; print defined( %hash )?"yea":"nay","\n";
returned a message that defined() was deprecated.
This was in 5.6.1; this is not the case for 5.005. Flipping to the recent docs for this, I discover this is truely the case, and that you are encouraged to use:
#!/usr/bin/perl -w use strict; my %hash; print ( %hash )?"yea":"nay","\n";
which doesn't seem to terrible in itself. This only applies to it's use for hashes and arrays (references and scalars are quite happy with defined()). But consider that one of the issues in the aforementioned node was what to return; my personally style is that if there's a non-critical function, I'll return undef if the error occured, otherwise what is needed; this is similar to how many other perl built-ins work.

However, consider a function that can return a hash or array; both of these may be empty due to how data is stored or the like; for example, in my Tie::Hash::Stack, the hash is initially tied to a blank hash; this is still valid, but doing a test such as if ( %hash ) ... will fail with the empty hash; same can happen to a purposely empty array. In these cases, I'd expect that an empty hash or array reflects that no errors were encountered and the hash/array created successfully, while undef would tell me that there was a problem. For example:

sub all-between { my ($min, $max ) = @_; if ( $min > $max ) return; my @array = (); my $i = $min + 1; while ($i < $max) { push @array, $i++; } return @array; } my @ar1 = all-between( 4, 10 ); # Gives (5,6,7,8,9) my @ar2 = all-between( 4, 5 ); # Gives () my @ar3 = all-between( 4, 3 ); # Gives undef if ( my @ar = all-between( $arg1, $arg2 ) ) { # as opposed to: # if ( defined( my @ar = all-between( $arg1, $arg2 ) ) ) { print "Your allbetween are " , join( ',', @ar ), "\n"; }
(despite the lameness) will now break in select cases because of this change.

Thus, the question is, is there a valid replacement for defined() for arrays and hashes, or will we need to add an extra step of logic for these types of situations?


Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain

In reply to Replacement for defined(%)/defined(@)? by Masem

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.