The routine want's a coderef. In the first case you're giving it one. It is a reference to a routine which, when executed, calls change_mode(1).
In the second case you're giving it the result of calling a subroutine whose reference is held in $change_mode.
And the third case is the same as the second, the except dereferencing is done with different symbols.
Result:#!/usr/bin/perl -w use Data::Dumper; $Data::Dumper::Deparse = 1; my $change_mode = \&change_mode; sub change_mode { return "returning: @_ " } #Case 1: Callback routine called directly with anonymous reference Button(-text => 'To Lower Case', -command => sub { &change_mode(1) } ); #Case 2: Callback routine called with argument via named reference ve +r. 1 Button(-text => 'To Upper Case', -command => &{$change_mode}(2) ); #Case 3: Callback routine called with argument via named reference ve +r. 2 Button(-text => 'To Mixed Case', -command => $change_mode->(3) ); sub Button { my %h = @_; print Dumper(\%h); }
--Bob Niederman, http://bob-n.com$VAR1 = { '-text' => 'To Lower Case', '-command' => sub { &change_mode(1); } }; $VAR1 = { '-text' => 'To Upper Case', '-command' => 'returning: 2 ' }; $VAR1 = { '-text' => 'To Mixed Case', '-command' => 'returning: 3 ' };
In reply to Re: Passing arguments to callback routines with named references
by bobn
in thread Passing arguments to callback routines with named references
by nysus
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |