Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re^5: Who's a thief? (recursion limitations)

by Limbic~Region (Chancellor)
on Jan 14, 2005 at 19:27 UTC ( #422356=note: print w/replies, xml ) Need Help??

in reply to Re^4: Who's a thief? (recursion limitations)
in thread Who's a thief? -- the follow up

Every time Perl enters a sub (normally), it creates a new stack frame and this quickly eats memory.

Presumably, you are referring to &sub as the abnormal example where you could use goto &sub to bypass the recursion limit as seen in the following example:

#!/usr/bin/perl use strict; use warnings; print sum( 1000 ); sub sum { my ($num, $tot) = @_; return $tot if ! $num; @_ = ($num - 1, $tot += $num); goto ∑ }
According to TimToady, this actually does create a new stack, but only after the old one is stripped off. Of course you could always ignore the warning, turn off the 'recursion' warning, or recompile Perl to avoid the warning. In practice, I have never needed this kind of technique because it seems that iterative solutions aren't that difficult to come up with. In this case, the formula (n^2 + n) / 2 would do the trick.

And as I am previewing this, I see BrowserUk has already said something similar - oh well.

Cheers - L~R

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (4)
As of 2020-02-24 21:34 GMT
Find Nodes?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?

    Results (107 votes). Check out past polls.