| 1 | package App::PerlShell::Config; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | ######################################################## | ||||||||||||||||||||||||||||
| 4 | # AUTHOR = Michael Vincent | ||||||||||||||||||||||||||||
| 5 | # www.VinsWorld.com | ||||||||||||||||||||||||||||
| 6 | ######################################################## | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use Exporter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 3 | our %EXPORT_TAGS = ( 'config_where' => [qw( config_where )], ); | |||||||||||||||||||||||||||
| 15 | 3 | our @EXPORT_OK = ( @{$EXPORT_TAGS{'config_where'}} ); | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 3 | our @ISA = qw( Exporter ); | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=12.3
Pod
documented
| |||||||||||||||||||||||||||||
| 20 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 21 | 1 | ▶ | my $class = ref($self) || $self; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table33%1/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 1 | ▶ | if ( ( @_ % 2 ) == 1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | 0 | croak("Insufficient number of args - @_"); | |||||||||||||||||||||||||||
| 25 | } else { | ||||||||||||||||||||||||||||
| 26 | 1 | my %cfg = @_; | |||||||||||||||||||||||||||
| 27 | 1 | return bless \%cfg, $class; | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 11 | ▶ | sub config { | ||||||||||||||||||||||||||
|
Subroutine
config: called CC=13 SCAR=30.9
Pod
documented
| |||||||||||||||||||||||||||||
| 32 | 11 | my $self = shift; | |||||||||||||||||||||||||||
| 33 | 11 | my ( $key, $value ) = @_; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 11 | my $retType = wantarray; | |||||||||||||||||||||||||||
| 36 | 11 | ▶ | if ( defined $key ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 10 | ▶ | if ( exists $self->{$key} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | # Set | ||||||||||||||||||||||||||||
| 40 | 7 | ▶ | if ( defined $value ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 2 | my $prev = $self->{$key}; | |||||||||||||||||||||||||||
| 42 | 2 | ▶ | if ( $value eq '' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 0 | undef $self->{$key}; | |||||||||||||||||||||||||||
| 44 | } else { | ||||||||||||||||||||||||||||
| 45 | 2 | $self->{$key} = $value; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | 2 | ▶ | return ( defined $prev ) ? $prev : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | # Get | ||||||||||||||||||||||||||||
| 50 | } else { | ||||||||||||||||||||||||||||
| 51 | 5 | ▶ | if ( !defined $retType ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | printf "%-15s => %s\n", $key, | ||||||||||||||||||||||||||||
| 53 | 0 | ▶ | defined( $self->{$key} ) ? $self->{$key} : 'undef'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | 5 | return $self->{$key}; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | } else { | ||||||||||||||||||||||||||||
| 58 | 3 | ▶ | if ( !defined $retType ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 0 | carp("Key not valid - `$key'"); | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | 3 | return undef; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | # Get all | ||||||||||||||||||||||||||||
| 65 | } else { | ||||||||||||||||||||||||||||
| 66 | 1 | ▶ | if ( !defined($retType) ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 67 | 0 | for ( sort( keys( %{$self} ) ) ) { | |||||||||||||||||||||||||||
| 68 | printf "%-15s => %s\n", $_, | ||||||||||||||||||||||||||||
| 69 | 0 | ▶ | defined( $self->{$_} ) ? $self->{$_} : 'undef'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | } elsif ($retType) { | ||||||||||||||||||||||||||||
| 72 | 0 | return %{$self}; | |||||||||||||||||||||||||||
| 73 | } else { | ||||||||||||||||||||||||||||
| 74 | 1 | my %ret = %{$self}; | |||||||||||||||||||||||||||
| 75 | 1 | return \%ret; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 5 | ▶ | sub add { | ||||||||||||||||||||||||||
|
Subroutine
add: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 81 | 5 | my $self = shift; | |||||||||||||||||||||||||||
| 82 | 5 | my ( $key, $value ) = @_; | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 5 | my $retType = wantarray; | |||||||||||||||||||||||||||
| 85 | 5 | ▶ | if ( defined $key ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 4 | ▶ | if ( !exists $self->{$key} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 2 | ▶ | if ( defined $value ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 1 | $self->{$key} = $value; | |||||||||||||||||||||||||||
| 89 | } else { | ||||||||||||||||||||||||||||
| 90 | 1 | $self->{$key} = undef; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | 2 | return 1; | |||||||||||||||||||||||||||
| 93 | } else { | ||||||||||||||||||||||||||||
| 94 | 2 | ▶ | if ( !defined $retType ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 1 | carp("Key exists"); | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | } else { | ||||||||||||||||||||||||||||
| 99 | 1 | carp("Key required"); | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | 3 | return 0; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 4 | ▶ | sub delete { | ||||||||||||||||||||||||||
|
Subroutine
delete: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 105 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 106 | 4 | my ($key) = @_; | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 4 | my $retType = wantarray; | |||||||||||||||||||||||||||
| 109 | 4 | ▶ | if ( defined $key ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 3 | ▶ | if ( exists $self->{$key} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 1 | delete $self->{$key}; | |||||||||||||||||||||||||||
| 112 | 1 | return 1; | |||||||||||||||||||||||||||
| 113 | } else { | ||||||||||||||||||||||||||||
| 114 | 2 | ▶ | if ( !defined $retType ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 1 | carp("Key doesn't exist"); | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | } else { | ||||||||||||||||||||||||||||
| 119 | 1 | carp("Key required"); | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | 3 | return 0; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 3 | ▶ | sub exists { | ||||||||||||||||||||||||||
|
Subroutine
exists: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 125 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 126 | 3 | my ($key) = @_; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 3 | ▶ | if ( defined $key ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 2 | ▶ | if ( exists $self->{$key} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 1 | return 1; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | } else { | ||||||||||||||||||||||||||||
| 133 | 1 | carp("Key required"); | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | 2 | return 0; | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 0 | ▶ | sub config_where { | ||||||||||||||||||||||||||
|
Subroutine
config_where: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||
| 139 | 0 | my ( $config_file, $dir ) = @_; | |||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 0 | my $home_dir = $ENV{HOME}; | |||||||||||||||||||||||||||
| 142 | 0 | ▶ | if ( ( $^O eq 'MSWin32' ) and !( defined $home_dir ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$^O eq "MSWin32" and not defined $home_dir
MC/DC0%0/2
$^O eq "MSWin32" and not defined $home_dir
$^O eq "MSWin32"
not defined $home_dir
Truth table0%0/3
A and not(B)
A= $^O eq "MSWin32"B= not defined $home_dir
| |||||||||||||||||||||||||||||
| 143 | 0 | $home_dir = $ENV{USERPROFILE}; | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | # 1 current working directory | ||||||||||||||||||||||||||||
| 147 | 0 | ▶ | if ( -e $config_file ) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 148 | 0 | return $config_file | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | # 2 user home directory | ||||||||||||||||||||||||||||
| 151 | } elsif ( -e $home_dir . "/" . $config_file ) { | ||||||||||||||||||||||||||||
| 152 | 0 | return $home_dir . "/" . $config_file; | |||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | # 3 provided directory | ||||||||||||||||||||||||||||
| 155 | ▶ | } elsif ( ( defined $dir ) and ( -e $dir . "/" . $config_file ) ) { | |||||||||||||||||||||||||||
|
Condition0%0/3
defined $dir and -e $dir . '/' . $config_file
MC/DC0%0/2
defined $dir and -e $dir . '/' . $config_file
defined $dir
-e $dir . '/' . $config_file
Truth table0%0/3
A and B
A= defined $dirB= -e $dir . '/' . $config_file
| |||||||||||||||||||||||||||||
| 156 | 0 | return $dir . "/" . $config_file; | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | # not found - indicate so in config | ||||||||||||||||||||||||||||
| 159 | } else { | ||||||||||||||||||||||||||||
| 160 | 0 | return; | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 3 | 1; | |||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | __END__ | ||||||||||||||||||||||||||||