Perl / Tk: get selected value of options menu
I am developing one interface in Perl / Tk.
In this I use one optionmenu to list usernames.
And when selecting a user from the optionmenu, it should display the employee's corresponding date of birth.
And I should be able to update the date of birth of the selected user.
I wrote the following code.
$dob_label = $form_name -> Label(-text=>"BirthDay")->place(-x=>150,-y=>200);
$dob=$form_name->DateEntry(-width=>11,-parsecmd=>\&parse,-formatcmd=>\&format)->place(-x=>250,-y=>200);
$ename = $form_name->Optionmenu(-variable=>\$select_value,-options => [@names],
-command=>sub {&get_id_date($hash_ref,$eid,$dob,$_[-1])})->place(-x=>250, -y=>100);
$post_button=$form_name->Button(-text=>"Add",-command=>[\&Add_Birthday,$select_value,$dob,"edit"])->place(-x=>250,-y=>275);
code>
Get_id_date function is used to get id and dob of employee using employee name.
It returns the correct id and dob.
Then I edited the employee dob.
And I call the Add_Birthday function to save the changes to the database. But what's the problem with this, the $ select_value variable always has the first name in the optionmenu. In fact, it should have the value of the last selected item in the options menu.
So what is the problem in this code,
Please give a solution for this as well.
Thanks in advance.
a source to share
When you create a button, you pass the current $ select_value to the button / command setting. When you click the button, the old $ select_value has already been evaluated and set in the command argument list. You need to make the command private so that $ select_value is not evaluated until the button is clicked, e.g .:
-command => sub { Add_Birthday($select_value, "dob", $edit) }
For the sake of completeness, I should also mention that another way to do this is to pass links:
-command => [\&Add_Birthday, \$select_value, "dob", \$edit]
But this requires rewriting the function to place references in the argument list.
a source to share