Specifically, this code:
returned a message that defined() was deprecated.#!/usr/bin/perl -w use strict; my %hash; print defined( %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.#!/usr/bin/perl -w use strict; my %hash; print ( %hash )?"yea":"nay","\n";
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:
(despite the lameness) will now break in select cases because of this change.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"; }
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?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
(tye)Re: Replacement for defined(%)/defined(@)?
by tye (Sage) on Jun 29, 2001 at 21:53 UTC | |
by Masem (Monsignor) on Jun 29, 2001 at 23:24 UTC | |
by tye (Sage) on Jun 29, 2001 at 23:41 UTC | |
by John M. Dlugosz (Monsignor) on Jun 29, 2001 at 23:35 UTC |