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.

#!/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); }
Result:
$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 ' };
--Bob Niederman, http://bob-n.com

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.