I'm learning Perl and Tkx and am wondering if it is possible to pass Perl functions to the Tcl interpreter in a manner similar to Python's "register" method in the Tkinter module. In Python, you can call register("mypythonfunction"), which is then passed to the Tcl interpreter as though it were a Tcl procedure. This functionality is useful when interacting wtih the Tcl interpreter at a low level.
Let me explain my interest in this issue. I'm working on Mac OS X and am trying to build a Perl/Tkx application that supports responding to AppleEvents, i.e. you can script the Perl-based application via AppleScript just like you can script Safari, iTunes, Photoshop, and other apps. Perl, though, lacks up-to-date methods for integrating with AppleScript; its current methods, such as http://search.cpan.org/~cnandor/Mac-AppleEvents-Simple-1.18/Simple.pm, are based on deprecated functions that don't work in a 64-bit context, which is essential.
I've investigated alternative approaches for adding such functionality, such as creating a SWIG-based extension, but SWIG doesn't allow callbacks into the Perl interpreter. Another approach I've researched is using SWIG to call out to a helper Perl application that would then call the main Perl application via some sort of RPC mechanism, but all the RPC mechanisms I've tried (numerous links at http://search.cpan.org/~powerman/JSON-RPC2-0.1.1/lib/JSON/RPC2.pm#RAT...) are all half-baked in some way. (If someone can point me to an RPC module they've had success with, I'd be grateful. Ruby's "druby" module is elegant and simple, but its lack of desktop app deployment tools for the Mac make Ruby unsuitable.)
So the approach I'd like to try is installing AppleEvent handlers via Tcl's TclAE package: this is a powerful package that can install the appropriate AppleEvents and then execute event handlers in the Tcl interpreter. Integrating this with Perl would, ideally, involve some sort of mechanism where I can "register" a Perl function that is passed to the Tcl interpreter and executed as if it were Tcl code.
On Sunday, August 19, 2012 at 9:19 PM, Kevin Walzer wrote:
> Hello all,
> I'm learning Perl and Tkx and am wondering if it is possible to pass
> Perl functions to the Tcl interpreter in a manner similar to Python's
> "register" method in the Tkinter module. In Python, you can call
> register("mypythonfunction"), which is then passed to the Tcl
> interpreter as though it were a Tcl procedure. This functionality is
> useful when interacting wtih the Tcl interpreter at a low level.
> Let me explain my interest in this issue. I'm working on Mac OS X and am
> trying to build a Perl/Tkx application that supports responding to
> AppleEvents, i.e. you can script the Perl-based application via
> AppleScript just like you can script Safari, iTunes, Photoshop, and
> other apps. Perl, though, lacks up-to-date methods for integrating with
> AppleScript; its current methods, such as
> http://search.cpan.org/~cnandor/Mac-AppleEvents-Simple-1.18/Simple.pm,
> are based on deprecated functions that don't work in a 64-bit context,
> which is essential.
> I've investigated alternative approaches for adding such functionality,
> such as creating a SWIG-based extension, but SWIG doesn't allow
> callbacks into the Perl interpreter. Another approach I've researched is
> using SWIG to call out to a helper Perl application that would then call
> the main Perl application via some sort of RPC mechanism, but all the
> RPC mechanisms I've tried (numerous links at
> http://search.cpan.org/~powerman/JSON-RPC2-0.1.1/lib/JSON/RPC2.pm#RAT...)
> are all half-baked in some way. (If someone can point me to an RPC
> module they've had success with, I'd be grateful. Ruby's "druby" module
> is elegant and simple, but its lack of desktop app deployment tools for
> the Mac make Ruby unsuitable.)
> So the approach I'd like to try is installing AppleEvent handlers via
> Tcl's TclAE package: this is a powerful package that can install the
> appropriate AppleEvents and then execute event handlers in the Tcl
> interpreter. Integrating this with Perl would, ideally, involve some
> sort of mechanism where I can "register" a Perl function that is passed
> to the Tcl interpreter and executed as if it were Tcl code.
The binding for Tkx is much more efficient than Tkinter, and does allow for 2 way communication and command binding in the interface.
Alexander noted you have ::perl::Eval in Tcl, but there are more ways to bind the 2 pieces together. I would recommend looking at the code for ppm in ActivePerl, which uses Tkx in various elaborate ways (blending Tcl and Perl in interesting ways), for lots of good code examples. See
/usr/local/ActivePerl-<ver>/bin/ppm
/usr/local/ActivePerl-<ver>/lib/ActivePerl/PPM/*
Note that ActivePerl uses Tkx via tkkits (baseballs with extended functionality). These can be extended (see archives of this list), but you can use external packages as needed.
> I'm learning Perl and Tkx and am wondering if it is possible to pass Perl functions to the Tcl interpreter in a manner similar to Python's "register" method in the Tkinter module. In Python, you can call register("mypythonfunction"), which is then passed to the Tcl interpreter as though it were a Tcl procedure. This functionality is useful when interacting wtih the Tcl interpreter at a low level.
> Let me explain my interest in this issue. I'm working on Mac OS X and am trying to build a Perl/Tkx application that supports responding to AppleEvents, i.e. you can script the Perl-based application via AppleScript just like you can script Safari, iTunes, Photoshop, and other apps. Perl, though, lacks up-to-date methods for integrating with AppleScript; its current methods, such as http://search.cpan.org/~cnandor/Mac-AppleEvents-Simple-1.18/Simple.pm, are based on deprecated functions that don't work in a 64-bit context, which is essential.
> I've investigated alternative approaches for adding such functionality, such as creating a SWIG-based extension, but SWIG doesn't allow callbacks into the Perl interpreter. Another approach I've researched is using SWIG to call out to a helper Perl application that would then call the main Perl application via some sort of RPC mechanism, but all the RPC mechanisms I've tried (numerous links at http://search.cpan.org/~powerman/JSON-RPC2-0.1.1/lib/JSON/RPC2.pm#RAT...) are all half-baked in some way. (If someone can point me to an RPC module they've had success with, I'd be grateful. Ruby's "druby" module is elegant and simple, but its lack of desktop app deployment tools for the Mac make Ruby unsuitable.)
> So the approach I'd like to try is installing AppleEvent handlers via Tcl's TclAE package: this is a powerful package that can install the appropriate AppleEvents and then execute event handlers in the Tcl interpreter. Integrating this with Perl would, ideally, involve some sort of mechanism where I can "register" a Perl function that is passed to the Tcl interpreter and executed as if it were Tcl code.
> The binding for Tkx is much more efficient than Tkinter, and does allow for 2 way communication and command binding in the interface.
> Alexander noted you have ::perl::Eval in Tcl, but there are more ways to bind the 2 pieces together. I would recommend looking at the code for ppm in ActivePerl, which uses Tkx in various elaborate ways (blending Tcl and Perl in interesting ways), for lots of good code examples. See
> /usr/local/ActivePerl-<ver>/bin/ppm
> /usr/local/ActivePerl-<ver>/lib/ActivePerl/PPM/*
> Note that ActivePerl uses Tkx via tkkits (baseballs with extended functionality). These can be extended (see archives of this list), but you can use external packages as needed.
> Jeff
Jeff,
Thanks to you and Alexander for this advice: it appears that I can safely integrate Tcl's AppleEvent package with Tkx given the mechanisms you describe. Awesome.
On Sunday, August 19, 2012 at 13:19 PM, Kevin Walzer wrote:
> Hello all,
> I'm learning Perl and Tkx and am wondering if it is possible to pass > Perl functions to the Tcl interpreter in a manner similar to Python's > "register" method in the Tkinter module. In Python, you can call > register("mypythonfunction"), which is then passed to the Tcl > interpreter as though it were a Tcl procedure. This functionality is > useful when interacting wtih the Tcl interpreter at a low level.
I've used CreateCommand in the past:
$name="new_cmd_name_in_tcl";$tcl->CreateCommand($name, sub {
> my $clientdata=shift; # this is set to "a_string_here"
> my $interp=shift;
> my $argc=shift;
> # @_ will contain argv
> call_some_perl_sub(@_);
}, "a_string_here");
You can use the $argc and $clientdata strings in interesting ways...