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

Re: failed AUTOLOAD widgets when trying to destory widget

by Anonymous Monk
on Oct 26, 2012 at 08:22 UTC ( #1001022=note: print w/ replies, xml ) Need Help??


in reply to failed AUTOLOAD widgets when trying to destory widget

If you had  use strict; use warnings: you would have been warned

Variable "$text" will not stay shared at file line 1

If you had used diagnostics you'd have gotten

(W closure) An inner (nested) named subroutine is referencing a lexical variable defined in an outer named subroutine. When the inner subroutine is called, it will see the value of the outer subroutine's variable as it was before and during the *first +* call to the outer subroutine; in this case, after the first call to th +e outer subroutine is complete, the inner and outer subroutines will no longer share a common value for the variable. In other words, the variable will no longer be shared. This problem can usually be solved by making the inner subroutine anonymous, using the sub {} syntax. When inner anonymous subs that reference variables in outer subroutines are created, they are automatically rebound to the current values of such variables.

Read Tutorials: Closure on Closures, Lexical scoping like a fox

If you're going to use named subs, use pre-declare any my variables (like $tl and $text ) in the largest possible scope (before any sub definitions) or simply use our globals, and you'll be fine with strict/warnings, and you'll avoid calling methods on destroyed object ( you'll avoid Failed to AUTOLOAD... )

Or , keep what you have, but use anonymous sub/anonymous closure

sub do_top { if ( !Exists($tl) ) { $tl = $mw->Toplevel; $tl->title('Help'); $tl->geometry("1000x700"); my $frame = $tl->Frame()->pack; my $lab = $tl->Label()->pack; my $ent = $tl->Entry()->pack; #### ANONYMOUS SUB , ANONYMOUS CLOSURE, #### CLOSED OVER LEXICAL $text #### $text is lexical to if {} block in do_top my $text = $tl->Scrolled('Text'); my $push_button2 = sub { $text->insert( 'end', "You pushed button2\n" ); }; $tl->Button( -text => 'Exit', -command => sub { $tl->destroy; +} )->pack; $tl->Button( -text => 'Open File1', -command => $push_button2 + )->pack; $text->pack(); } else { $tl->deiconify; $tl->raise; } }


Comment on Re: failed AUTOLOAD widgets when trying to destory widget
Select or Download Code
Re^2: failed AUTOLOAD widgets when trying to destory widget
by reaper9187 (Scribe) on Oct 26, 2012 at 10:26 UTC
    Thank you so much .!!!!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (9)
As of 2014-11-27 07:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (180 votes), past polls