in reply to Calling subrutine after creating Tk mainwindow

G'day IB2017,

Take a look at "Re: Code after MainLoop; Tk", which I wrote about 18 months ago. This is an alternative to what you're currently doing and may be a better option.

With your posted code, &doSomething and $mw are tightly coupled. You only show &doSomething performing a print which is unrelated to $mw; it seems that $mw is only required for the destroy method.

Adapting the code I've linked, you could write something far cleaner, which might use this sort of format:

... { my $mw = ... ... ... $mw->destroy ... ... MainLoop; } doSomething(); sub doSomething { print "Hello!\n"; }

"$mw->destroy" could be called explicitly, e.g. via a Button action, or implicitly, e.g. along the lines of the after examples you've already been given. Note that, in the code fragment, $mw only exists in its own, limited, lexical scope; it is completely invisible to any code related to &doSomething.

It seems odd that you'd want to create a GUI then, as the very first thing, perform some unrelated task which culminates in the destruction of that GUI. If you provide a clearer picture of what you're hoping to accomplish, we can possibly provide better, or at least more focussed, advice.

— Ken