| 1 | package Config::PFiles::Path; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # ABSTRACT: manipulate PFILES path for IRAF Compatible parameter files | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 12 | ▶ | use 5.008009; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 12 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 12 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 12 | ▶ | use Symbol (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 12 | ▶ | use Sub::Uplevel (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 12 | our $VERSION = '0.04'; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 12 | ▶ | use overload '""' => '_export' ; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | my %is_mutator | ||||||||||||||||||||||||||||
| 18 | 12 | = map { $_ => 1 } qw( _append _prepend _replace _remove ); | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 12 | our $AUTOLOAD; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 2 | ▶ | sub _croak { | ||||||||||||||||||||||||||
|
Subroutine
_croak: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | 2 | require Carp; | |||||||||||||||||||||||||||
| 33 | 2 | goto &Carp::croak; | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | # allow the user to do thing when loading the package | ||||||||||||||||||||||||||||
| 37 | 11 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 11 | my $package = shift; | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 11 | ▶ | return unless @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 5 | my $method = shift; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | _croak( "Can't call method '$method' in this context\n" ) | ||||||||||||||||||||||||||||
| 46 | 5 | ▶ | unless $is_mutator{ "_$method" }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 4 | $AUTOLOAD = $method; | |||||||||||||||||||||||||||
| 49 | 4 | Sub::Uplevel::uplevel( 1, \&AUTOLOAD, $package, @_ ); | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 124 | ▶ | sub AUTOLOAD { | ||||||||||||||||||||||||||
|
Subroutine
AUTOLOAD: called CC=9 SCAR=22.6
| |||||||||||||||||||||||||||||
| 53 | 124 | (my $method = our $AUTOLOAD) =~ s/.*:://; | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | # we don't have a DESTROY method, so ignore it. | ||||||||||||||||||||||||||||
| 56 | 124 | ▶ | return if $method =~ /DESTROY/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 81 | my $imethod = '_' . $method; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 81 | my $subref = *{Symbol::qualify_to_ref($imethod,__PACKAGE__)}{CODE}; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | # make sure it's an existing method | ||||||||||||||||||||||||||||
| 63 | 81 | ▶ | _croak( qq{Can't locate object method "$method" via package "}, | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$method =~ /^_/ or not defined $subref
MC/DC0%0/2
$method =~ /^_/ or not defined $subref
$method =~ /^_/
not defined $subref
Truth table33%1/3
A or not(B)
A= $method =~ /^_/B= not defined $subref
| |||||||||||||||||||||||||||||
| 64 | __PACKAGE__, q{"} ) | ||||||||||||||||||||||||||||
| 65 | if $method =~ /^_/ || ! defined $subref; | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | # is this an object invocation? | ||||||||||||||||||||||||||||
| 69 | 81 | ▶ | if ( ref $_[0] && $_[0]->isa(__PACKAGE__) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $_[0] and $_[0]->isa(__PACKAGE__)
MC/DC50%1/2
ref $_[0] and $_[0]->isa(__PACKAGE__)
ref $_[0]
$_[0]->isa(__PACKAGE__)
Truth table66%2/3
A and B
A= ref $_[0]B= $_[0]->isa(__PACKAGE__)
| |||||||||||||||||||||||||||||
| 70 | { | ||||||||||||||||||||||||||||
| 71 | 61 | goto &$imethod; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | # nope. create default object based on $ENV{PFILES} and replace | ||||||||||||||||||||||||||||
| 75 | # the class name in the argument list with the new object | ||||||||||||||||||||||||||||
| 76 | 20 | my $package = shift; | |||||||||||||||||||||||||||
| 77 | 20 | my $env = $package->new( $ENV{PFILES} ); | |||||||||||||||||||||||||||
| 78 | 20 | unshift @_, $env; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | # if the method will alter the path, make sure to update $ENV{PFILES} | ||||||||||||||||||||||||||||
| 82 | # after it has been run | ||||||||||||||||||||||||||||
| 83 | 20 | ▶ | if ( $is_mutator{$imethod} ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | { | ||||||||||||||||||||||||||||
| 85 | # respect calling context | ||||||||||||||||||||||||||||
| 86 | 17 | my $wantarray = wantarray(); | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | # void | ||||||||||||||||||||||||||||
| 89 | 17 | ▶ | if ( ! defined $wantarray ) | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 90 | { | ||||||||||||||||||||||||||||
| 91 | 13 | Sub::Uplevel::uplevel( 1, $subref, @_ ); | |||||||||||||||||||||||||||
| 92 | 13 | $ENV{PFILES} = $env->_export; | |||||||||||||||||||||||||||
| 93 | 13 | return; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | # list | ||||||||||||||||||||||||||||
| 97 | elsif ( $wantarray) | ||||||||||||||||||||||||||||
| 98 | { | ||||||||||||||||||||||||||||
| 99 | 4 | my @results = Sub::Uplevel::uplevel( 1, $subref, @_ ); | |||||||||||||||||||||||||||
| 100 | 4 | $ENV{PFILES} = $env->_export; | |||||||||||||||||||||||||||
| 101 | 4 | return @results; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | # scalar | ||||||||||||||||||||||||||||
| 105 | else | ||||||||||||||||||||||||||||
| 106 | { | ||||||||||||||||||||||||||||
| 107 | 0 | my $result = Sub::Uplevel::uplevel( 1, $subref, @_ ); | |||||||||||||||||||||||||||
| 108 | 0 | $ENV{PFILES} = $env->_export; | |||||||||||||||||||||||||||
| 109 | 0 | return $result; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | # nope, just execute the method | ||||||||||||||||||||||||||||
| 114 | else | ||||||||||||||||||||||||||||
| 115 | { | ||||||||||||||||||||||||||||
| 116 | 3 | goto &$imethod; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 43 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 121 | 43 | my ( $class, $pfiles ) = @_; | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 43 | my $self = bless {}, $class; | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 43 | $self->__init( $pfiles ); | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 42 | return $self; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 43 | ▶ | sub __init { | ||||||||||||||||||||||||||
|
Subroutine
__init: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 131 | 43 | my ( $self, $pfiles ) = @_; | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 43 | ▶ | $pfiles ||= q{}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$pfiles ||= ""
MC/DC100%1/1
$pfiles ||= ""
$pfiles
Truth table100%2/2
A
A= $pfiles
| |||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 43 | my %dirs; | |||||||||||||||||||||||||||
| 136 | 43 | @dirs{ qw( RW RO ) } = | |||||||||||||||||||||||||||
| 137 | $pfiles =~ /^ | ||||||||||||||||||||||||||||
| 138 | ([^;]*) # grab everything that's not a semicolon (RW) | ||||||||||||||||||||||||||||
| 139 | (?:|;(.*)) # and everything that's after a semicolon (RO) | ||||||||||||||||||||||||||||
| 140 | $/x; | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | _croak( "illegal path: too many semi-colons: $pfiles\n" ) | ||||||||||||||||||||||||||||
| 143 | 43 | ▶ | if defined $dirs{RO} && $dirs{RO} =~ /;/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $dirs{"RO"} and $dirs{"RO"} =~ /;/
MC/DC100%2/2
defined $dirs{"RO"} and $dirs{"RO"} =~ /;/
defined $dirs{"RO"}
$dirs{"RO"} =~ /;/
Truth table100%3/3
A and B
A= defined $dirs{"RO"}B= $dirs{"RO"} =~ /;/
| |||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | # split and store non-empty paths | ||||||||||||||||||||||||||||
| 146 | 88 | ▶ | $self->{$_} = [ grep { $_ ne '' } split( /:/, $dirs{$_} || q{} ) ] | ||||||||||||||||||||||||||
|
Condition100%2/2
$dirs{$_} || ''
MC/DC100%1/1
$dirs{$_} || ''
$dirs{$_}
Truth table100%2/2
A
A= $dirs{$_}
| |||||||||||||||||||||||||||||
| 147 | 42 | for qw( RW RO ); | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 42 | return; | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 69 | ▶ | sub __check_set { | ||||||||||||||||||||||||||
|
Subroutine
__check_set: called CC=2 SCAR=7.9
| |||||||||||||||||||||||||||||
| 153 | 69 | my ( $dir_set ) = shift; | |||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 69 | my $match; | |||||||||||||||||||||||||||
| 156 | 69 | ▶ | unless ( ($match ) = $dir_set =~ /^(RW|RO)$/i ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | { | ||||||||||||||||||||||||||||
| 158 | 0 | local $Carp::CarpLevel = $Carp::CarpLevel + 1; | |||||||||||||||||||||||||||
| 159 | 0 | _croak( "illegal value for directory set: $dir_set\n" ) | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 69 | return uc($match); | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 7 | ▶ | sub _append { | ||||||||||||||||||||||||||
|
Subroutine
_append: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 166 | 7 | my ( $self, $dir_set, @dirs ) = @_; | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 7 | push @{$self->{__check_set($dir_set)}}, @dirs; | |||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 7 | return; | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 8 | ▶ | sub _prepend { | ||||||||||||||||||||||||||
|
Subroutine
_prepend: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 174 | 8 | my ( $self, $dir_set, @dirs ) = @_; | |||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 8 | unshift @{$self->{__check_set($dir_set)}}, @dirs; | |||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 8 | return; | |||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 32 | ▶ | sub _extract { | ||||||||||||||||||||||||||
|
Subroutine
_extract: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 182 | 32 | my ( $self, $dir_set ) = @_; | |||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 32 | return @{$self->{__check_set($dir_set)}}; | |||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 22 | ▶ | sub _replace { | ||||||||||||||||||||||||||
|
Subroutine
_replace: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 188 | 22 | my ( $self, $dir_set, @dirs ) = @_; | |||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 22 | $dir_set = __check_set($dir_set); | |||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 22 | my @old = @{$self->{$dir_set}}; | |||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 22 | $self->{$dir_set} = [ @dirs ]; | |||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | return @old | ||||||||||||||||||||||||||||
| 197 | 22 | ▶ | if defined wantarray; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | 14 | return; | |||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 11 | ▶ | sub _remove { | ||||||||||||||||||||||||||
|
Subroutine
_remove: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 203 | 11 | my ( $self, $dir_set ) = @_; | |||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | 11 | return $self->replace( $dir_set ); | |||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | 23 | ▶ | sub _export { | ||||||||||||||||||||||||||
|
Subroutine
_export: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 209 | 23 | my ( $self ) = @_; | |||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | # join together the non-empty directories in the sets; | ||||||||||||||||||||||||||||
| 212 | my ( $rw, $ro ) = | ||||||||||||||||||||||||||||
| 213 | 23 | map { join( q{:}, grep { $_ ne q{} } @{$self->{$_}} ) } | |||||||||||||||||||||||||||
| 214 | qw( RW RO ); | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | # construct a rational path | ||||||||||||||||||||||||||||
| 217 | return | ||||||||||||||||||||||||||||
| 218 | 23 | ▶ | $rw eq q{} ? ";$ro" | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 219 | : $ro eq q{} ? $rw | ||||||||||||||||||||||||||||
| 220 | : "$rw;$ro"; | ||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | 12 | 1; | |||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | # | ||||||||||||||||||||||||||||
| 227 | # This file is part of Config-PFiles-Path | ||||||||||||||||||||||||||||
| 228 | # | ||||||||||||||||||||||||||||
| 229 | # This software is Copyright (c) 2007 by Smithsonian Astrophysical Observatory. | ||||||||||||||||||||||||||||
| 230 | # | ||||||||||||||||||||||||||||
| 231 | # This is free software, licensed under: | ||||||||||||||||||||||||||||
| 232 | # | ||||||||||||||||||||||||||||
| 233 | # The GNU General Public License, Version 3, June 2007 | ||||||||||||||||||||||||||||
| 234 | # | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | __END__ | ||||||||||||||||||||||||||||