Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: call a sub from Tcl code in Tcl::pTk

by chrstphrchvz (Scribe)
on Jan 04, 2022 at 08:27 UTC ( #11140127=note: print w/replies, xml ) Need Help??


in reply to call a sub from Tcl code in Tcl::pTk

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.

  • Comment on Re: call a sub from Tcl code in Tcl::pTk

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11140127]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2022-09-27 07:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer my indexes to start at:




    Results (118 votes). Check out past polls.

    Notices?