Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re^2: Using & in function calls

by demerphq (Chancellor)
on Oct 02, 2007 at 08:30 UTC ( #642062=note: print w/ replies, xml ) Need Help??

in reply to Re: Using & in function calls
in thread Using & in function calls

... or b) I want to do goto &subname; (which is an even more rare, but does have its uses in that it's the only way to "fake tailrecursion" in perl - see goto)

Actually thats not correct, it is neither the only nor the best way to "fake tailrecursion". The best way to fake tail recursion is to use a while or for loop. The overhead of goto &sub is actually quite expensive compared to a proper loop.

Tail recursion optimisation is only really important in languages which lack looping constructs. Yet the meme that "tail recursion optimisation is cool and useful" somehow manages to leak out of languages that really do need it into languages that don't really need it. And Perl has a rich set of looping constructs making tail recursion and its optimisation much less important than people think. This is actually one of the reasons that perl doesnt bother with this optimisation. (Its not the only reason tho given Perls dynamic nature.)

Remember that all tail recursion optimisation does is quietly convert your tail recursive call into a loop.


Comment on Re^2: Using & in function calls
Re^3: Using & in function calls
by Joost (Canon) on Oct 02, 2007 at 21:12 UTC
    Well, yes.

    But, it can be useful to have the called subroutine determine the next call instead of moving the determining code to the calling routine. And doing that "naively" will lead you running out of call stack if you have enough nested calls.

    I've used the goto &subroutine; construct to get out of that situation.

    I acknowledge that this isn't strictly tail recursion.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (12)
As of 2014-07-23 19:23 GMT
Find Nodes?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:

    Results (152 votes), past polls