<?xml version="1.0" encoding="windows-1252"?>
<node id="198375" title="Re: single instance shared with module" created="2002-09-16 17:25:47" updated="2005-06-08 10:51:43">
<type id="11">
note</type>
<author id="36507">
jynx</author>
<data>
<field name="doctext">
&lt;br&gt;
Brothers smack me down if i'm wrong, but recently i used something similar to this code to do what you're describing:&lt;code&gt;
#!/usr/bin/perl -w
use strict;

package foo;

{
  my $self = {
    foo =&gt; bar
  };

  sub new {
    return bless $self, shift;
  }

  sub get_obj {
    return UNIVERSAL::isa($self, 'foo')
    # or not as cool:
    #return ref($self) eq 'foo'
      ? $self
      : goto &amp;new;
  }
}
&lt;/code&gt;
This not only makes new instances when you want them, it also can pass on old instances to any class that requires it and uses the 'get_obj' method.&lt;p&gt;
Whether this is Good Coding(tm) or not, i can't say, but it's worked for me (and i might change what i have, depending on how monks respond to this :)&lt;p&gt;
jynx&lt;p&gt;
&lt;b&gt;Update&lt;/b&gt;: Added the UNIVERSAL::isa call and commented out the ref($self) call due to [podmaster]'s suggestion. Could someone please elaborate on why UNIVERSAL::isa is better?&lt;p&gt;
&lt;b&gt;Update2&lt;/b&gt;: Whoops. Put the args for UNIVERSAL::isa in wrong order. Hmm, i tested it before i posted and it seemed to work fine the other way as well. Tested again and apparently i was smoking crack. Mental note: RTFM first next time! ;-)&lt;p&gt;</field>
<field name="root_node">
198354</field>
<field name="parent_node">
198354</field>
</data>
</node>
