X-Git-Url: http://git.vpit.fr/?a=blobdiff_plain;f=samples%2Fvm_vs_tie.pl;h=b7f888dd907a21de4489d90d1410b99c52a6c1d1;hb=HEAD;hp=cfb491c55da6987e1b9e3b32b79db3bb343d9fc5;hpb=91aec4cfae75e61ff8eeb79448501a8739b0d240;p=perl%2Fmodules%2FVariable-Magic.git diff --git a/samples/vm_vs_tie.pl b/samples/vm_vs_tie.pl index cfb491c..b7f888d 100755 --- a/samples/vm_vs_tie.pl +++ b/samples/vm_vs_tie.pl @@ -5,26 +5,46 @@ use warnings; use Tie::Hash; -use lib qw{blib/arch blib/lib}; -use Variable::Magic qw/wizard cast VMG_UVAR/; +use lib qw; +use Variable::Magic qw; -use Benchmark qw/cmpthese/; +use Benchmark qw; die 'Your perl does not support the nice uvar magic of 5.10.*' unless VMG_UVAR; -my @a = ('a' .. 'z'); - tie my %t, 'Tie::StdHash'; -$t{$a[$_]} = $_ for 0 .. $#a; +$t{a} = 1; -my $wiz = wizard fetch => sub { 0 }, store => sub { 0 }; +my $wiz = wizard fetch => sub { 0 }, + store => sub { 0 }, + exists => sub { 0 }, + delete => sub { 0 }; my %v; -$v{$a[$_]} = $_ for 0 .. $#a; cast %v, $wiz; +$v{a} = 2; + +print "Using Variable::Magic ", $Variable::Magic::VERSION, "\n"; + +print "Fetch:\n"; +cmpthese -1, { + 'tie' => sub { $t{a} }, + 'v::m' => sub { $v{a} } +}; -my $x = 0; +print "Store:\n"; +cmpthese -1, { + 'tie' => sub { $t{a} = 2 }, + 'v::m' => sub { $v{a} = 2 } +}; + +print "Exists:\n"; +cmpthese -1, { + 'tie' => sub { exists $t{a} }, + 'v::m' => sub { exists $v{a} } +}; -cmpthese -3, { - 'tie' => sub { my ($x, $y) = map @a[$x++ % @a], 1 .. 2; my $a = $t{$x}; $t{$y} = $a }, - 'v::m' => sub { my ($x, $y) = map @a[$x++ % @a], 1 .. 2; my $a = $v{$x}; $v{$y} = $a } +print "Delete/store:\n"; +cmpthese -1, { + 'tie' => sub { delete $t{a}; $t{a} = 3 }, + 'v::m' => sub { delete $v{a}; $v{a} = 3 } };