in reply to Re^2: passing subroutine references
in thread passing subroutine references

Your solution to getting rid of complexity is to get rid of a local and add goto &_hanoi;?

Even a beginner needs to know local, whereas they wouldn't know goto &_hanoi;, and it adds debugging complexities at next to no benefit.

And at what cost? You've added a global var, you've made the private recursive function public, and you've nested the main function (which seems innocent, but I've found it to cause is to confuse me time and time again).

I beg to differ. I think you've taken 4 steps back.

Replies are listed 'Best First'.
Re^4: passing subroutine references
by Jenda (Abbot) on Sep 19, 2009 at 15:43 UTC

    Global? There ain't any global sir. Neither there is any nested function. And _hanoi(@_); would work just as well, while your tricks with local are unavoidable. Besides the problem is not the local itself, but the typeglob handling.

    What debugging complexities are you speaking of?

    Jenda
    Enoch was right!
    Enjoy the last years of Rome.

      Global? There ain't any global sir.

      Global to all calls to hanoi. External? Non-local? Yet highly coupled.

      And _hanoi(@_); would work just as well

      That's what I said.

      while your tricks with local are unavoidable.

      How is localising a var "a trick"? Dealing with package vars is a necessity in Perl, and the first thing you need to know is how to localise them.

      It is avoidable, but it's messy and requires a trick to avoid a memory leak.

      my $_hanoi; # \ Messy line break $_hanoi = sub { # / ... $_hanoi->(...); # Relatively messy syntax ... }; $_hanoi->(...); undef $_hanoi; # Messy hack to avoid mem leak.

      Using local actually cleans up a whole of mess.

      Besides the problem is not the local itself, but the typeglob handling.

      "The typeglob handling" makes "setting a typeglob" sound falsely ominous. It's a manufactured problem.

      What debugging complexities are you speaking of?

      The removal of a stack frame creates lying stack traces, not just to users but to debugging tools.

        It's static and lexical to two subroutines.

        And yes, localizing a var is a trick. Something that confuses most newcomers to Perl. Localizing a whole typeglob so that you could call the newly created closure as if it was an ordinary subroutine doubly so. If you insist on using local(), localize the $move_disk and define the _hanoi() as an ordinary subroutine accessing the package variable $move_disk.

        BTW, what was the last time you actually generated a stack trace?

        Jenda
        Enoch was right!
        Enjoy the last years of Rome.