in reply to Re^7: Module Announcement: Perl-Critic-1.01 (scalar)
in thread Module Announcement: Perl-Critic-1.01
If you have a function that always returns exactly one item (as the list of items that it returns), then it doesn't make sense to change this function to return a zero-item list in one special case.That's not what return; does.
return; doesn't return a zero-item list? Yes it does and I know you know this.
It returns something that's false in boolean, scalar, and lists contexts.
I don't think there is a such a thing as "false in list context". The advantage of return; is that it makes a list assignment false (and that it makes an array that you assigned the return value to false). But I don't find myself doing such types of assigning a scalar to a list in a boolean context and when I consider that, I find it unnatural.
I don't find it unnatural to write:
my %results= ( foo => doFoo(), bar => getBar(), );
Where "foo" and "bar" are each the type of thing represented by a scalar value and one might fail to do "foo" or fail to get "bar". And it is convenient to be able to place in a scalar something that represents "oops, we failed to get this value" and that is why Perl has undef and I see no problem with using return undef; to represent a failure in a function that returns a scalar value (or to use return ''; to indicate failure in a fucntion that always returns a non-empty, true string value -- especially since it avoids gyrations to avoid "use of uninit val" warnings).
Damian's rule is Use a bare return to return failure. I believe that's good advice.
And I agree with that advice up to the point where you have a function that has always returned one scalar value in every other situation. I think I've covered rather thuroughly why I believe that. If the function usually returns just a scalar but sometimes doesn't, then I'd err on the side following that advice.
In another reply you write:
for those functions where their return value should be unambiguously false when it's false
And I find that return; breaks that very rule in quite natural code such as the above followed by:
if( $result{foo} ) {
or natural code like:
my( $foo, $bar )= ( doFoo(), getBar() ); if( $foo ) {
Your advice would give us "bar" (which isn't false) after doFoo() failed.
And I realize that when I write %hash= ( key => ... ) that "..." needs to be a scalar and so if someFunc() returns a list (that might have other than 1 element) then I need to write one of:
%hash= ( key => scalar someFunc() ); %hash= ( key => [ someFunc() ] ); %hash= ( key => 0 + someFunc() ); ...
But I would find it very unnatural to add to my mental model "if someFunc(), which is meant to return just a scalar, might want to return a failure indication then I have to put scalar() in because a failure indication can't be a scalar value (according to Damian)".
- tye
|
|---|