(See update to this post at bottom)
I have a database full of subroutines that I import into applications. The code is eval'd and stored into a hash, which is then tied to a scalar. When the user references the scalar, FETCH returns the subref so the user can execute it.
One of the nice things about this setup is that I can "call" a subroutine with the tied variable, but have other events occur prior to execution. For example, a user can pause execution of the code by tripping a button on a web form. When FETCH is called, it checks the pid for the routine against a database of active pids to see if the program has been paused. If so, it delays execution of the subref in the hash until the pause is released. Kinda neat!
Problem is, when I access one of the tied variables inside a for loop, the FETCH doesn't run. I've included a sample below. Can somebody tell me what's going on?
Thanks in advance!
my $r = { subref => sub { return 2 * $_[0] } };
my $tied;
tie $tied, 'TieTest', $r;
# This will perform the pre-action before executing
print $tied->(5);
# These will not
for(0..100){
print $tied->(5), "\n";
}
package TieTest;
sub TIESCALAR{
my $caller = shift;
my $r = shift;
bless $r, $caller;
}
sub FETCH{
my $r = shift;
print "Checking for pause...\n";
return $$r{subref};
}
** UPDATE:
EVEN STRANGER BEHAVIOR...
This code works:
# This will perform the pre-action before executing
print $tied;
# These will not
for(0..10){
print $tied, "\n";
}
This prints the "extra" message plus a "CODE(0x224e2c)". So FETCH only fails when I attempt to dereference the returned subref.
Dragonchild's $r->{subref} = $r->{subref} fix doesn't work, but I need that type of transparency to make this work in my case. Any ideas?
** END UPDATE
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.