Callbacks

Many widgets allow you to define a callback, which is a command to execute when the widget is selected. For example, when you press an exit button, the callback might be to a routine that cleans up and quits the program. When you click on a radio button, you might want to change the window to reflect the new preferences.

Widgets that support callbacks have a -command option to provide the callback function. In the “Hello World” example shown previously, the callback is to sub {exit}. In that example, the callback is called as an anonymous subroutine. You could also use a reference to a subroutine (e.g., \&routine). If you want to provide arguments to a subroutine, you can call it as an anonymous list (e.g., [ \&routine, $arg, $arg, ... ]).

Get Perl in a Nutshell, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.