# HG changeset patch # User Heiko Schlittermann (JUMPER) # Date 1406662501 -7200 # Node ID 0fc33e54b1c63f676ed61424642a9b56f0a5d353 # Parent f196858c983ec60d9bddc3dbae8e587b8d79c0d0 Renamed to Scalar::LockRefType diff -r f196858c983e -r 0fc33e54b1c6 Build.PL --- a/Build.PL Tue Jul 29 21:27:29 2014 +0200 +++ b/Build.PL Tue Jul 29 21:35:01 2014 +0200 @@ -2,9 +2,9 @@ use Module::Build; Module::Build->new( - module_name => 'Scalar::RefType', + module_name => 'Scalar::LockRefType', dist_abstract => 'simple scalar ref type checker', - version_from => 'lib/Scalar/RefType.pm', + version_from => 'lib/Scalar/LockRefType.pm', build_requires => { Test::Exception => 0, }, diff -r f196858c983e -r 0fc33e54b1c6 MANIFEST --- a/MANIFEST Tue Jul 29 21:27:29 2014 +0200 +++ b/MANIFEST Tue Jul 29 21:35:01 2014 +0200 @@ -1,6 +1,6 @@ Build.PL -lib/Scalar/RefType.pm +lib/Scalar/LockRefType.pm MANIFEST This list of files -t/10-usage.t +META.json META.yml -META.json +t/10-usage.t diff -r f196858c983e -r 0fc33e54b1c6 MANIFEST.SKIP --- a/MANIFEST.SKIP Tue Jul 29 21:27:29 2014 +0200 +++ b/MANIFEST.SKIP Tue Jul 29 21:35:01 2014 +0200 @@ -7,7 +7,7 @@ ,v$ \B\.svn\b \B\.git\b -\B\.hg\b +\B\.hg \B\.gitignore\b \b_darcs\b \B\.cvsignore$ diff -r f196858c983e -r 0fc33e54b1c6 lib/Scalar/LockRefType.pm --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/Scalar/LockRefType.pm Tue Jul 29 21:35:01 2014 +0200 @@ -0,0 +1,59 @@ +package Scalar::LockRefType; +use strict; +use warnings; +use Carp; + +our $VERSION = '0.02'; + +sub TIESCALAR { + my ($class, $type) = @_; + return bless { + value => undef, + type => @_ < 2 ? undef + : ref($type) ? ref($type) + : length($type) ? $type + : '' + }; +} + +sub FETCH { return $_[0]->{value} } + +sub STORE { + my ($self, $value) = @_; + my $ref = ref $value // ''; + $self->{type} //= $ref; + croak 'invalid reference type' if $ref ne $self->{type}; + return $self->{value} = $value; +} + +__END__ + +=head1 NAME + + Scalar::LockRefType - simple scalar type checker + +=head1 SYNOPSIS + + use Scalar::LockRefType; + + tie my $h1 => 'Scalar::LockRefType', {}; + tie my $h2 => 'Scalar::LockRefType', 'HASH'; + tie my $h3 => 'Scalar::LockRefType'; + + $h1 = []; # dies, violates the type + $h2 = []; # dies, violates the type + + $h3 = {}; # sets the type + $h3 = []; # dies + +=head1 DESCRIPTION + +This little module allows you to tie the type of a scalar to a specified +reference type. If the refererence type of an assignment violates the +tied type, the assignment throws an exception. + +=head1 AUTHOR + +Heiko Schlittermann + +=cut diff -r f196858c983e -r 0fc33e54b1c6 lib/Scalar/RefType.pm --- a/lib/Scalar/RefType.pm Tue Jul 29 21:27:29 2014 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,59 +0,0 @@ -package Scalar::RefType; -use strict; -use warnings; -use Carp; - -our $VERSION = '0.02'; - -sub TIESCALAR { - my ($class, $type) = @_; - return bless { - value => undef, - type => @_ < 2 ? undef - : ref($type) ? ref($type) - : length($type) ? $type - : '' - }; -} - -sub FETCH { return $_[0]->{value} } - -sub STORE { - my ($self, $value) = @_; - my $ref = ref $value // ''; - $self->{type} //= $ref; - croak 'invalid reference type' if $ref ne $self->{type}; - return $self->{value} = $value; -} - -__END__ - -=head1 NAME - - Scalar::RefType - simple scalar type checker - -=head1 SYNOPSIS - - use Scalar::RefType; - - tie my $h1 => 'Scalar::RefType', {}; - tie my $h2 => 'Scalar::RefType', 'HASH'; - tie my $h3 => 'Scalar::RefType'; - - $h1 = []; # dies, violates the type - $h2 = []; # dies, violates the type - - $h3 = {}; # sets the type - $h3 = []; # dies - -=head1 DESCRIPTION - -This little module allows you to tie the type of a scalar to a specified -reference type. If the refererence type of an assignment violates the -tied type, the assignment throws an exception. - -=head1 AUTHOR - -Heiko Schlittermann - -=cut diff -r f196858c983e -r 0fc33e54b1c6 t/10-usage.t --- a/t/10-usage.t Tue Jul 29 21:27:29 2014 +0200 +++ b/t/10-usage.t Tue Jul 29 21:35:01 2014 +0200 @@ -5,13 +5,13 @@ use Test::More; use Test::Exception; -use_ok 'Scalar::RefType' or BAIL_OUT q{Can't load the module}; +use_ok 'Scalar::LockRefType' or BAIL_OUT q{Can't load the module}; subtest 'NAMES' => sub { - isa_ok tie(my $h => 'Scalar::RefType', ref {}) => 'Scalar::RefType'; - isa_ok tie(my $a => 'Scalar::RefType', ref []) => 'Scalar::RefType'; - isa_ok tie(my $s => 'Scalar::RefType', ref \(undef)) => 'Scalar::RefType'; - isa_ok tie(my $p => 'Scalar::RefType', '') => 'Scalar::RefType'; + isa_ok tie(my $h => 'Scalar::LockRefType', ref {}) => 'Scalar::LockRefType'; + isa_ok tie(my $a => 'Scalar::LockRefType', ref []) => 'Scalar::LockRefType'; + isa_ok tie(my $s => 'Scalar::LockRefType', ref \(undef)) => 'Scalar::LockRefType'; + isa_ok tie(my $p => 'Scalar::LockRefType', '') => 'Scalar::LockRefType'; is tied($h)->{type}, 'HASH' => 'is a hash ref'; is tied($a)->{type}, 'ARRAY' => 'is a array ref'; @@ -21,16 +21,16 @@ }; subtest 'AUTO' => sub { - isa_ok tie(my $x => 'Scalar::RefType') => 'Scalar::RefType'; + isa_ok tie(my $x => 'Scalar::LockRefType') => 'Scalar::LockRefType'; ok $x = {} => 'hash: assignment'; is tied($x)->{type}, 'HASH' => 'hash: type'; throws_ok { $x = [] } qr/invalid/ => 'invalid type assignment'; }; -isa_ok tie(my $h => 'Scalar::RefType', {}) => 'Scalar::RefType'; -isa_ok tie(my $a => 'Scalar::RefType', []) => 'Scalar::RefType'; -isa_ok tie(my $s => 'Scalar::RefType', \(undef)) => 'Scalar::RefType'; -isa_ok tie(my $p => 'Scalar::RefType', undef) => 'Scalar::RefType'; +isa_ok tie(my $h => 'Scalar::LockRefType', {}) => 'Scalar::LockRefType'; +isa_ok tie(my $a => 'Scalar::LockRefType', []) => 'Scalar::LockRefType'; +isa_ok tie(my $s => 'Scalar::LockRefType', \(undef)) => 'Scalar::LockRefType'; +isa_ok tie(my $p => 'Scalar::LockRefType', undef) => 'Scalar::LockRefType'; is tied($h)->{type}, 'HASH' => 'is a hash ref'; is tied($a)->{type}, 'ARRAY' => 'is a array ref';