in reply to passing arguments in a dispatch table
I think that you are looking at a closure. As such, data are available as lexicals in the context in which your anonymous sub is defined. Usually you will want $fruit to go out of scope deliberately.
Also, add_entry does nothing with its argument, hence you would not be able to observe a difference:
Warning: untested code
#!/usr/bin/perl -w use strict; $|++; my $dispatch; { my $fruit = [ qw|apple orange pear| ]; $dispatch = { add => sub { return add_entry( $fruit ) }, }; } print scalar localtime, "\n"; sleep 1; print &{ $dispatch->{add} }, " <-- from sub \n";; sleep 1; print scalar localtime, "\n"; sub add_entry{ print scalar localtime, "$_[0] <-- in sub \n"; sleep 1; return scalar localtime; } __END__
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: passing arguments in a dispatch table
by CharlesClarkson (Curate) on Apr 29, 2002 at 02:45 UTC | |
by abstracts (Hermit) on Apr 29, 2002 at 03:12 UTC | |
by CharlesClarkson (Curate) on Apr 29, 2002 at 06:06 UTC |