3 BEGIN { ## no critic strict
4 if ( $ENV{PERL_CORE} ) {
5 unshift @INC, '../../t/lib';
10 if ( ( $Config::Config{'extensions'} !~ /\bB\b/ ) ) {
11 print "1..0 # Skip -- Perl configured without B module\n";
18 use Test::More tests => 4 * 3;
19 use B 'svref_2object';
21 # use Data::Dumper 'Dumper';
29 # hh => { mypragma => 42 }
33 # hh => { mypragma => 0 }
40 # Pragmas don't appear til they're used.
41 my $cop = find_op_cop( \&foo, qr/multiply/ );
42 isa_ok( $cop, 'B::COP', 'found pp_multiply opnode' );
44 my $rhe = $cop->hints_hash;
45 isa_ok( $rhe, 'B::RHE', 'got hints_hash' );
47 my $hints_hash = $rhe->HASH;
48 is( ref($hints_hash), 'HASH', 'Got hash reference' );
50 ok( not( exists $hints_hash->{mypragma} ), q[! exists mypragma] );
55 # Pragmas can be fetched.
56 my $cop = find_op_cop( \&foo, qr/add/ );
57 isa_ok( $cop, 'B::COP', 'found pp_add opnode' );
59 my $rhe = $cop->hints_hash;
60 isa_ok( $rhe, 'B::RHE', 'got hints_hash' );
62 my $hints_hash = $rhe->HASH;
63 is( ref($hints_hash), 'HASH', 'Got hash reference' );
65 is( $hints_hash->{mypragma}, 42, q[mypragma => 42] );
70 # Pragmas can be changed.
71 my $cop = find_op_cop( \&foo, qr/subtract/ );
72 isa_ok( $cop, 'B::COP', 'found pp_subtract opnode' );
74 my $rhe = $cop->hints_hash;
75 isa_ok( $rhe, 'B::RHE', 'got hints_hash' );
77 my $hints_hash = $rhe->HASH;
78 is( ref($hints_hash), 'HASH', 'Got hash reference' );
80 is( $hints_hash->{mypragma}, 0, q[mypragma => 0] );
87 my ( $sub, $op ) = @_;
88 my $cv = svref_2object($sub);
91 if ( not _find_op_cop( $cv->ROOT, $op ) ) {
100 # Make B::NULL objects evaluate as false.
102 use overload 'bool' => sub () { !!0 };
106 my ( $op, $name ) = @_;
108 # Fail on B::NULL or whatever.
111 # Succeed when we find our match.
112 return 1 if $op->name =~ $name;
114 # Stash the latest seen COP opnode. This has our hints hash.
115 if ( $op->isa('B::COP') ) {
119 # hints => $op->hints_hash->HASH
125 # Recurse depth first passing success up if it happens.
126 if ( $op->can('first') ) {
127 return 1 if _find_op_cop( $op->first, $name );
129 return 1 if _find_op_cop( $op->sibling, $name );
131 # Oh well. Hopefully our caller knows where to try next.