Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Closure confusion in Tkx

by AnomalousMonk (Archbishop)
on Jul 13, 2019 at 13:37 UTC ( [id://11102791]=note: print w/replies, xml ) Need Help??


in reply to Closure confusion in Tkx

I don't know if Tkx is the same (I suspect it would be), but in Perl::Tk there is the "callback" (see Tk::callbacks), an array reference that encapsulates a function reference and a set of aguments:

c:\@Work\Perl\monks>perl -wMstrict -le "use constant Y => 'bar'; ;; my @callbacks; ;; my $z = 9; ;; for (my $x = 1; $x < 4; $x++) { push @callbacks, [ \&foo, $x, Y, $z-- ]; } ;; $z = 'zero'; ;; $_->[0]->(@$_[ 1 .. $#$_ ]) for @callbacks; ;; sub foo { print qq{@_}; } " 1 bar 9 2 bar 8 3 bar 7
(Don't worry about the messy  $_->[0]->(@$_[ 1 .. $#$_ ]) stuff in the example; that's all taken care of under the hood in Tk and is only intended to illustrate the principle.)

Update: Changed example code to include  $z variable.


Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11102791]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-04-19 20:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found