Hello!

I am using perk/tk here and am having issues creating a file menu and having it perform a specific action based on a directory search.

----------

So previously I had the commands hard-coded and it looked like this:

$menubar = $top->Menu; $file_menu = $menubar->cascade(-label => "~File"); $file_menu->command(-label => "Restart", -command => \&Restart); $file_menu->command(-label => "Exit", -command => \&Exit); $file_menu = $menubar->cascade(-label => "Filter"); $file_menu->command(-label => "All", -command => sub {$fname = "mac +hines"; &Restart}); $file_menu->command(-label => "HP-UX", -command => sub {$fname = "m +achines.HPUX"; &Restart}); $file_menu->command(-label => "Linux", -command => sub {$fname = "m +achines.Linux"; &Restart});

----------------

But have since decided to pull the list (HP-UX, Linux, other things in the future), from files found in a directory. So now my code looks like this:

# Get filter list my @filter = qw(); my $dir = '.'; my $x=0; opendir(DIR, $dir) or die $!; while (my $file = readdir(DIR)) { next unless (-f "$dir/$file"); next unless ($file =~ m/^machines\./); push (@filter, $file); } closedir(DIR); $file_menu = $menubar->cascade(-label => "~File"); $file_menu->command(-label => "Restart", -command => \&Restart); $file_menu->command(-label => "Exit", -command => \&Exit); $file_menu = $menubar->cascade(-label => "Filter"); $file_menu->command(-label => "All", -command => sub {$fname = "mac +hines"; &Restart}); foreach (@filter) { $file_menu->command(-label => "$_", -command => sub {$fname = "$_ +"; &Restart}); }

------------------

However the issues lies in that when the button is clicked, "$fname" no longer knows what "$_" is, or is null.

So my question is, how can I create a file menu based on an array but still be able to set a variable in the command portion of the menu command?

I hope that all makes sense!


In reply to Perl/Tk menu and commands based on array by richie

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.