Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Pass Perl functions to Tcl interpreter

63 views
Skip to first unread message

Kevin Walzer

unread,
Aug 19, 2012, 1:19:41 PM8/19/12
to tc...@perl.org
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#RATIONALE)
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.

Advice is appreciated.

Thanks,
Kevin

--
Kevin Walzer
Code by Kevin
http://www.codebykevin.com

Alexander Nusov

unread,
Aug 19, 2012, 5:25:15 PM8/19/12
to k...@codebykevin.com, tc...@perl.org
Hello Kevin.

You can run perl code from tcl interpreter using (works for Tkx)
::perl::Eval {
# perl code
}

for example,
sub do_something {
print "Hello\n";
}

Tkx::eval(<<EOT

# … TCL code
# ...
proc invoke_perl_code {} {
::perl::Eval {
main::do_something();
}
}

invoke_perl_code

EOT
);


--
Best regards,
Alexander Nusov

Jeff Hobbs

unread,
Aug 19, 2012, 5:38:00 PM8/19/12
to k...@codebykevin.com, tc...@perl.org
Hi Kevin,

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

Kevin Walzer

unread,
Aug 20, 2012, 10:22:14 AM8/20/12
to Jeff Hobbs, tc...@perl.org
On 8/19/12 5:38 PM, Jeff Hobbs wrote:
> Hi Kevin,
>
> 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.

Eric Windisch

unread,
Aug 20, 2012, 10:41:31 AM8/20/12
to k...@codebykevin.com, tc...@perl.org
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...

--
Eric Windisch



0 new messages