Tcl::pTk maintainer here (as I rarely am; I do follow the perl.tcltk list, though, and this is a great topic to bring up there)…
Yes, the wrapper modules Tcl::Tk, Tcl::pTk, and Tkx all rely on Tcl.pm, which provides a few ways of running Perl code from Tcl. As another comment mentioned, one way is with the ::perl::Eval command:
# from Perl
$interp->Eval(<<'EOS');
# from Tcl
::perl::Eval {
# from Perl: same scope as $interp->Eval()
_do_something();
}
EOS
Another is with the export_to_tcl() method to export a Perl subroutine for use as a Tcl command (under the ::perl namespace):
# from Perl
$interp->export_to_tcl(
subs => {
'_do_something' => \&_do_something,
},
);
$interp->Eval(<<'EOS');
# from Tcl
set var1 {hello}
set var2 [split "x,y,,z" ","]
::perl::_do_something $var1 world $var2
EOS
I would note that for the export_to_tcl approach, at least in current Tcl.pm (1.27), any arguments will only appear as their string representations to Perl, so doing:
::perl::_do_something $var1 world $var2
in Tcl would call:
_do_something('hello', 'world', 'x y {} z')
in Perl. If the arguments correspond to Tcl lists or other more structured values, and you want to retrieve their contents from Perl, then rather than relying on Perl string operations, consider using $interp->SplitList() or additional calls to $interp->Eval() (which tries to be more intelligent when converting the value returned from Tcl to a close Perl equivalent, and not just always giving back the string representation).
Admittedly, I’m not aware of there being much usage of either approach, let alone good examples for reference or inspiration. |