Scalar-Accessors-LikeHash

 view release on metacpan or  search on metacpan

lib/Tie/Hash/SerializedString.pm  view on Meta::CPAN

package Tie::Hash::SerializedString;

use 5.008;
use strict;
use warnings;

our $AUTHORITY = 'cpan:TOBYINK';
our $VERSION   = '0.002';

use base "Tie::Hash";
use Carp;
use Module::Runtime;

sub TIEHASH
{
	my $class = shift;
	my ($ref, $implementation) = @_;
	croak "need a scalar ref to tie hash to" unless ref $ref eq 'SCALAR';
	$implementation = "Scalar::Accessors::LikeHash::JSON" unless defined $implementation;
	Module::Runtime::use_package_optimistically($implementation);
	bless [$implementation, $ref] => $class;
}

for my $method (qw( STORE FETCH EXISTS DELETE CLEAR ))
{
	my $lc_method = lc $method;
	my $coderef = sub {
		my ($implementation, $ref) = @{+shift};
		return $implementation->$lc_method($ref, @_);
	};
	no strict 'refs';
	*$method = $coderef;
}

sub FIRSTKEY
{
	my ($implementation, $ref) = @{+shift};
	my @keys = $implementation->keys($ref);
	return $keys[0];
}

sub NEXTKEY
{
	my ($implementation, $ref) = @{+shift};
	my ($lastkey) = @_;
	my @keys = $implementation->keys($ref);
	while (@keys)
	{
		my $this = shift @keys;
		return $keys[0] if $this eq $lastkey && @keys;
	}
	return;
}

sub SCALAR
{
	my ($implementation, $ref) = @{+shift};
	return $$ref;
}

1;

__END__

=head1 NAME

Tie::Hash::SerializedString - tied interface for Scalar::Accessors::LikeHash

=head1 SYNOPSIS

   my $string = '{}';
   tie my %hash, "Tie::Hash::SerializedString", \$string;
   
   $hash{foo} = "bar";
   
   print $string;   # prints '{"foo":"bar"}'

=head1 DESCRIPTION

This provides a tied hash wrapper around L<Scalar::Accessors::LikeHash>
implementations.

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 1.320 second using v1.00-cache-2.02-grep-82fe00e-cpan-9f2165ba459b )