What is in The End?

Well a good day today. Had an interesting though come by my desk. Was debugging a problem with a colleague and in our back and forth he came up with the little jape'
'I prefer to never return anything from a perl subroutine!'
I could of course dive into the often fought and confusing battle of the difference between a function or a subroutine, but that dead horse has been done over like yesterday's poutine gravy. We could of course take a vote on it but I think it would be this sort of reaction in the community?
Anyway on with my story I did a little digging and was surprise to discover that in perl a subroutine always returns a value. This goes back to day 1 when Larry was first playing with his disruptive technology, If I am remembering Lary's talk coorecty he was just lazy and put it this way 'why should I have to add in the code for a "return" syntax if I don't really need to'. So since the dark ages perl subroutines all-ways return the last evaluated expression. So I guess my colleague should amend his jape to
"I never deliberately return something from a perl subroutine!"
Of course this perl gem can always come up and bite you in the ass. Something like this comes to mind
sub blog_total_hits{ my $self = shift; my ($today,$current) = @_; $today + $current warn "Checking blog hits" if ($self->debug); }
I would would be rather disappointed in my hit counter for the day. Besides an obvious goof up like the above example it really just boils down to a question of style. I myself go by the old adage
Return Early! Return Often!
Which led the above discussion in the first place as I did a little change to a sub
sub fix_sql_for_pg { my $self = shift my ($sql) = @_; ++ return $sql ++ unless ($self->isPostgres); -- if ( $self->isPostgres){ ... imagine code to mung SQL here -- } return $sql;
which to me was a little change that Daiman would be proud of. I guess it just boils down to what style you like.


Larry Wall may or may not have been ahead of his time.

PBP calls for functions/methods to return explicitly, so I always try and get my team to do that. The reasons given in the book seem fair enough, or would have been fair enough in 2006.

However, there was a lot of noise about Scala in the Perl community about a year ago. As a result I, like many Perl programmers, have been idly looking at it.

Scala also returns the last value if you don't return explicitly. However, that's beeen designed in for a specific reason.

Doing an implicit return (among other things) in Scala allows for tail recursion and massively reduces memory usage during deep recursion.

Does anybody know if Perl optimises using tail recursion?

I suspect it doesn't, but it would be spooky if it did. It would show how advanced Perl was when it came out.

Perl has trouble with tail recursion because it can't always tell if the subroutine name references the same code.

But, good programmers don't need recursion. :)

What's wrong with recursion?

A look at recursion.

Leave a comment

About byterock

user-pic Long time Perl guy, a few CPAN mods allot of work on DBD::Oracle and a few YAPC presentations