The Win32 System Menu Item

It’s possible to add menu items to the Win32 System menubutton (the button at the top-left of a Perl/Tk window, labeled with the red Tk letters in script). Based on the operating system identifier, $^O, this code conditionally adds a command menu item that executes a DOS pipeline.

if ($^O eq 'MSWin32') {
    my $syst = $menubar->cascade(-label => '~System');
    my $dir = 'dir | sort | more';
    $syst->command(
        -label   => $dir,
        -command => sub {system $dir},
    );
}

Figure 12-3 illustrates.

A System menu item that pages a sorted dir command

Figure 12-3. A System menu item that pages a sorted dir command

Figure 12-4 shows a DOS window and the contents of a particular directory. Then a Perl/Tk program is started and the “dir | sort | more” button is clicked; note the unsolicited, sorted directory listing.

Output from pressing the “dir | sort | more” System menu button

Figure 12-4.  Output from pressing the “dir | sort | more” System menu button

The special menu item labeled “Tearoff” is really a tearoff menu item that, when clicked, tears off only the Tk portion of the System menu.

Get Mastering Perl/Tk 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.