<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
<br>
<br>
Leigh Sharpe wrote:
<blockquote
cite="mid:96CF49BD8B56384395D698BA99007FA347A288@exchange.pacwire.local"
type="cite">
<meta http-equiv="Content-Type" content="text/html; ">
<meta content="MSHTML 6.00.6000.16788" name="GENERATOR">
<div><span class="791150423-08122008"><font face="Arial" size="2">Hi
All,</font></span></div>
<div><span class="791150423-08122008"><font face="Arial" size="2"> I'm
working on a TK app, and I'm struggling with a few things.</font></span></div>
<div><span class="791150423-08122008"><font face="Arial" size="2">I
have a function which looks a bit like this:</font></span></div>
<div><span class="791150423-08122008"></span> </div>
<div><span class="791150423-08122008"><font face="Arial" size="2">sub
new_object</font></span></div>
<div><span class="791150423-08122008"><font face="Arial" size="2">{</font></span></div>
<div><span class="791150423-08122008"><font face="Arial" size="2"> my
$object=My::Class->new(); # Create a new object to monitor<br>
my $tl=$mw->Toplevel; # Create a top-level
window.<br>
$tl->title($object->{'name'}); # Set the title of
the new window.<br>
$tl->Label(-text=>"Name:")->grid(-row=>0,
-column=>0); # Add Labels to window.<br>
$tl->Label(-textvariable=>\$object->{'name'})->grid(-row=>0,
-column=>1);<br>
$tl->Label(-text=>"IP Address")->grid(-row=>1,
-column=>0);<br>
$tl->Label(-textvariable=>\$object->{'ip'})->grid(-row=>1,
-column=>1);<br>
$tl->Label(-text=>"Uptime:")->grid(-row=>2, -column=>0);<br>
$tl->Label(-textvariable=>\$object->{'uptime'})->grid(-row=>2,
-column=>1);<br>
$tl->Button(-text=>"Exit",
-command=>sub{$tl->destroy()})->grid(-row=>20,
-column=>0); # Create an exit button.<br>
$tl->repeat(5000, sub{$object->update_status()}); # Update the
status every 5 seconds.<br>
}</font></span></div>
<div><span class="791150423-08122008"></span> </div>
<div><span class="791150423-08122008"><font face="Arial" size="2">Now
this works as it is, but I just know it won't scale well. I would
rather have a single class method I could call which would run
update_status() on all objects of it's class, and call it from the main
window ($mw in this case). So my first question is, how can a class
access all instances of itself? I considered creating an array in which
I could store a reference to all objects, but that just seems wrong,
'cause it will prevent objects from going out of scope when all other
references to them are deleted.</font></span></div>
</blockquote>
I'm assuming that this new_object function is in the package
"Package::Blah". If not, then it probably should be.<br>
<br>
So:<br>
<br>
package Package::Blah;<br>
our $SOMEVARIABLE;<br>
<br>
then in your other code:<br>
<br>
$Package::Blah::SOMEVARIABLE = "fred";<br>
<br>
ie: just refer to the full package name.<br>
<blockquote
cite="mid:96CF49BD8B56384395D698BA99007FA347A288@exchange.pacwire.local"
type="cite">
<div> </div>
<div><span class="791150423-08122008"><font face="Arial" size="2">The
second question is in relation to keeping $object in scope. If I remove
the last line of the above function, $object goes out of scope as soon
as the function completes. Having a label with
-textvariable=>\$object{'anything'} isn't sufficient to keep $object
in scope.</font></span></div>
</blockquote>
Then you need to keep a package reference to it, eg in Package::Blah:<br>
<br>
our @SOMELIST;<br>
push @SOMELIST, $object;<br>
<br>
<blockquote
cite="mid:96CF49BD8B56384395D698BA99007FA347A288@exchange.pacwire.local"
type="cite">
<div><span class="791150423-08122008"><font face="Arial" size="2"><span
class="791150423-08122008">So, if I was to create a class method as
required, there would be no objects to call update_status() on.</span></font></span></div>
</blockquote>
map { $_->update() } @Package::Blah::SOMELIST;<br>
<blockquote
cite="mid:96CF49BD8B56384395D698BA99007FA347A288@exchange.pacwire.local"
type="cite">
<div><font face="Arial" size="2">However, with the function as it is
above, even destroying $tl (when the user clicks on the 'Exit' button)
doesn't call DESTROY on $object. There must be some reference to the
object somewhere<span class="791150423-08122008">, created by
$tl->repeat(), which isn't going out of scope when $tl is
destroy()ed.</span></font></div>
</blockquote>
Are you sure its not being destroyed?<br>
<br>
cheers,<br>
Mathew<br>
</body>
</html>