in reply to static storage in a local subroutine?

This facility would reduce the time it takes to write certain types of one-off scripts.

For example? I know this closure stuff is supposed to be useful, but I still don't quite grasp why...

  • Comment on Re: static storage in a local subroutine?

Replies are listed 'Best First'.
Re^2: static storage in a local subroutine?
by Moron (Curate) on Nov 23, 2005 at 14:20 UTC
    Given that scripts tend to grow as variations in the requirement come along, it is important that they are as maintainable as possible. The use of lots of global variables for static storage causes maintenance problems because the code and variables are not in the same place, creating the need to have to search a growing amount of code to solve functionality coded in a localised place. Encapsulation is key to why O-O solves complexity and closure is a related concept.

    -M

    Free your mind

      Yes, it's beginning to make sense: "Barrie Slaymaker calls closures "inside-out objects:" objects are data that have some subroutines attached to them, and closures are subroutines that have some data attached to them."

      sub make_counter { my $start = shift; return sub { $start++ } } my $from_ten = make_counter(10); my $from_three = make_counter(3); print $from_ten->(); # 10 print $from_ten->(); # 11 print $from_three->(); # 3 print $from_ten->(); # 12 print $from_three->(); # 4
      This example helped me understand a lot better.