Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

help with scoping (code), part 1

by deprecated (Priest)
on Nov 04, 2001 at 01:54 UTC ( #123089=perlquestion: print w/replies, xml ) Need Help??

deprecated has asked for the wisdom of the Perl Monks concerning the following question:

i'm trying to compartmentalize an api that i am writing. im doing something like this:
my @children = ( \&sub_one, \&sub_two, ); foreach my $child (@children) { if ($child -> (args)) { # $child was successful } else { # $child was unsuccessful } }
the problem is, if sub_two is a help sub, i need it to be able to call all of the subs in @children, including itself.

i suspect making my @children into our @children, i would solve my problem, but that strikes me as rather inelegant.

how can i make my children able to see eachother?

brother deprecated

Laziness, Impatience, Hubris, and Generosity.

Replies are listed 'Best First'.
Re: help with scoping (code), part 1
by Fastolfe (Vicar) on Nov 04, 2001 at 03:20 UTC
    I'm not sure I parsed your question correctly. How do you tell if sub_two is a "help" sub? What is a "help" sub? What do you need to call all of the subs in @children? This anonymous subroutine in sub_two? And when you say "all of the subs", you mean sub_one and sub_two, yes?

    So basically, the problem is that you need this subroutine sub_two to be able to access @children, yes?

    In your other question, a poster discussed the use of a simple block to wrap around your functions:

    { my @children = ( ... ); sub sub_two { ... } foreach my $child (@children) { if ($child->(...)) { ... } else { ... } } }
    So long as the subroutine in question is declared within the same lexical scope as the variable, you're OK. This may mean you need to move 'my @children = (...)' higher up in your file, or use our instead of my as you suggest.

    After that, you can execute these subroutines in any fashion, and they'll still end up seeing the same variables in the same lexical scope.

Re: help with scoping (code), part 1
by larryk (Friar) on Nov 04, 2001 at 02:12 UTC
    IMHO not as inelegant as use vars '@children'; looks :)

    ...or you could just pass in @children as an extra argument

    perl -le "s,,reverse killer,e,y,rifle,lycra,,print"

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://123089]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2023-06-01 16:03 GMT
Find Nodes?
    Voting Booth?

    No recent polls found