dpmott has asked for the wisdom of the Perl Monks concerning the following question:

I was looking at the source code for Tk::OptionMenu version 4.013, and I just can't get my brain wrapped around this little bit of logic in addOptions().

Here's a snippet:
sub addOptions { my $w = shift; my $menu = $w->menu; my $tvar = $w->cget(-textvariable); my $vvar = $w->cget(-variable); my $oldt = $$tvar; my $width = $w->cget('-width'); my %hash; my $first; while (@_) { my $val = shift; my $label = $val; if (ref $val) { if ($vvar == $tvar) { my $new = $label; $w->configure(-textvariable => ($tvar = \$new)); } ($label, $val) = @$val; } my $len = length($label); [snip]
Okay, so if I pass it an array ref, and if my $tvar == $vvar, then I assign $new the value of my label (but... $label is $val is a ref...) and then I take a ref of $new (yielding a ref of an array ref...) and assign it to $tvar and configure my textvariable with it.

Do I have that right?

I'm almost certain that this code was supposed to be structured like this:
($label, $val) = @$val; if ($vvar == $tvar) { my $new = $label; $w->configure(-textvariable => ($tvar = \$new)); }
If lots of people agree, I'd be happy to email the author but... I can't find the author for this module. Maybe I just don't know where to look. If you agree that the above code has a bug and can provide an email address for the author, I'd greatly appreciate it.

Thanks!

Replies are listed 'Best First'.
Re: Is this magic, or a bug, in Tk::Optionmenu?
by Anonymous Monk on Jun 03, 2009 at 07:32 UTC