Stuff with the Perl Foundation. A couple of patches in the Perl core. A few CPAN modules. That about sums it up.
In code which sometimes returns 'false', I keep seeing code like the following:
sub foo {
return undef unless bar(@_); # or 'return 0;'
return $something_else;
}
That behavior needs to be documented very carefully as it has a subtle bug which people keep stumbling over. Most of the time it works:
if ( my $result = foo() ) {
... }
But what if, for example, you want to accumulate results so you decide to use an array?
if ( my @results = foo() ) {
... }
Congrats! If &foo return false, you now have a one-element array which, in this context, evaluates as true. This is very likely a bug. You can accomplish the same thing, probably bug free, if you just use a bare return:
sub foo {
return unless bar(@_);
return $something_else;
}
From perldoc -f return:
If no EXPR is given, returns an empty list in list context, the undefined value in scalar context, and (of course) nothing at all in a void context.
In other words, if you wish to return 'false', a bare return will Do The Right Thing.
The only significant objection this I can recall hearing is the following:
if ( some_func(1, foo(), 2) ) {
... }
Because that's in list context, if &foo has a bare return, &foo returns the empty list and &some_func receives (1, 2). I don't see this happening too often, but you can get around it by forcing scalar context.
if ( some_func(1, scalar foo(), 2) ) {
... }
Now &some_func will receive (1, undef, 2).
Also, if you're returning from a ternary operator, you can get the same behavior with this:
return $some_val ? $some_val : ();
Opposite Bug (Score:1)
We recently hit against a bug with this the opposite way round. The
JSON [cpan.org]module'sobjToJsonfunction takes a Perl data structure and returns a Json string representation of it. But if the input isundefthen it uses barereturn.The bug was that this return value was being used in the parameter list of another function, and the bare
returnmeant there was a argument missing.Once we'd spotted the bug it was simple to put
scalarbefore the call toobjToJson, but it was subtle. If a function is documenRe: (Score:2)
You know, I did cover that specific case in my post :)
Perl::Critic enforcement (Score:1)