I have an app written in Perl/Tk. I am having a very strange problem in relation to the Button and Entry widgets. On one dialog I have an Entry widget and a Button. When the Button is pressed, I want to call a sub with the current contents of the Entry widget. Here is the code that attempts to do this:
my $newBtn =
$upper->Button('-text' => "New Subdirectory",
'-command' => [\&newDir, $direntry->get()])
->grid('-column' => 1,'-row' => 1);
This code successfully calls the newDir sub, but no value gets passed in. This led me to think that the
get() wasn't returning anything so I tried this line instead:
my $newBtn =
$upper->Button('-text' => "New Subdirectory",
'-command' => sub { print $direntry->get(),"\n"; })
->grid('-column' => 1,'-row' => 1);
Making this substitution prints out the correct information when the button is clicked.
Why can I only access the contents of the Entry through the anonymous sub? Why does it not seem to pass the value to the newDir sub? Is there some kind of scoping or execution order problem that I'm not aware of here? I'm sure I could wrap the call to newDir in an anonymous sub in the command handler, but the thought of doing that makes me cringe.
GuildensternNegaterd character class uber alles!
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.