On Thursday 06 May 2010 13:24:10 Harry Putnam wrote:
> Philip Potter <philip.g.pot...@gmail.com> writes:
> > On 5 May 2010 17:29, Harry Putnam <rea...@newsguy.com> wrote:
> >> Anyway, I understood he was saying NOT global.
> >> 
> >> What I asked is why that would matter.  That is, the values or
> >> elements in @_ arrive inside the `sub dispt {...}', so should be
> >> available to anything inside `sub dispt {...}'  right?
> >> 
> >> And `%hash = (...)' is inside `sub dispt {. %hash = (...)..}'
> >> 
> >> I do get confused often about how scope works.
> > 
> > Did you read the second part of my message where I addressed this very
> > issue? You haven't quoted it or answered it so I have nothing to say
> > other than to ask you to read it again and tell me what you still find
> > confusing.
> 
> I'm sorry, I skipped right over it somehow. It appeared you only
> commented on what Uri had to say.  I didn't scroll far enough, so it
> appeared it was just the quoted code below you comments about `FF'.
> 
> That explains whats happening better.  And I see now... I'm going
> about this all wrong. Nesting isn't really what I'm after.
> 
> Looks like it would be better to just rely on global variables to
> supply needed information inside whatever subroutines are called in
> the dispatch table.  In fact someone suggested just that
> earlier... and I didn't follow what they were talking about.

Using global variables to pass arguments into descended functions is almost 
always a very bad idea, because it messes up with recursion, with multi-
threading (which is not an issue in Perl), and is not elegant. I suggest you 
instead do something like this:

sub dispatch
{
        my ($method, @rest_of_args) = @_;

        my %dispatch =
        (
                'N' => sub { return N_func(@rest_of_args); },
                'L' => sub { return L_func(@rest_of_args); },
                .
                .
                .
        );

        return $dispatch{$method}->();
}

You can also call the dispatched method with some arguments to assign stuff to 
its @_. And you may also wish to investigate objects and object-oriented-
programming as a way to pass along arbitrary context along the way:

http://perl-begin.org/topics/object-oriented/

There's also Aspect-oriented programming which allows for a certain "wormhole 
pattern", but this is very advanced stuff and something even I still could not 
keep resident in my head:

http://search.cpan.org/dist/Aspect/

But nevertheless, there are better ways than to assign to a global variable 
and have each function read from it.

Regards,

        Shlomi Fish

-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
Freecell Solver - http://fc-solve.berlios.de/

God considered inflicting XSLT as the tenth plague of Egypt, but then
decided against it because he thought it would be too evil.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to