| 1 | package Devel::Pragma; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 7 | ▶ | use 5.008001; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | # make sure this is loaded first | ||||||||||||||||||||
| 6 | 7 | ▶ | use Lexical::SealRequireHints; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 7 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 7 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 7 | ▶ | use Carp qw(carp croak); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 7 | ▶ | use Scalar::Util; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 7 | ▶ | use XSLoader; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 7 | ▶ | use base qw(Exporter); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 7 | our $VERSION = '1.1.0'; | |||||||||||||||||||
| 18 | 7 | our @EXPORT_OK = qw(my_hints hints new_scope ccstash scope fqname); | |||||||||||||||||||
| 19 | 7 | our %EXPORT_TAGS = (all => [ @EXPORT_OK ]); | |||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | 7 | XSLoader::load(__PACKAGE__, $VERSION); | |||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | # return a reference to the hints hash | ||||||||||||||||||||
| 24 | 55 | ▶ | sub my_hints() { | ||||||||||||||||||
|
Subroutine
my_hints: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 25 | # set HINT_LOCALIZE_HH (0x20000) | ||||||||||||||||||||
| 26 | 55 | $^H |= 0x20000; | |||||||||||||||||||
| 27 | 55 | return \%^H; | |||||||||||||||||||
| 28 | } | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | 7 | ▶ | BEGIN { *hints = \&my_hints } | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | # make sure the "enable lexically-scoped %^H" flag is set (on by default in 5.10) | ||||||||||||||||||||
| 33 | 45 | ▶ | sub check_hints() { | ||||||||||||||||||
|
Subroutine
check_hints: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 34 | 45 | ▶ | unless ($^H & 0x20000) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 35 | 1 | carp('Devel::Pragma: unexpected $^H (HINT_LOCALIZE_HH bit not set) - setting it now, but results may be unreliable'); | |||||||||||||||||||
| 36 | } | ||||||||||||||||||||
| 37 | 45 | return hints; # create it if it doesn't exist - in some perls, it starts out NULL | |||||||||||||||||||
| 38 | } | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | # return a unique integer ID for the current scope | ||||||||||||||||||||
| 41 | 29 | ▶ | sub scope() { | ||||||||||||||||||
|
Subroutine
scope: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 42 | 29 | check_hints; | |||||||||||||||||||
| 43 | 29 | xs_scope(); | |||||||||||||||||||
| 44 | } | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | # return a boolean indicating whether this is the first time "use MyPragma" has been called in this scope | ||||||||||||||||||||
| 47 | 16 | ▶ | sub new_scope(;$) { | ||||||||||||||||||
|
Subroutine
new_scope: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||
| 48 | 16 | ▶ | my $caller = shift || caller; | ||||||||||||||||||
|
Condition66%2/3
shift() || caller
MC/DC0%0/2
shift() || caller
shift()
caller
Truth table66%2/3
A || B
A= shift()B= caller
| |||||||||||||||||||||
| 49 | 16 | my $hints = check_hints(); | |||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | # this is %^H as an integer - it changes as scopes are entered/exited i.e. it's a unique | ||||||||||||||||||||
| 52 | # identifier for the currently-compiling scope (the scope in which new_scope | ||||||||||||||||||||
| 53 | # is called) | ||||||||||||||||||||
| 54 | # | ||||||||||||||||||||
| 55 | # we don't need to stack/unstack it in %^H as %^H itself takes care of that | ||||||||||||||||||||
| 56 | # note: we need to call this *after* %^H is referenced (and possibly autovivified) above | ||||||||||||||||||||
| 57 | # | ||||||||||||||||||||
| 58 | # every time new_scope is called, we write this scope ID to $^H{"Devel::Pragma::new_scope::$caller"}. | ||||||||||||||||||||
| 59 | # if $^H{"Devel::Pragma::new_scope::$caller"} == scope() (i.e. the stored scope ID is the same as the | ||||||||||||||||||||
| 60 | # current scope ID), then we're augmenting the current scope; otherwise we're in a new scope - i.e. | ||||||||||||||||||||
| 61 | # a nested or outer scope that didn't previously "use MyPragma" | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | 16 | my $current_scope = scope(); | |||||||||||||||||||
| 64 | 16 | my $id = "Devel::Pragma::new_scope::$caller"; | |||||||||||||||||||
| 65 | 16 | ▶ | my $old_scope = exists($hints->{$id}) ? $hints->{$id} : 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 66 | 16 | my $new_scope; # is this a scope in which new_scope has not previously been called? | |||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | 16 | ▶ | if ($current_scope == $old_scope) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 69 | 9 | $new_scope = 0; | |||||||||||||||||||
| 70 | } else { | ||||||||||||||||||||
| 71 | 7 | $hints->{$id} = $current_scope; | |||||||||||||||||||
| 72 | 7 | $new_scope = 1; | |||||||||||||||||||
| 73 | } | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | 16 | return $new_scope; | |||||||||||||||||||
| 76 | } | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | # given a short name (e.g. "foo"), expand it into a fully-qualified name with the caller's package prefixed | ||||||||||||||||||||
| 79 | # e.g. "main::foo" | ||||||||||||||||||||
| 80 | # | ||||||||||||||||||||
| 81 | # if the name is already fully-qualified, return it unchanged | ||||||||||||||||||||
| 82 | 5 | ▶ | sub fqname ($;$) { | ||||||||||||||||||
|
Subroutine
fqname: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||
| 83 | 5 | my $name = shift; | |||||||||||||||||||
| 84 | 5 | my ($package, $subname); | |||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | 5 | $name =~ s{'}{::}g; | |||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | 5 | ▶ | if ($name =~ /::/) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 89 | 3 | ($package, $subname) = $name =~ m{^(.+)::(\w+)$}; | |||||||||||||||||||
| 90 | } else { | ||||||||||||||||||||
| 91 | 2 | ▶ | my $caller = @_ ? shift : ccstash(); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 92 | 2 | ($package, $subname) = ($caller, $name); | |||||||||||||||||||
| 93 | } | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | 5 | ▶ | return wantarray ? ($package, $subname) : "$package\::$subname"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 96 | } | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | # helper function: return true if $ref ISA $class - works with non-references, unblessed references and objects | ||||||||||||||||||||
| 99 | 0 | ▶ | sub _isa($$) { | ||||||||||||||||||
|
Subroutine
_isa: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 100 | 0 | my ($ref, $class) = @_; | |||||||||||||||||||
| 101 | 0 | ▶ | return Scalar::Util::blessed($ref) ? $ref->isa($class) : ref($ref) eq $class; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 102 | } | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | # make sure "enable lexically-scoped %^H" is set in older perls, and export the requested functions | ||||||||||||||||||||
| 105 | 9 | ▶ | sub import { | ||||||||||||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 106 | 9 | my $class = shift; | |||||||||||||||||||
| 107 | 9 | $^H |= 0x20000; # set HINT_LOCALIZE_HH (0x20000) | |||||||||||||||||||
| 108 | 9 | $class->export_to_level(1, undef, @_); | |||||||||||||||||||
| 109 | } | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | 7 | 1; | |||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | __END__ | ||||||||||||||||||||