Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re: single instance shared with module

by jynx (Priest)
on Sep 16, 2002 at 21:25 UTC ( #198375=note: print w/replies, xml ) Need Help??

in reply to single instance shared with module

Brothers smack me down if i'm wrong, but recently i used something similar to this code to do what you're describing:
#!/usr/bin/perl -w use strict; package foo; { my $self = { foo => 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 &new; } }
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.

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 :)


Update: 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?

Update2: 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! ;-)

Replies are listed 'Best First'.
Re: Re: single instance shared with module
by Solo (Deacon) on Sep 16, 2002 at 22:39 UTC
    In brief, UNIVERSAL::isa allows for inheritance, while ref($self) doesn't.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://198375]
[Corion]: 1nickt: I imagine (heh) that Agile done somewhat right will make the process (and why we postpone things) somewhat more explicit to the outsider, or at least the "who do we ask to postpone X"
[choroba]: Corion ad functional: in Erlang, it's even more like Prolog, as its syntax is very close to Prolog's

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2017-12-11 12:08 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (290 votes). Check out past polls.