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

Re: Perl/Tk and exit(0)

by 1nickt (Canon)
on Apr 02, 2020 at 23:15 UTC ( [id://11114966]=note: print w/replies, xml ) Need Help??


in reply to Perl/Tk and exit(0)

Hi, I am not a Tk programmer, but ... exit exits :-) If you want to return from a sub, return :-)

(And commenting out the exit statement makes your script work because you don't need to use an explicit return statement to return from a sub, if you are not returning anything.)

Hope this helps!


The way forward always starts with a minimal test.

Replies are listed 'Best First'.
Re^2: Perl/Tk and exit(0) [Tk::exit]
by kcott (Archbishop) on Apr 03, 2020 at 07:05 UTC

    G'day 1nickt,

    The link you posted labeled "exit" is to a Google search (http://www.google.com/search?q=exit%20site%3Aperldoc.perl.org). The first item returned from that search is to perldoc (https://perldoc.perl.org/functions/exit.html). I couldn't see anything else pertinent, so I'm wondering why the intermediary Google step. Anyway, the point is somewhat moot as that's the wrong "exit".

    "I am not a Tk programmer ..."

    In my experience, even amongst Tk programmers, it's a little known fact that Tk exports, by default, its own exit: Tk::exit.

    Knowing that doesn't help the OP but I thought it was worth mentioning.

    — Ken

      Thanks Ken, I'll update the link. Not sure how that happened.

      Update: I fixed the links by changing from [perldoc:// to [doc://. I wonder if the behaviour of the former syntax is new, broken, or something I just imagined was different. The links as I originally posted definitely went to a Goog search. Maybe a topic for PM Discussion...

      The way forward always starts with a minimal test.
        "I wonder if the behaviour of the former syntax is new, broken, or something I just imagined was different."

        Unfortunately, I suspect it may be the latter.

        I rarely, if ever, use those shortcuts; however, checking in "What shortcuts can I use for linking to other information?", I see (my emphasis added):

        The Perl documentation:

        • Search the Perl docs: [perldoc://search terms]
        • Link directly to a the doc of a function or variable: [doc://name] ...

        There was a change to [doc://name] back in February: "NEW [doc://perldoc#deeplink] works now with all perldocs". I don't recall any recent changes to [perldoc://search terms].

        "Maybe a topic for PM Discussion..."

        Or maybe not. :-)

        Unless you wish to request a typo fix: s/to a the doc/to the doc/

        — Ken

      As I understand, the two exit procedures only matter if you fork. Calling Tk's exit from a forked child causes the parent to also exit, while calling CORE::exit causes only that forked child to exit.

        G'day jcb,

        I'm unsure whether you posted this to corroborate what I said or correct something you believed I had wrong in some way. So, purely by way of clarification:

        • I was pointing out that the exit in a Tk script was the one described in https://metacpan.org/pod/distribution/Tk/pod/exit.pod and not the one described in https://perldoc.perl.org/functions/exit.html.
        • I was not, in any way, suggesting that changing from Tk::exit to CORE::exit would be beneficial for the OP. In fact, I wrote: "Knowing that doesn't help the OP ...".
        • What you wrote ("As I understand, the two exit procedures ...") pretty much paraphrases what's in the Tk::exit documentation; i.e. "Using exit from a forked process will terminate both the child process and the parent process. As Tk::exit is imported by default one should use CORE::exit in the forked process instead.".
        • The reason to generally prefer Tk::exit over CORE::exit is given in the preceding paragraph: "If calling exit from code invoked via a Tk callback then this Tk version of exit cleans up more reliably than using the perl exit.".

        If there was any miscommunication or misunderstanding, I hope that clears it up.

        — Ken

        As I understand, the two exit procedures only matter if you fork.

        This is on windows, clean exit

        perl -e"use Tk; $mw=tkinit; $mw->Button(-command=>sub{ Tk::exit })->pa +ck; MainLoop; "

        Dirty exit

        perl -e"use Tk; $mw=tkinit; $mw->Button(-command=>sub{ CORE::exit })-> +pack; MainLoop; " perl.exe has encountered a problem and needs to close. Faulting application perl.exe, version 5.20.3.3, faulting module tk.xs +.dll, version 8.4.2.5, fault address 0x00044b1e.
Re^2: Perl/Tk and exit(0)
by saw55 (Novice) on Apr 02, 2020 at 23:49 UTC

    Thanks for the reply. I know exit causes the program to exit, but I believe the call to the subroutine preceding the exit statement should cause the message window to display before the program exits. I know sub timedDialog executes because I inserted a print statement in it and it does indeed print but the message window never displays; and I put a sleep statement between the sub call and exit in hope of allowing time for the message to display but no go.

    I just noticed I called timedDialog both in sub checkDays and in sub setupGUI I fixed this and still no message shows

    Fixed code follows

    #!/usr/bin/perl ###################################################################### +## use strict; use warnings; use Tk; my $mw = MainWindow -> new; my $timedDialogTitle = ''; my $timedDialogText = ''; my $svBtn = undef; #Option window SAVE button. &setupGUI; $mw->deiconify(); $mw->raise(); MainLoop; exit(0); ################################################ ################################################ sub setupGUI{ $svBtn = $mw->Button( -text => "SAVE", -command => sub {&checkDays +; exit(0);}); $svBtn->grid(-row => 9, -column => 2, -sticky => 'e'); $mw->bind('<KeyPress-Return>' => sub {&checkDays; exit(0);}); $mw-> withdraw(); } ##################################### sub checkDays { &timedDialog("Exiting", "O.K., no backup will be made, + then....Exiting", 25_000); sleep 15; } ##################################### sub timedDialog { print ("in timedDialog\n"); my $subwindow = MainWindow->new; $subwindow->geometry("490x150+400+400"); $subwindow->title($_[0]); my $label = $subwindow->Label(-text => $_[1]); $label->pack; $subwindow->after($_[2], sub {$subwindow->destroy;}); } #####################################

      All UI activity in Tk occurs in the Tk event loop. You are exiting the program from an event handler before returning control to the event loop. Try replacing that handler with sub {&checkDays; $mw->after(5000, sub {exit(0)});} and see Tk::after for more details.

        Thanks for the reply, but I could use some clarification if you could. Are you saying I should replace

        $svBtn = $mw->Button( -text => "SAVE", -command => sub {&checkDays; exit(0);});

        with

        $svBtn = $mw->Button( -text => "SAVE", -command => sub {&checkDays; $mw->after(5000, sub {exit(0)});}

        When I do that I get an error saying "Scalar found where operator expected" at the next line, near "$mw" I guess I am sort of lost.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2024-04-20 02:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found