[Melbourne-pm] TK repeats, scoping and keeping track of objects

Toby Corkindale toby.corkindale at strategicdata.com.au
Mon Jan 12 18:36:39 PST 2009


Toby Corkindale wrote:
> Leigh Sharpe wrote:
>> Hi Jacinta,
>>
>>
>>> You can create an array of all the objects and then weaken those
>> references so that the object will still be
>>> cleaned up upon going out of scope.  You then want to make sure that
>> you handle holes in your array.  For
>>> example:
>>  
>> Thanks, that looks like the answer I'm after.
>>
>> Perhaps, however, I didn't explain my problem clearly enough:
>>
>>> Is there a reason you're not returning your object?  It seems odd to
>> worry about whether objects can properly be
>>> cleaned up when going out of scope (as you mentioned above) and then
>> not actually pass them back into the
>>> program so that you can use them in the scope for which you've created
>> them. 
>> I am returning an object. The function I had in my original email was
>> not in my package, it was in main. Here's a working example of what I'm
>> experiencing:
> 
> [snip]
> I note that the Tk docs state:
> "Note however that while a window exists for $widget the perl object is 
> maintained (due to "references" in perl/Tk internals) even though 
> original variables may have gone out of scope. (Normally this is 
> intuitive.)"
> 
> I suggest you look at the OnDestroy() callback instead of relying on the 
> Perl garbage collection here.

See attached for a quick demo, which seems to have the desired behaviour:
$ ./tkdemo.pl
new mainWindow opened.. at ./tkdemo.pl line 25.
# I click 'about'
new About dialog opened.. at ./tkdemo.pl line 48.
# I click 'close'
About->close button pressed at ./tkdemo.pl line 40.
myDestroy called, and left a voicemail. at ./tkdemo.pl line 59.
Destroy called.. and hung-up on me. at ./tkdemo.pl line 54.
# I click 'About' again..
new About dialog opened.. at ./tkdemo.pl line 48.
# and close it again..
About->close button pressed at ./tkdemo.pl line 40.
myDestroy called, and left a voicemail. at ./tkdemo.pl line 59.
Destroy called.. and hung-up on me. at ./tkdemo.pl line 54.
# now I close the main window
mainWindow->close button pressed at ./tkdemo.pl line 21.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: tkdemo.pl
Type: application/x-perl
Size: 1285 bytes
Desc: not available
URL: <http://mail.pm.org/pipermail/melbourne-pm/attachments/20090113/868968f0/attachment.bin>


More information about the Melbourne-pm mailing list