#!/usr/bin/perl -- ## perltidy -olq -csc -csci=10 -cscl="sub : BEGIN END if " -otr -opr -ce -nibc -i=4 -pt=0 "-nsak=*" use strict; use warnings; use Tk; use Data::Dump qw/ dd /; Main( @ARGV ); exit( 0 ); sub Main { GoTk(); } sub GoTk { my $mw = tkinit( -title => "tk closures people" ); ## the "globals" my %info; my %cells; my $currentrow = 1; ## the oddballs my $row = 1; my $column = 1; for my $key ( qw/ a b c Q 6 42 / ) { my $tvref = \$info{ $key . $row }; $$tvref = "($row)($column) $key"; my $label = $mw->Label( -textvariable => $tvref, -relief => "ridge", -width => 15, )->grid( -row => $row, -column => $column ); $cells{ $key . $row } = $label; ## logic flaw $row++; $column = int rand( 4 ); } ## one callback for entire grid, not one for each label $mw->bind( 'Tk::Label' => '', [ \&Pickett, \$currentrow, \%info ], ## NO CLOSURE!!! ); use Tk::WidgetDump; $mw->WidgetDump; $mw->MainLoop; dd( \%cells, \%info ); } ## end sub GoTk sub Pickett { my( $label, $currentrowref, $info ) = @_; my $e = $Tk::event; my $xy = sprintf( '(%s,%s)', $e->X, $e->Y, ); ## $info->{a1} print "\n$xy $label $currentrowref $$currentrowref\n"; if( $label->isa( 'Tk::Label' ) ) { print "$label ", $label->cget( -text ), "\n"; } } ## end sub Pickett __END__ (210,135) Tk::Label=HASH(0x107489c) SCALAR(0xb9b374) 1 Tk::Label=HASH(0x107489c) (5)(1) 6 (239,145) Tk::Label=HASH(0x10749cc) SCALAR(0xb9b374) 1 Tk::Label=HASH(0x10749cc) (6)(2) 42 ( { 426 => bless({ _TkValue_ => ".label5" }, "Tk::Label"), 65 => bless({ _TkValue_ => ".label4" }, "Tk::Label"), a1 => bless({ _TkValue_ => ".label" }, "Tk::Label"), b2 => bless({ _TkValue_ => ".label1" }, "Tk::Label"), c3 => bless({ _TkValue_ => ".label2" }, "Tk::Label"), Q4 => bless({ _TkValue_ => ".label3" }, "Tk::Label"), }, { 426 => "(6)(2) 42", 65 => "(5)(1) 6", a1 => "(1)(1) a", b2 => "(2)(0) b", c3 => "(3)(0) c", Q4 => "(4)(3) Q", }, )