>>>>> "JP" == Jeff Peng <jeffp...@netzero.net> 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? <answer below> 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 ------ u...@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-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/