I'm looking at the following code snippet:
my @ret = <someMethod>
return (undef) if( $DB_ERROR );
return (undef) unless ($#ret >= 0);
Does $#
just give you a count of elements in a array?
I'm looking at the following code snippet:
my @ret = <someMethod>
return (undef) if( $DB_ERROR );
return (undef) unless ($#ret >= 0);
Does $#
just give you a count of elements in a array?
$#arrayname
gives you the index of the last element, so if array @ret
has 2 elements then $#ret
is 1.
And, as noted by Barry Brown, an empty array gives -1.
To get the length you can use the array in scalar context:
print scalar @ret;
Be aware that the $#array expression will return -1 when array has zero elements.
edg is correct, but the original code is unnecessarily obtuse. In most cases, $#foo
is a red flag that the code could be written more simply using scalar @foo
.
return (undef) unless ($#ret >= 0);
unless foo >= bar
is difficult to puzzle out. First, turn it into a positive statement.
return (undef) if ($#ret < 0);
When is $#ret < 0? When it's -1. A $#ret of -1 is an array of length 0. So the above can be written much more simply as...
return (undef) if scalar @ret <= 0;
But you can't have a negative length array, so...
return (undef) if scalar @ret == 0;
And == is in scalar context, so that "scalar" is redundant...
return (undef) if @ret == 0;
But that's just a wordy way of saying "if @ret
is false".
return (undef) if !@ret;
Which I think for simple statement modifiers is better expressed with unless.
return (undef) unless @ret;
Isn't that easier to follow?
As a final side-note, return undef
is discouraged because it does the wrong thing in list context. You get back a list containing one undef element, which is true. Instead, just use a blank return which returns undef in scalar context and an empty list in list context.
return unless @ret;
To summarize everyone else, that code is much more legible if written like this:
my @ret = someMethod();
return if $DB_ERROR;
return unless @ret;