Skip Menu |

This queue is for tickets about the Class-WeakSingleton CPAN distribution.

Report information
The Basics
Id: 68524
Status: new
Priority: 0/
Queue: Class-WeakSingleton

People
Owner: Nobody in particular
Requestors: shay [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Normal
Broken in: 1.05
Fixed in: (no value)



Subject: [PATCH] Default _new_instance() implementation could be more useful
Any additional arguments passed to the instance() method are forwarded to the _new_instance() method, but the default implementation of that does nothing with them. In the Class-Singleton module the default implementation of _new_instance() copies any additional arguments received into the new object, so if you're happy to work with a simple hash reference based object and you want to initialize attributes in it by passing arguments to the instance() method then there is no need to provide your own _new_instance() method to achieve that. As an example, the following program prints "str = x": package Foo; use parent qw(Class::Singleton); package main; my $foo = Foo->instance(str => 'x'); print "str = ", ($foo->{str} // '<undef>'), "\n"; but if you use Class::WeakSingleton instead then it prints "str = <undef>". It would be helpful if Class-WeakSingleton provided a similar default implementation of _new_instance() to avoid needing to overload it in cases like the above where no very specialized behaviour is required. The attached patch does this by simply providing the same default implementation as Class::Singleton.
Subject: new.patch
diff -ruN Class-WeakSingleton-1.05.orig/lib/Class/WeakSingleton.pm Class-WeakSingleton-1.05/lib/Class/WeakSingleton.pm --- Class-WeakSingleton-1.05.orig/lib/Class/WeakSingleton.pm 2008-11-13 18:21:44.000000000 +0000 +++ Class-WeakSingleton-1.05/lib/Class/WeakSingleton.pm 2011-05-28 16:40:47.375000000 +0100 @@ -110,8 +110,8 @@ sub _new_instance { my $class = shift; - - return bless {}, $class; + my %args = @_ && ref $_[0] eq 'HASH' ? %{ $_[0] } : @_; + return bless { %args }, $class; } =back