Variable-Magic

 view release on metacpan or  search on metacpan

t/17-ctl.t  view on Meta::CPAN

  cast $x, $wiz;
 }
 die 'not reached again';
};

like $@, expect('eggplant', $0),
                            'die in free callback in block in eval with $@ set';

eval q{BEGIN {
 $wiz = wizard free => sub { die 'onion' };
 my $x;
 cast $x, $wiz;
}};

like $@, expect('onion', undef, "\nBEGIN.*"), 'die in free callback in BEGIN';

eval q{BEGIN {
 $wiz = wizard data => sub { $_[1] },
               len  => sub { $_[1]->(); $_[2] },
               free => sub { my $x = @{$_[0]}; () };
 my @a = (1 .. 5);
 cast @a, $wiz, sub { die "pepperoni" };
}};

like $@, expect('pepperoni', undef, "\nBEGIN.*"),
                                'die in free callback in len callback in BEGIN';

# Inspired by B::Hooks::EndOfScope

eval q{BEGIN {
 $wiz = wizard data => sub { $_[1] },
               free => sub { $_[1]->(); () };
 $^H |= 0x020000;
 cast %^H, $wiz, sub { die 'cabbage' };
}};

like $@, expect('cabbage'), 'die in free callback at end of scope';

use lib 't/lib';

my $vm_tse_file = 't/lib/Variable/Magic/TestScopeEnd.pm';

eval "use Variable::Magic::TestScopeEnd";
like $@, expect('turnip', $vm_tse_file, "\nBEGIN(?s:.*)"),
        'die in BEGIN in require in eval string triggers hints hash destructor';

eval q{BEGIN {
 Variable::Magic::TestScopeEnd::hook {
  pass 'in hints hash destructor 2';
 };
 die "tomato";
}};

like $@, expect('tomato', undef, "\nBEGIN.*"),
                          'die in BEGIN in eval triggers hints hash destructor';

SKIP: {
 my $count = 1;

 my ($stat, $out, $err) = capture_perl <<' CODE';
use Variable::Magic qw<wizard cast>; { BEGIN { $^H |= 0x020000; cast %^H, wizard free => sub { die q[cucumber] } } }
 CODE
 skip CAPTURE_PERL_FAILED($out) => $count unless defined $stat;
 like $err, expect('cucumber', '-e', "\nExecution(?s:.*)"),
            'die in free callback at compile time and not in eval string';
 --$count;
}

# Uvar

SKIP:
{
 my $count = 1;

 skip 'No nice uvar magic for this perl' => $count unless VMG_UVAR;

 my ($stat, $out, $err) = capture_perl <<' CODE';
use Variable::Magic qw<wizard cast>; BEGIN { cast %derp::, wizard fetch => sub { die q[raddish] } } derp::hlagh()
 CODE
 skip CAPTURE_PERL_FAILED($out) => $count unless defined $stat;
 like $err, expect('raddish', '-e', "\nExecution(?s:.*)"),
            'die in free callback at compile time and not in eval string';
 --$count;
}



( run in 0.855 second using v1.01-cache-2.11-cpan-92ad3014f07 )