reutun undef

reutun undef

am 25.03.2010 05:07:34 von Jeff Peng

>>> "return" statement with explicit "undef" at line 185, column 9. See page 199 of PBP. (Severity: 5)

I got the cpan test report, it pointed out the one above.
Is "return undef" not to be encouraged in current Perl?

Thanks.

--
Jeff Peng
Email: jeffpeng@netzero.net
Skype: compuperson

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: reutun undef

am 25.03.2010 05:15:29 von jwkrahn

Jeff Peng wrote:
>>>> "return" statement with explicit "undef" at line 185, column 9. See page 199 of PBP. (Severity: 5)
>
> I got the cpan test report, it pointed out the one above.
> Is "return undef" not to be encouraged in current Perl?

Only if you want to explicitly return a single scalar value irrespective
of context. Using return with no value will respect context and return
undef in scalar context and an empty list in list context.



John
--
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity. -- Damian Conway

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: reutun undef

am 25.03.2010 05:27:38 von Uri Guttman

>>>>> "JP" == Jeff Peng writes:

>>>> "return" statement with explicit "undef" at line 185, column 9.
>>>> See page 199 of PBP. (Severity: 5)

JP> I got the cpan test report, it pointed out the one above.
JP> Is "return undef" not to be encouraged in current Perl?

this has been debated in dark corners by many perl hackers. i am on the
side of good which is don't use return undef in most cases. let me
outline my reasoning behind this.

first off, most uses of return undef are boolean. plain return will
return an undef in scalar context and an empty list in list
context. both of those are false which is what you want. but if you
return undef in a list context:

sub bad { return undef }

@results = bad() ;

if ( @results ) {
do something ;
}
else {

handle error ;
}

which branch is taken?




the other (evil side) claims that if you do a plain return() where you
want data, then it can screw up a hash pair or list where something is
needed:

%hash = ( foo => get_foo(), bar => $bar ) ;

now if get_foo is this:

sub get_foo { ... return ; }

that will return an empty list which will make the list look like this:

( foo => 'bar', $bar )

which is an odd count which is likely a problem and will trigger a
warning.

but the issue is control of the return value. the caller can force an
undef value if they want with the scalar() call.

%hash = ( foo => scalar get_foo(), bar => $bar ) ;

that will ALWAYS work regardless of whether get_foo does return() or
return undef. and it tells the reader of the code that you know you want
a scalar there to fill in the slot.

so my point is that return() is better as it lets the caller of the code
control what gets returned but return undef doesn't give that option.

as for the above question, it will execute the do something branch since
@results will be assigned ( undef ) which is TRUE as it is an array with
one element in it. in boolean context (which is a subset of scalar
context), arrays give their count which is 1 in this case. an empty
array (as return() would return) would have 0 elements and is false as
you want.

i use this in some code where i want to do nothing when no values are
returned and something when data is returned. and undef is a legal value
to save. so i call that sub and assign it to an array. then i can tell
the difference between return() and return undef.

i have had this discussion too many times. it is good to write up this
short essay on it.

hope that helps,

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: reutun undef

am 25.03.2010 10:07:59 von Jeff Peng

Thanks all.
for "return ()", does it mean return an empty list, or return with no argument?


--
Jeff Peng
Email: jeffpeng@netzero.net
Skype: compuperson

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: reutun undef

am 25.03.2010 14:13:37 von derykus

On Mar 25, 2:07=A0am, jeffp...@netzero.net (Jeff Peng) wrote:

> for "return ()", does it mean return an empty list, or return with no arg=
ument?

return() is equivalent to return with no argument.

--
Charles DeRykus


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: reutun undef

am 25.03.2010 17:34:37 von Uri Guttman

>>>>> "JP" == Jeff Peng writes:

JP> Thanks all.
JP> for "return ()", does it mean return an empty list, or return with no argument?

return ; and return() are the same thing. whether it returns an empty
list or undef is determined by the caller's context.

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/