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";
15 print "1..0 # Skip -- No user pragmata in 5.8.x\n";
22 use Test::More tests => 4 * 3;
23 use B 'svref_2object';
25 # use Data::Dumper 'Dumper';
33 # hh => { mypragma => 42 }
37 # hh => { mypragma => 0 }
44 # Pragmas don't appear til they're used.
45 my $cop = find_op_cop( \&foo, qr/multiply/ );
46 isa_ok( $cop, 'B::COP', 'found pp_multiply opnode' );
48 my $rhe = $cop->hints_hash;
49 isa_ok( $rhe, 'B::RHE', 'got hints_hash' );
51 my $hints_hash = $rhe->HASH;
52 is( ref($hints_hash), 'HASH', 'Got hash reference' );
54 ok( not( exists $hints_hash->{mypragma} ), q[! exists mypragma] );
59 # Pragmas can be fetched.
60 my $cop = find_op_cop( \&foo, qr/add/ );
61 isa_ok( $cop, 'B::COP', 'found pp_add opnode' );
63 my $rhe = $cop->hints_hash;
64 isa_ok( $rhe, 'B::RHE', 'got hints_hash' );
66 my $hints_hash = $rhe->HASH;
67 is( ref($hints_hash), 'HASH', 'Got hash reference' );
69 is( $hints_hash->{mypragma}, 42, q[mypragma => 42] );
74 # Pragmas can be changed.
75 my $cop = find_op_cop( \&foo, qr/subtract/ );
76 isa_ok( $cop, 'B::COP', 'found pp_subtract opnode' );
78 my $rhe = $cop->hints_hash;
79 isa_ok( $rhe, 'B::RHE', 'got hints_hash' );
81 my $hints_hash = $rhe->HASH;
82 is( ref($hints_hash), 'HASH', 'Got hash reference' );
84 is( $hints_hash->{mypragma}, 0, q[mypragma => 0] );
91 my ( $sub, $op ) = @_;
92 my $cv = svref_2object($sub);
95 if ( not _find_op_cop( $cv->ROOT, $op ) ) {
104 # Make B::NULL objects evaluate as false.
106 use overload 'bool' => sub () { !!0 };
110 my ( $op, $name ) = @_;
112 # Fail on B::NULL or whatever.
115 # Succeed when we find our match.
116 return 1 if $op->name =~ $name;
118 # Stash the latest seen COP opnode. This has our hints hash.
119 if ( $op->isa('B::COP') ) {
123 # hints => $op->hints_hash->HASH
129 # Recurse depth first passing success up if it happens.
130 if ( $op->can('first') ) {
131 return 1 if _find_op_cop( $op->first, $name );
133 return 1 if _find_op_cop( $op->sibling, $name );
135 # Oh well. Hopefully our caller knows where to try next.