Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Perl: last evaluated value as a returned value for a subroutine. See inside

by swkronenfeld (Hermit)
on Feb 21, 2013 at 17:02 UTC ( #1019984=note: print w/ replies, xml ) Need Help??


in reply to Perl: last evaluated value as a returned value for a subroutine. See inside

Not commenting on the exact behavior of Perl, but I have a coding style suggestion.

Perl's feature of returning the last evaluated value can be handy when you're writing quick code (or perhaps when you're golfing). But the code you wrote above is not very clear. Hand that to other programmers and I bet many won't know the return value - even plenty of Perl veterans.

Just because you can do something doesn't mean you should. Write more explicit code. For everyone's sake (including yourself a week/month/year in the future).


Comment on Re: Perl: last evaluated value as a returned value for a subroutine. See inside
Re^2: Perl: last evaluated value as a returned value for a subroutine. See inside
by sundialsvc4 (Abbot) on Feb 21, 2013 at 17:38 UTC

    Very strongly agree.

    The problem with a more “lackadaisical” coding style is that it is not immediately apparent what the actual return-value will be ... let alone what it was intended to be.   Even in the case at bar, we see this:   Perl did exactly what it is programmed to do, but the author mistook what that would turn out to be.   (The next person to come along, now frantically trying to find the obscure bug that is torching a production system, is even more “sunk.”)

    If a function returns a value, you should always IMHO return $some_value;.   If the function makes decisions such that it could have more than one “intended result,” put an appropriate return statement at each point.   Not only is there no good reason to let control “dribble down” to the endpoint of the routine, but a subtle and unexpected bug might be introduced if additional code were added at the end of the routine.   Code for clarity.   Hair-follicles are a precious thing.

Re^2: Perl: last evaluated value as a returned value for a subroutine. See inside
by Anonymous Monk on Feb 22, 2013 at 00:38 UTC

    won't know the return value - even plenty of Perl veterans.

    doubtful , this knowledge is even more basic than scalar/list context

    perldoc -f return

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1019984]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2014-12-26 23:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (176 votes), past polls