| 1 | package Perlbal::Fields; | ||||||||||
| 2 | 23 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 23 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 23 | ▶ | use fields; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | # allow package to be called in command line | ||||||||||
| 7 | 23 | ▶ | __PACKAGE__->run(@ARGV) unless caller(); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 8 | |||||||||||
| 9 | # should be the main method called, extra sub could be triggered from this point | ||||||||||
| 10 | 1 | ▶ | sub run { | ||||||||
|
Subroutine
run: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 11 | 1 | my ( $package, @options ) = @_; | |||||||||
| 12 | |||||||||||
| 13 | # unactivate fields if launch in command line | ||||||||||
| 14 | 1 | $package->remove(); | |||||||||
| 15 | |||||||||||
| 16 | 1 | 1; | |||||||||
| 17 | } | ||||||||||
| 18 | |||||||||||
| 19 | # hash with keys and undef val for each class | ||||||||||
| 20 | 23 | my $cache_for_class = {}; | |||||||||
| 21 | |||||||||||
| 22 | # replace fields::new method which uses Hash::Util::lock_ref_keys | ||||||||||
| 23 | # - it's a good idea to keep using the original fields::new during development stage | ||||||||||
| 24 | # - but during production we can avoid locking hash and wasting time doing this ( ~ 30 % ) | ||||||||||
| 25 | 1 | ▶ | sub remove { | ||||||||
|
Subroutine
remove: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 26 | 1 | my ($class) = @_; | |||||||||
| 27 | |||||||||||
| 28 | 23 | ▶ | no warnings "redefine"; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 29 | 23 | ▶ | no strict 'refs'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 30 | 2 | ▶ | *fields::new = sub { | ||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||
| 31 | 2 | my $class = shift; | |||||||||
| 32 | 2 | ▶ | $class = ref $class if ref $class; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 33 | |||||||||||
| 34 | 2 | ▶ | if ( !defined( $cache_for_class->{$class} ) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | 2 | my $h = {}; | |||||||||
| 36 | 2 | my @keys = keys %{ $class . "::FIELDS" }; | |||||||||
| 37 | 2 | map { $h->{$_} = undef; } @keys; | |||||||||
| 38 | 2 | $cache_for_class->{$class} = $h; | |||||||||
| 39 | } | ||||||||||
| 40 | 2 | my %h = %{ $cache_for_class->{$class} }; | |||||||||
| 41 | |||||||||||
| 42 | 2 | return bless \%h, $class; | |||||||||
| 43 | 1 | }; | |||||||||
| 44 | } | ||||||||||
| 45 | |||||||||||
| 46 | 23 | 1; | |||||||||
| 47 | |||||||||||