diff options
author | Salvatore Bonaccorso <carnil@debian.org> | 2016-08-20 13:13:32 +0200 |
---|---|---|
committer | Salvatore Bonaccorso <carnil@debian.org> | 2016-08-20 13:13:32 +0200 |
commit | 56da6157af9dac709e8d8de75c9145575b1d8169 (patch) | |
tree | ed0e3116ed4d0a2e1534eddfa26c6344b70e45ba | |
parent | e98dfd90cf88a5e4e721dd9d421a47e6577c891e (diff) |
Imported Upstream version 0.003004
-rw-r--r-- | Changes | 18 | ||||
-rw-r--r-- | MANIFEST | 2 | ||||
-rw-r--r-- | META.json | 2 | ||||
-rw-r--r-- | META.yml | 2 | ||||
-rw-r--r-- | Makefile.PL | 2 | ||||
-rw-r--r-- | README | 2 | ||||
-rw-r--r-- | lib/Var/Pairs.pm | 10 | ||||
-rw-r--r-- | lib/Var/Pairs/Pair_BuiltIn.pm | 86 | ||||
-rw-r--r-- | lib/Var/Pairs/Pair_DataAlias.pm | 83 |
9 files changed, 198 insertions, 9 deletions
@@ -92,3 +92,21 @@ Revision history for Var-Pairs Refactor Var::Pairs::Pair to avoid use of Data::Alias under Perl 5.24+ (thanks Salvatore) + + + +0.003002 Sat Aug 20 16:49:20 2016 + + Updated MANIFEST + + Documented reliance on Data::Alias + + + + +0.003004 Sat Aug 20 19:18:50 2016 + + Adjusted requirement for Data::Alias + down to pre-5.22 + + @@ -3,6 +3,8 @@ MANIFEST Makefile.PL README lib/Var/Pairs.pm +lib/Var/Pairs/Pair_BuiltIn.pm +lib/Var/Pairs/Pair_DataAlias.pm t/00.load.t t/array.t t/array_while.t @@ -41,5 +41,5 @@ } }, "release_status" : "stable", - "version" : "0.003000" + "version" : "0.003004" } @@ -23,4 +23,4 @@ requires: PadWalker: '1.93' Test::More: '0' experimental: '0' -version: '0.003000' +version: '0.003004' diff --git a/Makefile.PL b/Makefile.PL index 4b3c92b..dd1c320 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -12,7 +12,7 @@ WriteMakefile( PREREQ_PM => { 'Test::More' => 0, 'Devel::Callsite' => 0.06, - 'Data::Alias' => 1.16, +($] < 5.022 ? ('Data::Alias' => 1.16) : ()), 'PadWalker' => 1.93, 'experimental' => 0, }, @@ -1,4 +1,4 @@ -Var::Pairs version 0.003000 +Var::Pairs version 0.003004 This module exports a small number of subroutines that add some Perl 6 conveniences to Perl 5. Specifically, diff --git a/lib/Var/Pairs.pm b/lib/Var/Pairs.pm index 9aa296b..ef5e6f4 100644 --- a/lib/Var/Pairs.pm +++ b/lib/Var/Pairs.pm @@ -1,6 +1,6 @@ package Var::Pairs; -our $VERSION = '0.003000'; +our $VERSION = '0.003004'; use 5.014; use warnings; @@ -277,8 +277,8 @@ sub _get_each_kv { return wantarray ? @each_kv : $each_kv[0]; } -use if $] < 5.024, 'Var::Pairs::Pair_DataAlias'; -use if $] >= 5.024, 'Var::Pairs::Pair_BuiltIn'; +use if $] < 5.022, 'Var::Pairs::Pair_DataAlias'; +use if $] >= 5.022, 'Var::Pairs::Pair_BuiltIn'; 1; # Magic true value required at end of module __END__ @@ -291,7 +291,7 @@ Var::Pairs - OO iterators and pair constructors for variables =head1 VERSION -This document describes Var::Pairs version 0.003000 +This document describes Var::Pairs version 0.003004 =head1 SYNOPSIS @@ -774,7 +774,7 @@ The module requires Perl 5.014 and the following modules: =item Devel::Callsite -=item Data::Alias +=item Data::Alias (under Perl 5.20 and earlier) =item PadWalker diff --git a/lib/Var/Pairs/Pair_BuiltIn.pm b/lib/Var/Pairs/Pair_BuiltIn.pm new file mode 100644 index 0000000..4306e1f --- /dev/null +++ b/lib/Var/Pairs/Pair_BuiltIn.pm @@ -0,0 +1,86 @@ +package +Var::Pairs::Pair_BuiltIn; + +use strict; +use warnings; +use experimental 'refaliasing'; + +# Class implementing each key/value pair... +# (aliasing via 5.22 built-in aliasing) +package Var::Pairs::Pair { + use Scalar::Util qw< looks_like_number >; + + use Carp; + + # Each pair object has two attributes... + my @key_for; + my @value_for; + my @freed; + + # Accessors for the attributes (value is read/write)... + sub value :lvalue { $value_for[${shift()}] } + sub index { $key_for[${shift()}] } + sub key { $key_for[${shift()}] } + sub kv { my $self = shift; $key_for[$$self], $value_for[$$self] } + + # The usual inside-out constructor... + sub new { + my ($class, $key, $container_ref, $container_type) = @_; + + # Create a scalar based object... + my $scalar = @key_for; + my $new_obj = bless \$scalar, $class; + + # Initialize its attributes (value needs to be an alias to the original)... + $key_for[$scalar] = $key; + \$value_for[$scalar] = $container_type eq 'array' ? \$container_ref->[$key] + : $container_type eq 'none' ? \$_[2] + : \$container_ref->{$key}; + $freed[$scalar] = 0; + + return $new_obj; + } + + # Type coercions... + use overload ( + # As a string, a pair is just: key => value + q{""} => sub { + my $self = shift; + my $value = $value_for[$$self]; + $value = ref $value ? ref $value + : looks_like_number($value) ? $value + : qq{"$value"}; + return "$key_for[$$self] => $value"; + }, + + # Can't numerify a pair (make it a hanging offence)... + q{0+} => sub { croak "Can't convert Pair(".shift.") to a number" }, + + # All pairs are true (just as in Perl 6)... + q{bool} => sub { !!1 }, + + # Everything else as normal... + fallback => 1, + ); + + sub DESTROY { + my $self = shift; + + # Mark current storage as reclaimable... + $freed[$$self] = 1; + + # Reclaim everything possible... + if ($freed[$#freed]) { + my $free_from = $#freed; + while ($free_from >= 0 && $freed[$free_from]) { + $free_from--; + } + splice @key_for, $free_from+1; + splice @value_for, $free_from+1; + splice @freed, $free_from+1; + } + } +} + +# Magic true value required at the end of a module... +1; diff --git a/lib/Var/Pairs/Pair_DataAlias.pm b/lib/Var/Pairs/Pair_DataAlias.pm new file mode 100644 index 0000000..d4bc361 --- /dev/null +++ b/lib/Var/Pairs/Pair_DataAlias.pm @@ -0,0 +1,83 @@ +package Var::Pairs::Pair; +use warnings; + +# Class implementing each key/value pair... +# (aliasing via Data::Alias) +package Var::Pairs::Pair { + use Scalar::Util qw< looks_like_number >; + use Data::Alias; + use Carp; + + # Each pair object has two attributes... + my @key_for; + my @value_for; + my @freed; + + # Accessors for the attributes (value is read/write)... + sub value :lvalue { $value_for[${shift()}] } + sub index { $key_for[${shift()}] } + sub key { $key_for[${shift()}] } + sub kv { my $self = shift; $key_for[$$self], $value_for[$$self] } + + # The usual inside-out constructor... + sub new { + my ($class, $key, $container_ref, $container_type) = @_; + + # Create a scalar based object... + my $scalar = @key_for; + my $new_obj = bless \$scalar, $class; + + # Initialize its attributes (value needs to be an alias to the original)... + $key_for[$scalar] = $key; + alias $value_for[$scalar] = $container_type eq 'array' ? $container_ref->[$key] + : $container_type eq 'none' ? $_[2] + : $container_ref->{$key}; + $freed[$scalar] = 0; + + return $new_obj; + } + + # Type coercions... + use overload ( + # As a string, a pair is just: key => value + q{""} => sub { + my $self = shift; + my $value = $value_for[$$self]; + $value = ref $value ? ref $value + : looks_like_number($value) ? $value + : qq{"$value"}; + return "$key_for[$$self] => $value"; + }, + + # Can't numerify a pair (make it a hanging offence)... + q{0+} => sub { croak "Can't convert Pair(".shift.") to a number" }, + + # All pairs are true (just as in Perl 6)... + q{bool} => sub { !!1 }, + + # Everything else as normal... + fallback => 1, + ); + + sub DESTROY { + my $self = shift; + + # Mark current storage as reclaimable... + $freed[$$self] = 1; + + # Reclaim everything possible... + if ($freed[$#freed]) { + my $free_from = $#freed; + while ($free_from >= 0 && $freed[$free_from]) { + $free_from--; + } + splice @key_for, $free_from+1; + splice @value_for, $free_from+1; + splice @freed, $free_from+1; + } + } +} + +# Magic true value required at the end of a module... +1; + |