
perlplexer
Deleted
Dec 1, 2000, 7:35 AM
Post #1 of 2
(467 views)
|
|
to ref or not to ref
|
Can't Post
|
|
Hey people! I am trying to write a code fragment that will create a menu using Tk. The code should take two arguments: @buttons and %actions, where @buttons contains all the names and % actions contains references to subs that must be executed when user hits a button. ([()],[()],[()] may be more efficient but that's not the point here) Here is my first try on this: ----------------Code A--------------------- my @buttons = ("Display Log Files",); my %actions = (); @actions{@buttons} = (\&DisplayLog); $top = MainWindow->new(); foreach $button (@buttons){ $top->Button(-text => $button, -command => sub{$actions{$button}($top)})->pack; } MainLoop; exit; --------------------------------------------- The code compiles but does not work as expected because $button becomes "" after the 'foreach' loop and everytime user hits a button I get "Undefined subroutine &main:: called at line blah..." Now, if you still follow, look at the following fragment: -------------------Code B-------------------- my @buttons = ("Display Log Files",); my %actions = (); @actions{@buttons} = ('DisplayLog'); $top = MainWindow->new(); foreach $button (@buttons){ $code = '$top->Button(-text => $button, -command => sub{'."$actions{$button}".q/($top)}); return 1;/; print "Eval failed\n" unless eval $code; } MainLoop; exit; ------------------------------------------- This code compiles and runs fine. BUT! I don't like it because it uses eval(). The question is, is there a way to do the same thing without using eval? I would really prefer to use references...
|