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

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

Replies are listed 'Best First'.
Re^2: call a sub from Tcl code in Tcl::pTk
by LanX (Saint) on Apr 29, 2021 at 13:40 UTC
    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.