AKA - Annoyed by default return
Perl sometimes annoys me.
A couple of weeks ago it was the fact that a my variable declared in a if (...)
block is still valid in the same block's elsif (...)
section, meaning that you get warnings if you do:
if (my $val = $tiedhash{$key}) { ... } elsif (my $val = $othertiedhash{$key}) { ... }This vexxed me greatly.
use strict 'return'
" which would turn off this ugly behaviour.$self->{status}
value written to the socket.I get a lot of milage out of implicit return
– I write many functions that consist of a single expression (disregarding parameter parsing), which read much nicer that way.
Maybe precisely because I use it so purposefully, I am always keenly aware that subs always return something, and so when writing subs that aren’t supposed to, I usually put a bare return
in there.
Re:But I like it
Matts on 2006-10-29T14:15:36
I do this too, as can be seen in my code on CPAN. I just would like a strict mode to turn it off.
Re:Default behavior
Matts on 2006-10-29T14:14:34
As I said, it was partly due to the dumb API, but often times the last statement is hidden somewhere in an if block, or while block or something. I think I'd personally rather have an explicit return, via use strict 'return';Re:Default behavior
sigzero on 2006-11-03T02:53:31
I think I'd personally rather have an explicit return, via use strict 'return';I get what you are saying now.
The fact that blocks return the last expression evaluated is what makes writing concise maps and greps.
Also note that whether a subroutine returns something or not is not determined by a return, or a last expression. Like anything else, it's determined by context. A subroutine will return nothing, if, and only if, it's called in void context. Otherwise, it will return something, even if it's an empty list (in list context), or the undefined value (in scalar context).
Having said all this, I fail to understand why you can be bitten by Perl returning something from an subroutine without it having an explicit return statement. It sounds to me that you have assigned the return value of a subroutine to a variable - and you have done that with a subroutine you expected to return "nothing". Surely the mistake lies there - and not in the fact the sub returned something after all.
Re:It's not the subroutine, it's the block.
Matts on 2006-10-30T13:51:40
It's an OO API, so you subclassprocess_line
to process a line of data. But I forgot that (somewhere in another file/class) that if process_line returns a scalar it gets sent to the client. It's hard to see that sort of thing in OO virtual APIs sometimes, but as everyone has rightly pointed out - it was programmer error.
Doesn't mean I think "use strict 'return'" is any less of a good idea though.Re:It's not the subroutine, it's the block.
Dom2 on 2006-10-30T20:57:25
I've been bitten by this in mod_perl handlers. I forgot to return OK, and the final statement was returning undef. That triggered a "Use of uninitialised value" message in the error log... but it had no line number info. That took me months to track down. I've always been very explicit about using return since then.-Dom