The problem to add all objects to a stack (your @pack array) is that this will break Object destruction, since you always will have a reference to any object in the @pack list. To avoid that you can use the module
Hash::NoRef, where you can add values to this hash without count the references to this values, keeping the behavior of the garbage colection system.
package Dog ;
use Hash::NoRef ;
use vars qw(%GHOST_TABLE) ;
tie(%GHOST_TABLE , 'Hash::NoRef') ;
my $GHOST_ID ;
sub new {
my $class = shift ;
my $this = bless( {} , $class ) ;
++$GHOST_ID ;
$GHOST_TABLE->{$GHOST_ID} = $this ;
return $this
}
package main ;
{
my $obj = new Dog() ;
print $GHOST_TABLE->{1} ; ## will show the object reference
}
print $GHOST_TABLE->{1} ; ## will show undef
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.