| 1 | package ctflags; | ||||||||||||||||||
| 2 | |||||||||||||||||||
| 3 | 1 | our $VERSION = '0.04'; | |||||||||||||||||
| 4 | |||||||||||||||||||
| 5 | 1 | ▶ | use 5.006; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 6 | |||||||||||||||||||
| 7 | 1 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 8 | 1 | ▶ | use warnings; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 9 | |||||||||||||||||||
| 10 | 1 | ▶ | use Carp; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 11 | |||||||||||||||||||
| 12 | 1 | ▶ | use ctflags::memory; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 13 | 1 | ▶ | use ctflags::check; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 14 | |||||||||||||||||||
| 15 | 1 | ▶ | use constant PREFIX => "ctflag_"; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 16 | |||||||||||||||||||
| 17 | # set and get functions are wrappers around | ||||||||||||||||||
| 18 | # ctflags::memory::(set|get)_ctflag, wrappers capture errors an report | ||||||||||||||||||
| 19 | # them via croak | ||||||||||||||||||
| 20 | |||||||||||||||||||
| 21 | 4 | ▶ | sub set ($$$ ) { | ||||||||||||||||
|
Subroutine
set: called CC=2 SCAR=8.4
Pod
documented
| |||||||||||||||||||
| 22 | 4 | eval { &set_ctflag }; | |||||||||||||||||
| 23 | 0 | ▶ | if ($@) { chomp $@; croak $@ }; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 24 | } | ||||||||||||||||||
| 25 | |||||||||||||||||||
| 26 | 7 | ▶ | sub get ($$ ) { | ||||||||||||||||
|
Subroutine
get: called CC=2 SCAR=7.9
Pod
documented
| |||||||||||||||||||
| 27 | 7 | my $r=eval { &get_ctflag }; | |||||||||||||||||
| 28 | 0 | ▶ | if ($@) { chomp $@; croak $@ }; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 29 | 7 | return $r; | |||||||||||||||||
| 30 | } | ||||||||||||||||||
| 31 | |||||||||||||||||||
| 32 | |||||||||||||||||||
| 33 | # parse_flags breaks a string defining a flag set maybe with default | ||||||||||||||||||
| 34 | # values. If strings is '*' it expand it to all the flags allowed in | ||||||||||||||||||
| 35 | # the namespace (allowed ctflags are not the flags defined but the | ||||||||||||||||||
| 36 | # ones configured with ctflags::memory::restrict_ctflags() subrutine). | ||||||||||||||||||
| 37 | |||||||||||||||||||
| 38 | 4 | ▶ | sub parse_flags ($$) { | ||||||||||||||||
|
Subroutine
parse_flags: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||
| 39 | 4 | my ($ns, $flags)=@_; | |||||||||||||||||
| 40 | 4 | ▶ | return allowed_ctflags($ns) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 41 | if $flags eq '*'; | ||||||||||||||||||
| 42 | |||||||||||||||||||
| 43 | 4 | return ($flags=~/\G$flag_re$value_re?/go) | |||||||||||||||||
| 44 | } | ||||||||||||||||||
| 45 | |||||||||||||||||||
| 46 | |||||||||||||||||||
| 47 | # export_sub creates the constant subrutine in the given package | ||||||||||||||||||
| 48 | |||||||||||||||||||
| 49 | 8 | ▶ | sub export_sub ($$$ ) { | ||||||||||||||||
|
Subroutine
export_sub: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||
| 50 | 8 | my $qname=$_[0].'::'.$_[1]; | |||||||||||||||||
| 51 | 8 | my $value=$_[2]; | |||||||||||||||||
| 52 | |||||||||||||||||||
| 53 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 54 | 0 | ▶ | *$qname = sub () { $value }; | ||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||
| 55 | } | ||||||||||||||||||
| 56 | |||||||||||||||||||
| 57 | 0 | ▶ | sub export_subsub ($$$ ) { | ||||||||||||||||
|
Subroutine
export_subsub: not called CC=1 SCAR=1.2
Pod
undocumented
| |||||||||||||||||||
| 58 | 0 | my $qname=$_[0].'::'.$_[1]; | |||||||||||||||||
| 59 | 0 | my $sub=$_[2]; | |||||||||||||||||
| 60 | |||||||||||||||||||
| 61 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 62 | 0 | *$qname = $sub | |||||||||||||||||
| 63 | } | ||||||||||||||||||
| 64 | |||||||||||||||||||
| 65 | # export_ctflags_as combine ctflag set with arithmetic or and export | ||||||||||||||||||
| 66 | # constant with the resulting value | ||||||||||||||||||
| 67 | |||||||||||||||||||
| 68 | 2 | ▶ | sub export_ctflags_as ($$$$) { | ||||||||||||||||
|
Subroutine
export_ctflags_as: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||
| 69 | 2 | my ($package, $ns, $flags, $name)=@_; | |||||||||||||||||
| 70 | 2 | my $acu=0; | |||||||||||||||||
| 71 | 2 | $acu|=get_ctflag($ns, $_) foreach (parse_flags $ns, $flags); | |||||||||||||||||
| 72 | 2 | export_sub $package, $name, $acu; | |||||||||||||||||
| 73 | } | ||||||||||||||||||
| 74 | |||||||||||||||||||
| 75 | |||||||||||||||||||
| 76 | # export every flag specified in $flags as package::prefix_flag | ||||||||||||||||||
| 77 | |||||||||||||||||||
| 78 | 2 | ▶ | sub export_ctflags ($$$$) { | ||||||||||||||||
|
Subroutine
export_ctflags: called CC=3 SCAR=12.0
Pod
undocumented
| |||||||||||||||||||
| 79 | 2 | my ($package, $ns, $flags, $prefix)=@_; | |||||||||||||||||
| 80 | 2 | foreach my $fe (parse_flags $ns, $flags) { | |||||||||||||||||
| 81 | 6 | my ($f)=split '', $fe; | |||||||||||||||||
| 82 | 6 | my $v=get_ctflag($ns, $fe); | |||||||||||||||||
| 83 | 6 | my $sub=get_ctflag_call($ns, $f); | |||||||||||||||||
| 84 | 6 | ▶ | if ($sub) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 85 | export_subsub $package, $prefix, | ||||||||||||||||||
| 86 | 0 | ▶ | sub () {&$sub($ns, $f, $v); $v} | ||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||
| 87 | 0 | } | |||||||||||||||||
| 88 | else { | ||||||||||||||||||
| 89 | 6 | export_sub $package, $prefix.$f, $v; | |||||||||||||||||
| 90 | } | ||||||||||||||||||
| 91 | } | ||||||||||||||||||
| 92 | } | ||||||||||||||||||
| 93 | |||||||||||||||||||
| 94 | |||||||||||||||||||
| 95 | # see pod docs below for import description. | ||||||||||||||||||
| 96 | |||||||||||||||||||
| 97 | 4 | ▶ | sub import { | ||||||||||||||||
|
Subroutine
import: called CC=7 SCAR=21.2
| |||||||||||||||||||
| 98 | 4 | my $self=shift; | |||||||||||||||||
| 99 | 4 | my $prefix=PREFIX; # prefix to use until another one is defined. | |||||||||||||||||
| 100 | 4 | my ($package)=caller; # by default constants are exported to calling | |||||||||||||||||
| 101 | # package | ||||||||||||||||||
| 102 | 4 | eval { | |||||||||||||||||
| 103 | 4 | ▶ | while (@_) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 104 | 5 | my $key=shift; | |||||||||||||||||
| 105 | 5 | ▶ | if (my ($name, $ns, $flags)= | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 106 | $key=~m{^ # all the string should match. | ||||||||||||||||||
| 107 | (?:($identifier_re)=)? # name for the constant, optional. | ||||||||||||||||||
| 108 | ($ns_re) # namespace. | ||||||||||||||||||
| 109 | : # namespace/flags separator ':' | ||||||||||||||||||
| 110 | ( | ||||||||||||||||||
| 111 | \* # asterisk | ||||||||||||||||||
| 112 | | # or | ||||||||||||||||||
| 113 | (?:$flag_re # flag name | ||||||||||||||||||
| 114 | $value_re?) # maybe with default value, | ||||||||||||||||||
| 115 | * # several allowed | ||||||||||||||||||
| 116 | )$ | ||||||||||||||||||
| 117 | }xo ) { | ||||||||||||||||||
| 118 | # option is a ctflags -> constants conversion specification. | ||||||||||||||||||
| 119 | 4 | ▶ | if ($name) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 120 | 2 | export_ctflags_as($package, $ns, $flags, $name) | |||||||||||||||||
| 121 | } | ||||||||||||||||||
| 122 | else { | ||||||||||||||||||
| 123 | 2 | export_ctflags($package, $ns, $flags, $prefix) | |||||||||||||||||
| 124 | } | ||||||||||||||||||
| 125 | } | ||||||||||||||||||
| 126 | elsif ($key eq 'prefix') { | ||||||||||||||||||
| 127 | 1 | $prefix=shift; | |||||||||||||||||
| 128 | 1 | check_cntprefix $prefix; | |||||||||||||||||
| 129 | } | ||||||||||||||||||
| 130 | elsif ($key eq 'package') { | ||||||||||||||||||
| 131 | 0 | $package=shift; | |||||||||||||||||
| 132 | 0 | check_package $package; | |||||||||||||||||
| 133 | } | ||||||||||||||||||
| 134 | else { | ||||||||||||||||||
| 135 | 0 | die "unknow option or invalid ctflags specification '$key'\n"; | |||||||||||||||||
| 136 | } | ||||||||||||||||||
| 137 | } | ||||||||||||||||||
| 138 | }; | ||||||||||||||||||
| 139 | 0 | ▶ | if ($@) { chomp $@; croak $@ }; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 140 | } | ||||||||||||||||||
| 141 | |||||||||||||||||||
| 142 | |||||||||||||||||||
| 143 | 1 | 1; | |||||||||||||||||
| 144 | __END__ | ||||||||||||||||||