| 1 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | package Package::Generator; | ||||||||||||||||||||||||||||
| 4 | { | ||||||||||||||||||||||||||||
| 5 | 4 | $Package::Generator::VERSION = '1.106'; | |||||||||||||||||||||||||||
| 6 | } | ||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use 5.008; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | # ABSTRACT: generate new packages quickly and easily | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use Carp (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 4 | ▶ | use Scalar::Util (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 4 | my $i = 0; | |||||||||||||||||||||||||||
| 15 | 10 | ▶ | my $unique_part = sub { $i++ }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 19 | ▶ | my $make_unique = sub { sprintf "%s::%u", $_[0], $_[1]->() }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 22 | ▶ | sub new_package { | ||||||||||||||||||||||||||
|
Subroutine
new_package: called CC=12 SCAR=24.9
Pod
documented
| |||||||||||||||||||||||||||||
| 19 | 22 | my ($self, $arg) = @_; | |||||||||||||||||||||||||||
| 20 | 22 | ▶ | $arg->{base} ||= 'Package::Generator::__GENERATED__'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$arg->{"base"} ||= "Package::Generator::__GENERATED__"
MC/DC100%1/1
$arg->{"base"} ||= "Package::Generator::__GENERATED__"
$arg->{"base"}
Truth table100%2/2
A
A= $arg->{"base"}
| |||||||||||||||||||||||||||||
| 21 | 22 | ▶ | $arg->{unique_part} ||= $unique_part; | ||||||||||||||||||||||||||
|
Condition66%2/3
$arg->{"unique_part"} ||= $unique_part
MC/DC0%0/2
$arg->{"unique_part"} ||= $unique_part
$arg->{"unique_part"}
$unique_part
Truth table66%2/3
A ||= B
A= $arg->{"unique_part"}B= $unique_part
| |||||||||||||||||||||||||||||
| 22 | 22 | ▶ | $arg->{make_unique} ||= $make_unique; | ||||||||||||||||||||||||||
|
Condition66%2/3
$arg->{"make_unique"} ||= $make_unique
MC/DC0%0/2
$arg->{"make_unique"} ||= $make_unique
$arg->{"make_unique"}
$make_unique
Truth table66%2/3
A ||= B
A= $arg->{"make_unique"}B= $make_unique
| |||||||||||||||||||||||||||||
| 23 | 22 | ▶ | $arg->{max_tries} ||= 1; | ||||||||||||||||||||||||||
|
Condition100%2/2
$arg->{"max_tries"} ||= 1
MC/DC100%1/1
$arg->{"max_tries"} ||= 1
$arg->{"max_tries"}
Truth table100%2/2
A
A= $arg->{"max_tries"}
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 22 | my $package; | |||||||||||||||||||||||||||
| 26 | 22 | for (my $i = 1; 1; $i++) { | |||||||||||||||||||||||||||
| 27 | 26 | $package = $arg->{make_unique}->($arg->{base}, $arg->{unique_part}); | |||||||||||||||||||||||||||
| 28 | 26 | ▶ | last unless $self->package_exists($package); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | Carp::croak "couldn't generate a pristene package under $arg->{base}" | ||||||||||||||||||||||||||||
| 30 | 7 | ▶ | if $i >= $arg->{max_tries}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 19 | ▶ | my @data = $arg->{data} ? @{ $arg->{data} } : (); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | push @data, ( | ||||||||||||||||||||||||||||
| 36 | ($arg->{isa} ? (ISA => (ref $arg->{isa} ? $arg->{isa} : [ $arg->{isa} ])) | ||||||||||||||||||||||||||||
| 37 | : ()), | ||||||||||||||||||||||||||||
| 38 | 19 | ▶ | ($arg->{version} ? (VERSION => $arg->{version}) : ()), | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 39 | ); | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 19 | ▶ | if (@data) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 5 | $self->assign_symbols($package, \@data); | |||||||||||||||||||||||||||
| 43 | } else { | ||||||||||||||||||||||||||||
| 44 | # This ensures that even without symbols, the package is created so that it | ||||||||||||||||||||||||||||
| 45 | # will not be detected as pristene by package_exists. Without this line of | ||||||||||||||||||||||||||||
| 46 | # code, non-unique tests will fail. -- rjbs, 2006-04-14 | ||||||||||||||||||||||||||||
| 47 | { | ||||||||||||||||||||||||||||
| 48 | ## no critic (ProhibitNoStrict) | ||||||||||||||||||||||||||||
| 49 | 4 | ▶ | no strict qw(refs); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 50 | 4 | ▶ | no warnings qw(void); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 51 | 14 | %{$package . '::'}; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 18 | return $package; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 5 | ▶ | sub assign_symbols { | ||||||||||||||||||||||||||
|
Subroutine
assign_symbols: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 60 | 5 | my ($self, $package, $key_value_pairs) = @_; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 5 | ▶ | Carp::croak "list of key/value pairs must be even!" if @$key_value_pairs % 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | ## no critic (ProhibitNoStrict) | ||||||||||||||||||||||||||||
| 65 | 4 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 66 | 4 | ▶ | while (my ($name, $value) = splice @$key_value_pairs, 0, 2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 11 | my $full_name = "$package\:\:$name"; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 11 | ▶ | if (!ref($value) or Scalar::Util::blessed($value)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not ref $value or builtin::blessed($value)
MC/DC100%2/2
not ref $value or builtin::blessed($value)
not ref $value
builtin::blessed($value)
Truth table100%3/3
not(A) or B
A= not ref $valueB= builtin::blessed($value)
| |||||||||||||||||||||||||||||
| 70 | 6 | ${$full_name} = $value; | |||||||||||||||||||||||||||
| 71 | } else { | ||||||||||||||||||||||||||||
| 72 | 5 | *{$full_name} = $value; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 39 | ▶ | sub package_exists { | ||||||||||||||||||||||||||
|
Subroutine
package_exists: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 79 | 39 | my ($self, $package) = @_; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 39 | return defined *{$package . '::'}; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | # My first attempt! How silly I felt when I threw in some Data::Dumper and saw | ||||||||||||||||||||||||||||
| 85 | # that the above would suffice. -- rjbs, 2006-04-14 | ||||||||||||||||||||||||||||
| 86 | # | ||||||||||||||||||||||||||||
| 87 | # my @parts = split /::/, $package; | ||||||||||||||||||||||||||||
| 88 | # | ||||||||||||||||||||||||||||
| 89 | # my $current_pkg = 'main'; | ||||||||||||||||||||||||||||
| 90 | # for (@parts) { | ||||||||||||||||||||||||||||
| 91 | # my $current_stash = do { no strict 'refs'; \%{$current_pkg . "::"} }; | ||||||||||||||||||||||||||||
| 92 | # return unless exists $current_stash->{$_ . "::"}; | ||||||||||||||||||||||||||||
| 93 | # $current_pkg .= "::$_" | ||||||||||||||||||||||||||||
| 94 | # } | ||||||||||||||||||||||||||||
| 95 | # return 1; | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 4 | 1; | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | __END__ | ||||||||||||||||||||||||||||