Config::Tiny::Singleton - singleton-pattern implementation for Config::Tiny


Config-Tiny-Singleton documentation Contained in the Config-Tiny-Singleton distribution.

Index


Code Index:

NAME

Top

Config::Tiny::Singleton - singleton-pattern implementation for Config::Tiny

SYNOPSIS

Top

	package MyProj::Config;
	use base qw/Config::Tiny::Singleton/;
	__PACKAGE__->file('/your/project/conf.file');
	1;

	package main;
	use MyProj::Config;

	my $var = MyProj::Config->instance->{section1}{key1};
	...

	# and in another package,

	package MyProj::Hoge;
	use MyProj::Config;
	my $var2 = MyProj::Config->instance->{section2}{key2};

DESCRIPTION

Top

You may make many modules and some config-files when you build large applications. There are several ways to handle configs. One is to create new config-object in each packages that need data set in config-files. However, this will make your app's performance pretty bad. Second is to let your context-object to keep your config-object. But your application become larger and larger, it'll be too hard to let your context to handle all configs. And third is, this is better, to implement singleton-pattern. Try that, and now you only need to call your configs instance method in your package where you want to use it in.

SEE ALSO

Top

Config::Tiny, Class::Singleton

AUTHOR

Top

Lyo Kato <kato@lost-season.jp>

COPYRIGHT AND LICENSE

Top


Config-Tiny-Singleton documentation Contained in the Config-Tiny-Singleton distribution.

package Config::Tiny::Singleton;
use strict;

our $VERSION = 0.02;

use base qw|
	Config::Tiny
	Class::Singleton
	Class::Data::Inheritable
|;

__PACKAGE__->mk_classdata($_) for qw/file errstr/;

sub _new_instance {
	my $class = shift;
	my $file  = $class->file;
	unless($file){
		require Carp;
		Carp::croak "set file before creating instance.";
	}
	return $class->read($file);
}

sub _error {
	my($self, $msg) = @_;
	$self->errstr($msg);
	undef;
}

1;
__END__