in reply to memory leak when using tail recursion?

See perldoc -f goto on your system.

Goto with a subroutine reference isn't a goto in the traditional sense. You need to use a label. They way you are using it is identical to calling bar().

  • Comment on Re: memory leak when using tail recursion?

Replies are listed 'Best First'.
Re^2: memory leak when using tail recursion?
by Limbic~Region (Chancellor) on Feb 14, 2007 at 00:05 UTC
    Cabrion,
    Perhaps you aren't familiar with what tail recursion is. You must use this specific form of goto for tail recursion which is the intent of the post. Unfortunately, it is leaking in a very specific case. I don't know why but you may want to check out Pure Perl tail call optimization as your post isn't really helpful.

    Cheers - L~R

      Sorry, and I'm not trying to start an argument by way of reply. In other languages goto is a jump. In perl I recalled that goto &foo wasn't "traditional" goto, I assumed (incorrectly) that calling it this way instead of implementing with a line label was the source of the leak. The docs say that localized variables are cleared, but says nothing about clearing anything else. Just some weird magic used for AUTOLOAD.

      In the end, your are correct that I'm not even a novice in tail-recursion, and probably should have kept my mouth shut, or at least qualifed my post.

        Cabrion,
        No worries. In languages that have tail recursion optimizations built in, there is no need for goto at all (just have the sub call itself). This is mostly the case for functional languages like Haskell. In perl, trying to achieve this yourself isn't much of a gain. It does avoid the deep recursion warning though.

        This type of optimization falls into the category of unrolling loops. If your language doesn't do it for you, trying to do it yourself can be prone to error. If you are interested in computer science, reading the links I sent may be of use.

        Cheers - L~R