Basically , you're not using the right method "get", and variable scoping (what choroba said, closures don't work for globals). Compare your program to this (the commented lines are equivalent in effect)
I avoid closures by using references
#!/usr/bin/perl --
use strict;
use warnings;
use Tk;
use Data::Dump qw/ dd /;
Main( @ARGV );
exit( 0 );
sub Main {
GoTk();
GoTk();
}
sub GoTk {
my $mw = tkinit( -title => "Frame delete test" );
my @mycolors = qw(red green blue violet yellow black);
my @username_refs;
for my $ix ( 0 .. 5 ) {
my $username = "";
my $frame = $mw->Frame(
-borderwidth => 10,
-relief => 'groove',
-background => $mycolors[$ix],
)->pack;
my $lbl = $frame->Label( -text => "Enter username: " )->pack;
my $ent = $frame->Entry( -textvariable => \$username )->pack;
my $btn = $frame->Button(
-text => "Delete frame # " . $ix,
#~ -command => [ $frame, 'destroy' ]
-command => \&delete_parent_frame
)->pack;
push @username_refs, \$username;
}
my $exit = $mw -> Button(
-text => "Exit",
-command => [ $mw, 'destroy' ],
)-> pack;
$mw->$_ for qw/ withdraw deiconify raise focusForce update /;
MainLoop;
#~ print map { "You entered $$_\n" } @username_refs;
dd\@username_refs;
} ## end sub GoTk
sub delete_parent_frame {
my $button = $Tk::event->W;
my $frame = $button->parent;
#~ my $text = ( $frame->children )[1];
#~ my $text = ( grep { $_->isa('Tk::Entry') } $frame->children
+)[0];
my( $text ) = grep { $_->isa( 'Tk::Entry' ) } $frame->children;
#~ my $textvar = $text->cget( '-text' ); ## WEIRD!
my $textvar = $text->cget( '-textvariable' );
print "You entered $textvar : $$textvar\n";
my $textstr = $text->get;
print "You did entered $textstr\n";
$frame->destroy;
return;
} ## end sub delete_parent_frame
Read the tutorials in links in readmore in Re: Entry Edit Refresh (nested subs and closures)
Also, Hi, perlmonks has threaded discussions :) so click the reply link of the node you wish to reply to -- otherwise you're talking to yourself :) which coincidentally is a pretty good debugging technique :) 1 / 2