Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Perl/Tk window->protocol question

by mnooning (Beadle)
on Mar 17, 2006 at 17:54 UTC ( #537515=perlquestion: print w/replies, xml ) Need Help??
mnooning has asked for the wisdom of the Perl Monks concerning the following question:


I am writing an event driven system wherein as one top level window is
closed, another pops up. I provide an Exit button on each window that
has it's own routine for cleaning up and exiting. If the user instead
clicks the upper right window cross hair to close the window, I still
want to go to the said cleaning up routine. The call below does not
work. $top of course is a Toplevel reference.

$top->protocol( 'WM_DELETE_WINDOW', \&my_clean_up_routine( $top, $another_parameter);

The call is fashioned after this example from


Perhaps the problem is that no parameters are allowed. I am guessing
that because the code below works in that if using it, the said cross
hairs will not close the window out, which breaks tradition.

$window->protocol( 'WM_DELETE_WINDOW', \&Tk::NoOp);

Any ideas?

Replies are listed 'Best First'.
Re: Perl/Tk window->protocol question
by jdporter (Canon) on Mar 17, 2006 at 18:06 UTC
    Perhaps the problem is that no parameters are allowed.

    You've hit the nail on the head. \& takes a reference to the subroutine's implementation, whereas parameters are data that are passed at runtime. Probably the easiest way to achieve what you want here is to wrap your function call in another sub which doesn't require any arguments. This, in turn, can simply call your function, with arguments. Like so:

    $top->protocol( 'WM_DELETE_WINDOW', sub { my_clean_up_routine( $top, $another_parameter ); } );

    (Pay close attention to the braces and parens in that example.)

    We're building the house of the future together.
Re: Perl/Tk window->protocol question
by zentara (Archbishop) on Mar 17, 2006 at 18:44 UTC
    I think you may be looking for the square bracket form of callback
    -command => sub { numpress( $i ) } # equals -command => [ \&numpress, $i ]
    so try
    $top->protocol( DELETE_WINDOW', [\&my_clean_up_routine, $top, $another_parameter] ); [download]

    I'm not really a human, but I play one on earth. flash japh
      It worked. Outstanding!
      Thanks for the quick response, too.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://537515]
Approved by GrandFather
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (1)
As of 2018-04-23 17:59 GMT
Find Nodes?
    Voting Booth?