|
Posted by Dominique Dumont on February 6, 2007, 3:34 am
Please log in for more thread options
> Thanks Dominique!
You're welcome.
> ie, given myvar and mysub reside in your perl program
>
> $myvar="hello world";
>
> mysub{
> ($greeting) = @_;
Beware, here, $greeting is a global variable. You should use a
lexical variable:
my ($greeting) = @_ ;
> print $$greeting;
> }
[snip]
> you can have a perl module (or widget) directly access these items
> without making any copies of anything by first passing the references:
>
> -command => [\&mysub,$myvar]
>
> and having the receiving perl module (or widget) use the references to
> execute the external code in some way:
>
> modsub=shift; #captures the ref \&mysub
> modvar=shift; #captures the ref $myvar
You forgot the $ (and probably the lexical declaration):
my $modsub=shift; #captures the ref \&mysub
my $modvar=shift; #captures the ref $myvar
> &$modsub($$modvar); #call the external mysub method and pass it hello
> world
This can also be run with this syntax:
$modsub -> ($$modvar) ;
> The &$ and $$ are called dereferencing operators, and they can also be
> used anywhere to "execute" a reference wherever it appears.
>
>
>
> There are, however, some Perl Modules that execute a copy of
> referenced code (by value) rather than doing it directly (by
> reference).
>
> These use another CPAN module called B::Deparse that is essentially a
> Perl de-compiler. Given a reference, Deparse can expand the tokens it
> finds there into Perl code using its coderef2text function:
>
> use B::Deparse;
> $deparse= B::Deparse->new();
> modsub=shift; #captures the ref \&mysub
> $modcode=$deparse->coderef2text(modsub); #modcode now contains mysub
You forgot the $ (and probably the lexical declaration):
use B::Deparse;
my $deparse = B::Deparse->new();
my $modsub = shift; #captures the ref \&mysub
# modcode now contains mysub listing
my $modcode = $deparse->coderef2text($modsub);
> The tk::scheduler does this because it persists scheduled tasks, even
> when it is shut down and restarted again at a later date. If the
> scheduler stored the addresses to scheduled code, those addresses
> could be invalid the next time it was reloaded back into the
> interpreter. Instead, it stores a copy of the subroutine to be
> executed, which is why I encountered all the previous access problems
> to subroutines other than my main one - they weren't included in the
> deparser decompile.
I hope there's a big warning in Tk::Scheduler doc. If not, I think you
should send a patch to its author.
Cheers
--
Dominique Dumont
"Delivering successful solutions requires giving people what they
need, not what they want." Kurt Bittner
|