3 BEGIN { ## no critic strict
4 if ( $ENV{PERL_CORE} ) {
6 @INC = qw(../lib . lib);
12 if ( ( $Config::Config{'extensions'} !~ /\bB\b/ ) ) {
13 print "1..0 # Skip -- Perl configured without B module\n";
20 use Test::More tests => 4 * 3;
21 use B 'svref_2object';
23 # use Data::Dumper 'Dumper';
31 # hh => { mypragma => 42 }
35 # hh => { mypragma => 0 }
42 # Pragmas don't appear til they're used.
43 my $cop = find_op_cop( \&foo, qr/multiply/ );
44 isa_ok( $cop, 'B::COP', 'found pp_multiply opnode' );
46 my $rhe = $cop->hints_hash;
47 isa_ok( $rhe, 'B::RHE', 'got hints_hash' );
49 my $hints_hash = $rhe->HASH;
50 is( ref($hints_hash), 'HASH', 'Got hash reference' );
52 ok( not( exists $hints_hash->{mypragma} ), q[! exists mypragma] );
57 # Pragmas can be fetched.
58 my $cop = find_op_cop( \&foo, qr/add/ );
59 isa_ok( $cop, 'B::COP', 'found pp_add opnode' );
61 my $rhe = $cop->hints_hash;
62 isa_ok( $rhe, 'B::RHE', 'got hints_hash' );
64 my $hints_hash = $rhe->HASH;
65 is( ref($hints_hash), 'HASH', 'Got hash reference' );
67 is( $hints_hash->{mypragma}, 42, q[mypragma => 42] );
72 # Pragmas can be changed.
73 my $cop = find_op_cop( \&foo, qr/subtract/ );
74 isa_ok( $cop, 'B::COP', 'found pp_subtract opnode' );
76 my $rhe = $cop->hints_hash;
77 isa_ok( $rhe, 'B::RHE', 'got hints_hash' );
79 my $hints_hash = $rhe->HASH;
80 is( ref($hints_hash), 'HASH', 'Got hash reference' );
82 is( $hints_hash->{mypragma}, 0, q[mypragma => 0] );
89 my ( $sub, $op ) = @_;
90 my $cv = svref_2object($sub);
93 if ( not _find_op_cop( $cv->ROOT, $op ) ) {
102 # Make B::NULL objects evaluate as false.
104 use overload 'bool' => sub () { !!0 };
108 my ( $op, $name ) = @_;
110 # Fail on B::NULL or whatever.
113 # Succeed when we find our match.
114 return 1 if $op->name =~ $name;
116 # Stash the latest seen COP opnode. This has our hints hash.
117 if ( $op->isa('B::COP') ) {
121 # hints => $op->hints_hash->HASH
127 # Recurse depth first passing success up if it happens.
128 if ( $op->can('first') ) {
129 return 1 if _find_op_cop( $op->first, $name );
131 return 1 if _find_op_cop( $op->sibling, $name );
133 # Oh well. Hopefully our caller knows where to try next.