For aesthetic reasons, I'd like to use a window menu bar and configure it on startup using the following code. Ideally I would like to write only one subroutine to handle all the menu requests as the directory contents and therefore the menu list can vary (I hope I'm making this clear).
1. #Read a list of files from the cur/wkg/dir and insert them in the 'Load' menu bar ; 2. opendir(DIR, $dir); 3. @dirList = readdir(DIR); 4. closedir(DIR); 5. # Sort the list; 6. @dirList = sort @dirList; 7. foreach $fn (@dirList) { 8. $Load_mb -> command(-label => $fn, 9. -command => sub {&Load ($fn)} ); 10. } ; # End foreach; 11. 12. sub Load { 13. $Title = "Loading $_[0] "; 14. $Main_Window -> title($Title); 15. #Do something clever with whichever menu item was requested; 16. }
This works OK, except for Line 9 which passes to the subroutine whatever was in $fn the last time it was used, regardless of which menu item was selected. I've tried all sorts of alternatives but nothing works as intended, and unless some clever monk has a really ingenious suggestion I think I'm going to be stuck with several dozen short subroutines and a hash table. I've searched, but cannot find an equivalent to the cget instruction used in listboxes.

In reply to Perl/Tk Menus by Clachair

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.