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.


In reply to Re: call a sub from Tcl code in Tcl::pTk by chrstphrchvz
in thread call a sub from Tcl code in Tcl::pTk by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.