| 1 | package pluskeys; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 2 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 2 | our($VERSION) = q($Revision: 2.4 $) =~ / \b ( \d+ (?: \. \d+ )+ ) \b /x; | |||||||||
| 9 | |||||||||||
| 10 | 2 | my $My_Package = __PACKAGE__; | |||||||||
| 11 | |||||||||||
| 12 | 4 | ▶ | sub import { | ||||||||
|
Subroutine
import: called CC=4 SCAR=13.9
| |||||||||||
| 13 | 4 | shift; # discard "pluskeys" invocant | |||||||||
| 14 | |||||||||||
| 15 | ################################################################# | ||||||||||
| 16 | # Create named constants in caller's package so they | ||||||||||
| 17 | # can be used to access munged member names with +NAME | ||||||||||
| 18 | # which both catches typos and adds package-name munging so | ||||||||||
| 19 | # that more than one class in a hierarchy can have a key | ||||||||||
| 20 | # with the same name. | ||||||||||
| 21 | # | ||||||||||
| 22 | # Must be called from a BEGIN block to do any good. That's | ||||||||||
| 23 | # why it is a use statement, since that has a BEGIN blocked | ||||||||||
| 24 | # wrapped around it. | ||||||||||
| 25 | ################################################################# | ||||||||||
| 26 | |||||||||||
| 27 | 4 | for my $member (@_) { | |||||||||
| 28 | # Has to start with a letter or an underscore, and be one | ||||||||||
| 29 | # or more identifier characters long and nothing else. | ||||||||||
| 30 | 5 | ▶ | $member =~ / ^ (?= [\p{Letter}_] ) \w+ $ /x | ||||||||
|
Branch: 1 branch
| |||||||||||
| 31 | || croak "Bad pluskey: '$member' is not a valid identifier"; | ||||||||||
| 32 | |||||||||||
| 33 | 5 | my $his_package = caller; | |||||||||
| 34 | |||||||||||
| 35 | 5 | ▶ | eval <<"MAKE_A_MEMBER_CONSTANT" || croak "member init of $member failed: $@"; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||
| 36 | package $his_package; | ||||||||||
| 37 | use constant $member => ${My_Package}::_mk_safe_member('$member'); | ||||||||||
| 38 | 1; | ||||||||||
| 39 | MAKE_A_MEMBER_CONSTANT | ||||||||||
| 40 | } | ||||||||||
| 41 | } | ||||||||||
| 42 | |||||||||||
| 43 | 5 | ▶ | sub _mk_safe_member($) { | ||||||||
|
Subroutine
_mk_safe_member: called CC=1 SCAR=0.0
| |||||||||||
| 44 | 5 | my($member_name) = @_; | |||||||||
| 45 | 5 | my $his_package = caller; | |||||||||
| 46 | 5 | return $his_package . "::" . $member_name; | |||||||||
| 47 | } | ||||||||||
| 48 | |||||||||||
| 49 | 2 | 1; | |||||||||
| 50 | |||||||||||
| 51 | __END__ | ||||||||||