Config::Hierarchical::Tie::ReadOnly - Access Hierarchical configuration container through a read only hash


Config-Hierarchical documentation Contained in the Config-Hierarchical distribution.

Index


Code Index:

NAME

Top

Config::Hierarchical::Tie::ReadOnly - Access Hierarchical configuration container through a read only hash

SYNOPSIS

Top

  


	my $config = new Config::Hierarchical
				(
				NAME => 'config',

				CATEGORY_NAMES   => ['A', 'B'],
				DEFAULT_CATEGORY => 'B',

				INITIAL_VALUES  =>
					[
					{CATEGORY => 'A', NAME => 'CC1', VALUE => '1'},
					{CATEGORY => 'B', NAME => 'CC2', VALUE => '2'},
					{CATEGORY => 'A', NAME => 'CC3', VALUE => '3'},
					{CATEGORY => 'B', NAME => 'CC4', VALUE => '4'},
					{CATEGORY => 'A', NAME => 'CC5', VALUE => '5'},
					] ,
				) ;

	my %hash ;
	tie %hash, 'Config::Hierarchical::Tie::ReadOnly' => $config ;

	my @keys = sort keys %hash ; # qw( CC1 CC2 CC3 CC4 CC5)
	print $hash{CC1} ; # print '1'

	$hash{CC1} = 2 ; # dies, hash is read only

DESCRIPTION

Top

Creates a read only wrapper around a Config::Hierarchical object. This let's you access the config object as a hash. You can use {} access which makes it easy to use the config in interpolated string. You can also use keys and each on the tied config.

but you can't modify the variables, clear the config or delete any variable.

This is also class is also used to allow you to link a category to an existing Config::Hierarchical object. See new in <Config::Hierarchical>.

DOCUMENTATION

Top

SUBROUTINES/METHODS

Top

TIEHASH

The method invoked by the command tie %hash, class name. Associates a new hash instance with the specified class.

STORE

Dies as this tie is read only.

FETCH

Retrieve the value associated with the configuration variable passed as argument

FIRSTKEY

Return the first key in the hash. Used internally by Perl.

NEXTKEY

Return the next key in the hash. Used internally by Perl.

EXISTS

Verify that key exists within the tied Config::Hierarchical.

DELETE

Dies as this tie is read only.

CLEAR

Dies as this tie is read only.

SCALAR

returns the number of elements in the tied Config::Hierarchical object.

BUGS AND LIMITATIONS

Top

None so far.

AUTHOR

Top

	Khemir Nadim ibn Hamouda
	CPAN ID: NKH
	mailto:nadim@khemir.net

LICENSE AND COPYRIGHT

Top

SUPPORT

Top

You can find documentation for this module with the perldoc command.

    perldoc Config::Hierarchical

You can also look for information at:

* AnnoCPAN: Annotated CPAN documentation

http://annocpan.org/dist/Config-Hierarchical

* RT: CPAN's request tracker

Please report any bugs or feature requests to L <bug-config-hierarchical@rt.cpan.org>.

We will be notified, and then you'll automatically be notified of progress on your bug as we make changes.

* Search CPAN

http://search.cpan.org/dist/Config-Hierarchical

SEE ALSO

Top


Config-Hierarchical documentation Contained in the Config-Hierarchical distribution.
package Config::Hierarchical::Tie::ReadOnly ;

use strict;
use warnings ;

BEGIN 
{
#~ use Exporter ();

use vars qw ($VERSION @ISA @EXPORT_OK %EXPORT_TAGS);

$VERSION     = 0.01;
@EXPORT_OK   = qw ();
%EXPORT_TAGS = ();
}

#-------------------------------------------------------------------------------

use Carp ;
use base qw(Tie::Hash) ;

use English qw( -no_match_vars ) ;

use Readonly ;
Readonly my $EMPTY_STRING => q{} ;

#-------------------------------------------------------------------------------

sub TIEHASH 
{
my ($class, @arguments) = @_ ;

unless('Config::Hierarchical' eq ref $arguments[0])
	{
	croak "Argument must be a 'Config::Hierarchical' object!\n" ;
	}
	
my $self = {CONFIG => $arguments[0]} ;
bless($self, $class) ;

return($self) ;
}

#-------------------------------------------------------------------------------

sub STORE
{ ## no critic (RequireFinalReturn)
	
my ($this, $key, $value) = @_ ;

my (undef, $filename, $line) = caller() ;
$this->{CONFIG}{INTERACTION}{DIE}->("This hash is read only at '$filename:$line'!\n") ;
}

#-------------------------------------------------------------------------------

sub FETCH 
{

my ($this, $key) = @_ ;

my (undef, $filename, $line) = caller() ;
return($this->{CONFIG}->Get(NAME => $key, FILE => $filename, LINE => $line)) ;
}

#-------------------------------------------------------------------------------

sub FIRSTKEY 
{

my ($this) = @_ ;

$this->{KEYS} = [$this->{CONFIG}->GetKeys()] ;
$this->{KEY_INDEX} = 0 ;

return $this->{KEYS}[$this->{KEY_INDEX}] ;
}

#-------------------------------------------------------------------------------

sub NEXTKEY 
{

my ($this, $lastkey) = @_ ;

$this->{KEY_INDEX}++ ;
return $this->{KEYS}[$this->{KEY_INDEX}] ;
}

#-------------------------------------------------------------------------------

sub EXISTS 
{

my ($this, $key) = @_ ;

return($this->{CONFIG}->Exists(NAME => $key)) ;
}

#-------------------------------------------------------------------------------

sub DELETE 
{ ## no critic (RequireFinalReturn)

my ($this, $key) =  @_ ;

my (undef, $filename, $line) = caller() ;
$this->{CONFIG}{INTERACTION}{DIE}->("This hash is read only at '$filename:$line'!\n") ;
}

#-------------------------------------------------------------------------------

sub CLEAR 
{ ## no critic (RequireFinalReturn)

my ($this) = @_ ;

my (undef, $filename, $line) = caller() ;
$this->{CONFIG}{INTERACTION}{DIE}->("This hash is read only at '$filename:$line'!\n") ;
}

#-------------------------------------------------------------------------------

sub SCALAR 
{

my ($this) = @_ ;

return($this->{CONFIG}->GetKeys()) ;
}
  
#-------------------------------------------------------------------------------

1 ;