1#!perl -T 2 3use strict; 4use warnings; 5 6use Test::More; 7 8use Variable::Magic qw/wizard cast dispell VMG_UVAR/; 9 10if (!VMG_UVAR) { 11 plan skip_all => 'No nice uvar magic for this perl'; 12} 13 14eval "use Hash::Util::FieldHash"; 15if ($@) { 16 plan skip_all => 'Hash::Util::FieldHash required for testing uvar interaction'; 17} else { 18 plan tests => 2 * 5 + 7 + 1; 19 my $v = $Hash::Util::FieldHash::VERSION; 20 diag "Using Hash::Util::FieldHash $v" if defined $v; 21} 22 23use lib 't/lib'; 24use Variable::Magic::TestWatcher; 25 26my $wiz = init_watcher [ qw/fetch store/ ], 'huf'; 27ok defined($wiz), 'huf: wizard with uvar is defined'; 28is ref($wiz), 'SCALAR', 'huf: wizard with uvar is a scalar ref'; 29 30Hash::Util::FieldHash::fieldhash(\my %h); 31 32my $obj = { }; 33bless $obj, 'Variable::Magic::Test::Mock'; 34$h{$obj} = 5; 35 36my ($res) = watch { cast %h, $wiz } { }, 'cast uvar magic on fieldhash'; 37ok $res, 'huf: cast uvar magic on fieldhash succeeded'; 38 39my ($s) = watch { $h{$obj} } { fetch => 1 }, 'fetch on magical fieldhash'; 40is $s, 5, 'huf: fetch on magical fieldhash succeeded'; 41 42watch { $h{$obj} = 7 } { store => 1 }, 'store on magical fieldhash'; 43is $h{$obj}, 7, 'huf: store on magical fieldhash succeeded'; 44 45($res) = watch { dispell %h, $wiz } { }, 'dispell uvar magic on fieldhash'; 46ok $res, 'huf: dispell uvar magic on fieldhash succeeded'; 47 48$h{$obj} = 11; 49$s = $h{$obj}; 50is $s, 11, 'huf: store/fetch on fieldhash after dispell still ok'; 51 52$Variable::Magic::TestWatcher::mg_end = { fetch => 1 }; 53