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