1N/A#!./perl
1N/A#
1N/A# Copyright (c) 1995-2000, Raphael Manfredi
1N/A#
1N/A# You may redistribute only under the same terms as Perl 5, as specified
1N/A# in the README file that comes with the distribution.
1N/A#
1N/A
1N/Asub BEGIN {
1N/A if ($ENV{PERL_CORE}){
1N/A chdir('t') if -d 't';
1N/A @INC = ('.', '../lib', '../ext/Storable/t');
1N/A } else {
1N/A unshift @INC, 't';
1N/A }
1N/A require Config; import Config;
1N/A if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bStorable\b/) {
1N/A print "1..0 # Skip: Storable was not built\n";
1N/A exit 0;
1N/A }
1N/A require 'st-dump.pl';
1N/A}
1N/A
1N/Asub ok;
1N/A
1N/Ause Storable qw(freeze thaw);
1N/A
1N/Aprint "1..23\n";
1N/A
1N/A($scalar_fetch, $array_fetch, $hash_fetch) = (0, 0, 0);
1N/A
1N/Apackage TIED_HASH;
1N/A
1N/Asub TIEHASH {
1N/A my $self = bless {}, shift;
1N/A return $self;
1N/A}
1N/A
1N/Asub FETCH {
1N/A my $self = shift;
1N/A my ($key) = @_;
1N/A $main::hash_fetch++;
1N/A return $self->{$key};
1N/A}
1N/A
1N/Asub STORE {
1N/A my $self = shift;
1N/A my ($key, $value) = @_;
1N/A $self->{$key} = $value;
1N/A}
1N/A
1N/Asub FIRSTKEY {
1N/A my $self = shift;
1N/A scalar keys %{$self};
1N/A return each %{$self};
1N/A}
1N/A
1N/Asub NEXTKEY {
1N/A my $self = shift;
1N/A return each %{$self};
1N/A}
1N/A
1N/Apackage TIED_ARRAY;
1N/A
1N/Asub TIEARRAY {
1N/A my $self = bless [], shift;
1N/A return $self;
1N/A}
1N/A
1N/Asub FETCH {
1N/A my $self = shift;
1N/A my ($idx) = @_;
1N/A $main::array_fetch++;
1N/A return $self->[$idx];
1N/A}
1N/A
1N/Asub STORE {
1N/A my $self = shift;
1N/A my ($idx, $value) = @_;
1N/A $self->[$idx] = $value;
1N/A}
1N/A
1N/Asub FETCHSIZE {
1N/A my $self = shift;
1N/A return @{$self};
1N/A}
1N/A
1N/Apackage TIED_SCALAR;
1N/A
1N/Asub TIESCALAR {
1N/A my $scalar;
1N/A my $self = bless \$scalar, shift;
1N/A return $self;
1N/A}
1N/A
1N/Asub FETCH {
1N/A my $self = shift;
1N/A $main::scalar_fetch++;
1N/A return $$self;
1N/A}
1N/A
1N/Asub STORE {
1N/A my $self = shift;
1N/A my ($value) = @_;
1N/A $$self = $value;
1N/A}
1N/A
1N/Apackage FAULT;
1N/A
1N/A$fault = 0;
1N/A
1N/Asub TIESCALAR {
1N/A my $pkg = shift;
1N/A return bless [@_], $pkg;
1N/A}
1N/A
1N/Asub FETCH {
1N/A my $self = shift;
1N/A my ($href, $key) = @$self;
1N/A $fault++;
1N/A untie $href->{$key};
1N/A return $href->{$key} = 1;
1N/A}
1N/A
1N/Apackage main;
1N/A
1N/A$a = 'toto';
1N/A$b = \$a;
1N/A
1N/A$c = tie %hash, TIED_HASH;
1N/A$d = tie @array, TIED_ARRAY;
1N/Atie $scalar, TIED_SCALAR;
1N/A
1N/A#$scalar = 'foo';
1N/A#$hash{'attribute'} = \$d;
1N/A#$array[0] = $c;
1N/A#$array[1] = \$scalar;
1N/A
1N/A### If I say
1N/A### $hash{'attribute'} = $d;
1N/A### below, then dump() incorectly dumps the hash value as a string the second
1N/A### time it is reached. I have not investigated enough to tell whether it's
1N/A### a bug in my dump() routine or in the Perl tieing mechanism.
1N/A$scalar = 'foo';
1N/A$hash{'attribute'} = 'plain value';
1N/A$array[0] = \$scalar;
1N/A$array[1] = $c;
1N/A$array[2] = \@array;
1N/A
1N/A@tied = (\$scalar, \@array, \%hash);
1N/A%a = ('key', 'value', 1, 0, $a, $b, 'cvar', \$a, 'scalarref', \$scalar);
1N/A@a = ('first', 3, -4, -3.14159, 456, 4.5, $d, \$d,
1N/A $b, \$a, $a, $c, \$c, \%a, \@array, \%hash, \@tied);
1N/A
1N/Aok 1, defined($f = freeze(\@a));
1N/A
1N/A$dumped = &dump(\@a);
1N/Aok 2, 1;
1N/A
1N/A$root = thaw($f);
1N/Aok 3, defined $root;
1N/A
1N/A$got = &dump($root);
1N/Aok 4, 1;
1N/A
1N/A### Used to see the manifestation of the bug documented above.
1N/A### print "original: $dumped";
1N/A### print "--------\n";
1N/A### print "got: $got";
1N/A### print "--------\n";
1N/A
1N/Aok 5, $got eq $dumped;
1N/A
1N/A$g = freeze($root);
1N/Aok 6, length($f) == length($g);
1N/A
1N/A# Ensure the tied items in the retrieved image work
1N/A@old = ($scalar_fetch, $array_fetch, $hash_fetch);
1N/A@tied = ($tscalar, $tarray, $thash) = @{$root->[$#{$root}]};
1N/A@type = qw(SCALAR ARRAY HASH);
1N/A
1N/Aok 7, tied $$tscalar;
1N/Aok 8, tied @{$tarray};
1N/Aok 9, tied %{$thash};
1N/A
1N/A@new = ($$tscalar, $tarray->[0], $thash->{'attribute'});
1N/A@new = ($scalar_fetch, $array_fetch, $hash_fetch);
1N/A
1N/A# Tests 10..15
1N/Afor ($i = 0; $i < @new; $i++) {
1N/A print "not " unless $new[$i] == $old[$i] + 1;
1N/A printf "ok %d\n", 10 + 2*$i; # Tests 10,12,14
1N/A print "not " unless ref $tied[$i] eq $type[$i];
1N/A printf "ok %d\n", 11 + 2*$i; # Tests 11,13,15
1N/A}
1N/A
1N/A# Check undef ties
1N/Amy $h = {};
1N/Atie $h->{'x'}, 'FAULT', $h, 'x';
1N/Amy $hf = freeze($h);
1N/Aok 16, defined $hf;
1N/Aok 17, $FAULT::fault == 0;
1N/Aok 18, $h->{'x'} == 1;
1N/Aok 19, $FAULT::fault == 1;
1N/A
1N/Amy $ht = thaw($hf);
1N/Aok 20, defined $ht;
1N/Aok 21, $ht->{'x'} == 1;
1N/Aok 22, $FAULT::fault == 2;
1N/A
1N/A{
1N/A package P;
1N/A use Storable qw(freeze thaw);
1N/A use vars qw($a $b);
1N/A $b = "not ok ";
1N/A sub TIESCALAR { bless \$a } sub FETCH { "ok " }
1N/A tie $a, P; my $r = thaw freeze \$a; $b = $$r;
1N/A print $b , 23, "\n";
1N/A}
1N/A