in reply to Modules exporting functions and objects at the same time.
So you only have one instance of the object? That's known as a singleton.
You could also create a static class.
package StaticClass; my $attrib = '...'; # print StaticClass->method(); sub method { my ($class) = @_; return $attrib; } 1;
But I like returning an object since it provides a nice upgrade path.
package SingletonClass; my $singleton; sub new { return $singleton ||= bless({ attrib => '...', }); } # print $obj->method(); sub method { my ($self) = @_; return $self->{attrib}; } 1;
Shouldn't this be posted in Meditations?
It's a question, so SoPW is definitely appropriate. I don't mind seeing questions meant to illicit discussion in Meditations, though. Both sections are appropriate for some posts.
|
|---|