|
Posted by Andy on January 31, 2007, 2:05 pm
Please log in for more thread options
I'm using the CPAN Tk::Scheduler module to time control the execution
of subroutines within my Perl script. I want to display the results
of a scheduled task in a tk text widget. But, when my subroutine gets
invoked by the scheduler via the -command switch, the called
subroutine itself seems to be unable to talk to any Perl::Tk widgets;
all such calls within the subroutine hang. Everything else seems to
work ok.
Does anyone know why? Do I have to call some sort of "refresh" tk
command or release tk resources?
use Tk;
use Tk::Schedule;
my $mw = MainWindow->new;
my $s = $mw->Schedule(
-interval => 60,
-repeat => "once",
-command => [\&run, "junk"],
-comment => "1-9999"
)->pack();
my $txtbox=$mw->Text(-width => 79,
-height => 8,
)->pack();
$txtbox->insert(end, "this works\n");
MainLoop;
sub run{
print "start run\n";
$txtbox->insert(end,"this just hangs\n");
print "exit run\n";
}
|
|
Posted by Andy on January 31, 2007, 3:14 pm
Please log in for more thread options
Some further investigation shows that the tk::scheduler executes the -
command switch by executing the assigned code throgh a Perl eval()
statement.
I suspect what is happening is that the Perl code inside the eval() is
out of scope to variables outside the eval statement (including the
scheduler.pm module, and the parent Perl program utilizing the
scheduler).
|
|
Posted by AC on February 1, 2007, 9:01 am
Please log in for more thread options
> Some further investigation shows that the tk::scheduler executes the -
> command switch by executing the assigned code throgh a Perl eval()
> statement.
>
> I suspect what is happening is that the Perl code inside the eval() is
> out of scope to variables outside the eval statement (including the
> scheduler.pm module, and the parent Perl program utilizing the
> scheduler).
>
Thank you for following up on your own posting with some insight. About 50%
of postings are useless when googling because the author never followed up
with an answer.
Allan
|
|
Posted by Andy on February 1, 2007, 11:05 am
Please log in for more thread options
I wish I could get it working...
The problem seems to have something to do with Perl references
(covered by the Perlref doc that comes with ActiveState). The
documentation says that in terms of capabilities, all memory within a
Perl program is referencable by anything (both perl modules and
calling programs have equal and full access to each other) - you just
have to ask for it.
Unlike other languages, Perl seems to have hundreds of ways to
reference memory for variables, arrays, and code. The differences
revolve around scope and encapsulation of whatever is being
referenced.
What is unusual about Perl is that you have a choice of whether or not
to make your reference use a symbolic table. Its when a "local"
symbolic table is used for a reference that accessibility problems
occur.
Perl borrows a concept of "closure" from the language LISP that seems
to allow you to dynamically define what "scope" really means for any
particular reference. I found the documentation very confusing.
"Closure" seems to be stipulated by a combination of reference
operators (such as \&, @, $), the context that they appear in (such as
inside eval(), do{}, or sub{}), and Perl directives (such as EXPORT).
And, several of these constructs even have a few variations (such as
@EXPORT_OK and @EXPORT).
I have found it isn't just TK widgets that I can't reference. Any
subroutines that I call from inside of the run() subroutine that are
part of the same program run() resides in can't be called either -
they also hang.
Any help in understanding this would be much appreciated.
|
|
Posted by Andy on February 1, 2007, 4:28 pm
Please log in for more thread options
Although this doesn't fix the Tk access problem, I found that if you
give your Perl program an arbitrary and unique package name, such as
placing the following line at the top of your listing:
package Tk::BulkE;
that this package name will automatically be included as the first
line in the subroutine specified by the -command switch inside the
code that gets evaluated:
sub run{
print "start run\n";
$txtbox->insert(end,"this just hangs\n");
print "exit run\n";
}
becomes
{
package Tk::BulkE;
print "start run\n";
$txtbox->insert(end,"this just hangs\n");
print "exit run\n";
}
The eval statement inside of the scheduler then runs the code in the
context of your original Perl program, allowing full access to all of
your subroutines and variables.
|
| Similar Threads | Posted | | comp.lang.perl.modules,alt.autos.rod-n-custom,alt.kids-talk,alt.internet.p2p,alt.support.nutty.as.a.fruitcake | December 28, 2004, 2:24 am |
| use of xml in perl application to describe command line logic | December 20, 2005, 11:03 am |
| Perl/Tk build fails on HP-UX | February 27, 2006, 4:58 pm |
| Net::LDAP makefile generation fails on ActiveState Perl 5.8.8 | June 23, 2006, 2:57 am |
| Log::Dispatch - How to "die" a script after all other methods are called??? | September 6, 2005, 4:55 pm |
| Curses::Widgets::Menu Question | May 11, 2005, 8:32 pm |
| Compile problem: Curses::Widgets | September 24, 2007, 2:54 pm |
| SOLVED: Curses::Widgets Runtime Error | October 13, 2007, 3:01 pm |
| "Undefined subroutine" | November 21, 2004, 8:48 pm |
| catch_int\catch_hup subroutine | February 25, 2005, 8:51 am |
|