Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

call a sub from Tcl code in Tcl::pTk

by Anonymous Monk
on Apr 28, 2021 at 21:14 UTC ( [id://11131823]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello

I use Tcl::pTk to write Tk Guis instead of outdated Perl/Tk. The module tcl::pTk has the following interesting feature:

my $int = $mw->interp; $int->Eval("..."); _do_something{}

to write directly Tcl code in my Perl script and allow me to use some interesting Tk features not available directly through the module. Is there a way to call a Perl subrutine, in my example _do_something, from that Eval?

Replies are listed 'Best First'.
Re: call a sub from Tcl code in Tcl::pTk
by jcb (Parson) on Apr 29, 2021 at 03:49 UTC

    I would not call Perl/Tk outdated.

    The major difference between Tk and Tcl::pTk is that Tk embeds a ported version of Tk that does not require a Tcl interpreter, while Tcl::pTk embeds a Tcl interpreter which, in turn, uses a native Tk extension. It is worth noting that the Tcl-free Tk port used in Tk has also been adapted to other languages; if I recall correctly, Python's tk module also uses it.

    The Perl community is generally very much in favor of TIMTOWTDI and deriding alternatives to your chosen solution is unlikely to earn you any help.

Re: call a sub from Tcl code in Tcl::pTk
by LanX (Saint) on Apr 29, 2021 at 11:01 UTC
    Could you explain why you want this?

    The way to call Perl from Tk is to trigger callbacks, like pressed buttons.

    Tcl is used to create the Tk GUI and can also create triggers.

    So there are ways, but this smells like a XY Problem

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

Re: call a sub from Tcl code in Tcl::pTk
by chrstphrchvz (Scribe) on Jan 04, 2022 at 08:27 UTC

    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.

Re: call a sub from Tcl code in Tcl::pTk
by Anonymous Monk on Apr 29, 2021 at 13:28 UTC

    Very sorry if I caused any trouble, maybe I missed to specify "outdated from a Tk, especially macOS-related, perspective".

    We work on an app using Tk on macOS, and Perl/Tk is - in our terms - outdated. It runs with 'hacks' (needs XQuartz) and the UI it produces is non-Acqua compatible, looks like old-fashioned UI and this makes Perl/Tk, in our modest opinion, a no-go in the macOS world (okay, you can define it not as outdated, happy to learn another definition that better suits this group). Tcl::pTk gives instead access to the modern Tcl/Tk framework which is actively maintained and in line with UI conventions and styles on macOS. No more than this.

    My question is pretty straightforward: from a portion of code written diretcly in Tcl inside $int->Eval("...") we want to call a subrutine which lives in the normal Perl code. A typical case would be to manipulate macOS typical menu entries such as Preferences, that can not be done - as far as we know directly from Perl i.e. Tcl::pTk:

    my $int = $mw->interp; my $menuPATH = $int->widget($menubar);#getting Tk name $int->Eval(" $menuPATH add cascade -menu [menu $menuPATH.apple] proc tk::mac::ShowPreferences {} {#CALL SUBRUTINE IN PERL +CODE THAT CREATES THE COMPLEX MENU WINDOW TO SET APP PREFERENCES"} ");

    Does it make more sense now

    PS: Python standard UI framework is Tkinter which uses the same principle of Tcl::pTk, i.e. it links to the original Tcl/Tk framework.

    Thank you

      Your question reveals an inside-out approach.

      The ->Eval method is meant for exceptional stuff which can't be done with normal Perl methods.

      The normal approach is to use standard pTk->methods to build menus and windows.

      If your special wish is even possible I doubt that you'll get a quick answer here.

      edit

      Anyway ... if you just want to generate Tcl code from a Perl method inside your template you can do something like

      ->Eval(<<"__TCL__"); ... some Tcl Code @{[ my_perl_sub() ]} ... more Tcl Code __TCL__

      the result from my_perl_sub() is inserted as string then, which will be interpreted as Tcl code.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

        Thank you LanX for your answer. I agree that ->Eval is to be used for exceptional cases where pTk->methods are not available. The case presented above is (should be) such a cases, since there are no Perl or pTk methods to manipulate the macOS menu. Such methods are instead available in the Tcl/Tk.

Re: call a sub from Tcl code in Tcl::pTk
by Marshall (Canon) on Apr 29, 2021 at 04:46 UTC
    I don't understand your question.
    What is it that you think that Tk doesn't do, that you want to do?
    Use as much verbiage as you want to explain some specific need.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11131823]
Approved by GrandFather
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-18 00:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found