A singleton with multiple constructor names?
"James FitzGibbon" <[email protected]> Fri, 10 Jun 2005 15:46:23 -0400
| Newsgroups | gmane.comp.lang.perl.modules.class-methodmaker |
|---|---|
| Organization | Primus Telecommunications Canada Inc. |
| Message-ID | <[email protected]> |
Given the following two classes:
--START Foo.pm--
package Foo;
use Class::MethodMaker
[
new => [ qw|-singleton new instance| ],
];
1;
---END Foo.pm---
--START Bar.pm--
package Bar;
use Class::MethodMaker
[
new => [ qw|-singleton new| ],
];
sub instance { shift->new(@_) }
1;
---END Bar.pm---
And this test script:
--START foo.pl--
use Foo;
use Bar;
print "Foo\n\n";
$foo = Foo->new;
print "after first call to ->new, \$foo = $foo\n";
$foo = Foo->new;
print "after second call to ->new, \$foo = $foo\n";
$foo = Foo->instance;
print "after first call to ->instance, \$foo = $foo\n";
$foo = Foo->instance;
print "after second call to ->instance, \$foo = $foo\n";
print "\n";
print "Bar\n\n";
$bar = Bar->new;
print "after first call to ->new, \$bar = $bar\n";
$bar = Bar->new;
print "after second call to ->new, \$bar = $bar\n";
$bar = Bar->instance;
print "after first call to ->instance, \$bar = $bar\n";
$bar = Bar->instance;
print "after second call to ->instance, \$bar = $bar\n";
---END foo.pl---
I get this output:
Foo
after first call to ->new, $foo = Foo=HASH(0x811f948)
after second call to ->new, $foo = Foo=HASH(0x811f948)
after first call to ->instance, $foo = Foo=HASH(0x811fa8c)
after second call to ->instance, $foo = Foo=HASH(0x811fa8c)
Bar
after first call to ->new, $bar = Bar=HASH(0x812032c)
after second call to ->new, $bar = Bar=HASH(0x812032c)
after first call to ->instance, $bar = Bar=HASH(0x812032c)
after second call to ->instance, $bar = Bar=HASH(0x812032c)
Notice that Foo has two constructors, both of which are singletons. But the
singleton-ness of the class
is being evaluated per-constructor, not per-class. So I end up with two
distinct Foo objects rather than
one.
Is this the expected behaviour? If so, is what I've done with Bar the
proper way to have a class that is
a singleton but can use ->new or ->instance interchangeably?
Thanks
--
j.
James FitzGibbon
Systems Developer, Primus Telecommunications Canada Inc.
416.644.6111
--
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.323 / Virus Database: 267.6.6 - Release Date: 6/8/2005
--
----------------------------------------------------------------------------
This electronic message contains information from Primus Telecommunications
Canada Inc. ("PRIMUS") , which may be legally privileged and confidential.
The information is intended to be for the use of the individual(s) or entity
named above. If you are not the intended recipient, be aware that any
disclosure, copying, distribution or use of the contents of this information
is prohibited. If you have received this electronic message in error, please
notify us by telephone or e-mail (to the number or address above)
immediately. Any views, opinions or advice expressed in this electronic
message are not necessarily the views, opinions or advice of PRIMUS.
It is the responsibility of the recipient to ensure that
any attachments are virus free and PRIMUS bears no responsibility
for any loss or damage arising in any way from the use
thereof.The term "PRIMUS" includes its affiliates.
----------------------------------------------------------------------------
Pour la version en français de ce message, veuillez voir
http://www.primustel.ca/fr/legal/cs.htm
----------------------------------------------------------------------------
-------------------------------------------------------
This SF.Net email is sponsored by: NEC IT Guy Games. How far can you shotput
a projector? How fast can you ride your desk chair down the office luge track?
If you want to score the big prize, get to know the little guy.
Play to win an NEC 61" plasma display: http://www.necitguy.com/?r=20