temporal has asked for the wisdom of the Perl Monks concerning the following question:
I am cleaning up some scripts and ended up pulling an entire script with its subroutines into another script - as a subroutine. Something like:
use strict; sub external { my $foo = some_op(); sub sub_of_sub { print $foo; } }
Like in the example, I have to keep these subroutines in scope so that some variables that are defined in the outer subroutine are available.
I don't want to make these variables global. I have a lot going into this script and I'm trying to keep things organized. I want all of my globals to be things that are used by more than one subroutine.
Some more background:
Essentially, I am writing a module that contains a collection of 'tasks'. A superscript to this one will be importing and calling these tasks individually and for different times, triggers, etc. This is one module of many. Each module will contain the same 'tasks' (same named subroutines) such that my superscript can iterate through them all easily.
Anyway, I checked out Defining a sub within a sub: OK?, but I'm not sure if it exactly answers my question. Why are anonymous subs more acceptable? I just want to get some feedback/suggestions since this seems like something that might be considered... poor Perl form.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Refactoring: sub within a sub
by GrandFather (Saint) on Jul 25, 2012 at 23:41 UTC | |
Re: Refactoring: sub within a sub
by moritz (Cardinal) on Jul 25, 2012 at 19:38 UTC | |
Re: Refactoring: sub within a sub
by tobyink (Canon) on Jul 25, 2012 at 19:44 UTC | |
Re: Refactoring: sub within a sub
by temporal (Pilgrim) on Jul 25, 2012 at 20:56 UTC | |
by tye (Sage) on Jul 25, 2012 at 22:46 UTC | |
Re: Refactoring: sub within a sub
by Anonymous Monk on Jul 26, 2012 at 06:51 UTC |