Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: how to make a universally inherited method?

by lachoy (Parson)
on Nov 29, 2001 at 10:12 UTC ( [id://128294]=note: print w/replies, xml ) Need Help??


in reply to how to make a universally inherited method?

I agree with IlyaM, a singleton makes more sense for this. A singleton is an object that can only be instantiated once -- successive requests for a new object just return the same one.

The Class::Singleton module probably has one of the highest POD-to-code ratios on CPAN, but it's very useful.

Your config module:

package My::Config; use strict; use base qw( Class::Singleton ); sub _new_instance { my ( $class ) = @_; my %data = ( var1 => 'This is var1', var2 => [ 'This', 'is', 'var2' ], ); return bless( \%data, $class ); } 1;

And you can use it:

my $conf = My::Config->instance; print "Config for var1 is $conf->{var1}\n";

Easy! Good luck.

Chris
M-x auto-bs-mode

Replies are listed 'Best First'.
Re: Re: how to make a universally inherited method?
by exphysicist (Sexton) on Nov 29, 2001 at 10:17 UTC
    Wow, this is exciting. I finally have a work-related reason to open my copy of the Gang-of-four book.

    I'll try Class::Singleton.

    Thanks

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://128294]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (7)
As of 2024-04-19 07:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found