| line | stmt | bran | cond | sub | pod | time | code | 
| 1 | 1 |  |  | 1 |  | 1558 | use 5.006; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 43 |  | 
| 2 | 1 |  |  | 1 |  | 4 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 38 |  | 
| 3 | 1 |  |  | 1 |  | 7 | use warnings; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 57 |  | 
| 4 |  |  |  |  |  |  | package CPAN::Testers::Config; | 
| 5 |  |  |  |  |  |  | # ABSTRACT: Manage CPAN Testers configuration data | 
| 6 |  |  |  |  |  |  | our $VERSION = '0.002'; # VERSION | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 1 |  |  | 1 |  | 7 | use Carp            (); | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 23 |  | 
| 9 | 1 |  |  | 1 |  | 3686 | use Data::Dumper    (); | 
|  | 1 |  |  |  |  | 7254 |  | 
|  | 1 |  |  |  |  | 27 |  | 
| 10 | 1 |  |  | 1 |  | 11 | use File::Basename  (); | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 15 |  | 
| 11 | 1 |  |  | 1 |  | 1944 | use File::HomeDir   (); | 
|  | 1 |  |  |  |  | 7361 |  | 
|  | 1 |  |  |  |  | 25 |  | 
| 12 | 1 |  |  | 1 |  | 8 | use File::Path      (); | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 17 |  | 
| 13 | 1 |  |  | 1 |  | 4 | use File::Spec      (); | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 590 |  | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | #--------------------------------------------------------------------------# | 
| 16 |  |  |  |  |  |  | # new -- construct empty object | 
| 17 |  |  |  |  |  |  | #--------------------------------------------------------------------------# | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | sub new { | 
| 20 | 9 |  |  | 9 | 1 | 2187 | my ($class, @args) = @_; | 
| 21 | 9 | 50 |  |  |  | 36 | Carp::confess( "\$class->new() requires an even number of arguments" ) | 
| 22 |  |  |  |  |  |  | if @args % 2; | 
| 23 | 9 |  |  |  |  | 88 | return bless { @args }, $class; | 
| 24 |  |  |  |  |  |  | } | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  | #--------------------------------------------------------------------------# | 
| 27 |  |  |  |  |  |  | # read -- load data from config file | 
| 28 |  |  |  |  |  |  | #--------------------------------------------------------------------------# | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | sub read { | 
| 31 | 6 |  |  | 6 | 1 | 3888 | my ($self, $file) = @_; | 
| 32 | 6 | 100 |  |  |  | 34 | $self = $self->new unless ref $self; # if called as class method | 
| 33 | 6 | 100 |  |  |  | 22 | $file = $self->config_file unless defined $file; | 
| 34 |  |  |  |  |  |  |  | 
| 35 | 6 | 100 |  |  |  | 531 | open my $fh, "<", $file or Carp::confess("Error reading '$file': $!"); | 
| 36 | 5 |  |  |  |  | 8 | my $data = eval do {local $/; <$fh>}; ## no critic | 
|  | 5 |  |  |  |  | 20 |  | 
|  | 5 |  |  |  |  | 471 |  | 
| 37 | 5 | 50 |  |  |  | 25 | if ( ref $data eq 'HASH' ) { | 
| 38 | 5 |  |  |  |  | 21 | %$self = %$data; | 
| 39 |  |  |  |  |  |  | } | 
| 40 |  |  |  |  |  |  | else { | 
| 41 | 0 | 0 |  |  |  | 0 | my $err = $@ ? $@ : "did not eval() to a perl hash"; | 
| 42 | 0 |  |  |  |  | 0 | Carp::confess("Could not eval config file '$file': $err"); | 
| 43 |  |  |  |  |  |  | } | 
| 44 | 5 |  |  |  |  | 84 | return $self; | 
| 45 |  |  |  |  |  |  | } | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  | #--------------------------------------------------------------------------# | 
| 48 |  |  |  |  |  |  | # write -- save data to config file | 
| 49 |  |  |  |  |  |  | #--------------------------------------------------------------------------# | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | sub write { | 
| 52 | 4 |  |  | 4 | 1 | 10 | my ($self, $file) = @_; | 
| 53 | 4 | 50 |  |  |  | 17 | $self = $self->new unless ref $self; # class method will write empty file | 
| 54 | 4 | 100 |  |  |  | 18 | $file = $self->config_file unless defined $file; | 
| 55 |  |  |  |  |  |  |  | 
| 56 | 4 |  |  |  |  | 820 | File::Path::mkpath( File::Basename::dirname( $file ) ); | 
| 57 | 4 | 50 |  |  |  | 365 | open my $fh, ">", $file or Carp::confess("Error writing '$file': $!"); | 
| 58 | 4 |  |  |  |  | 9 | print {$fh} $self->_data_dump; | 
|  | 4 |  |  |  |  | 17 |  | 
| 59 | 4 |  |  |  |  | 688 | close $fh; | 
| 60 | 4 |  |  |  |  | 23 | return $self; | 
| 61 |  |  |  |  |  |  | } | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  | #--------------------------------------------------------------------------# | 
| 64 |  |  |  |  |  |  | # config_dir -- get config directory name, given %ENV overrides | 
| 65 |  |  |  |  |  |  | #--------------------------------------------------------------------------# | 
| 66 |  |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  | sub config_dir { | 
| 68 | 10 |  |  | 10 | 1 | 8636 | my ($self) = @_; | 
| 69 | 10 |  | 66 |  |  | 118 | return  $ENV{CPAN_TESTERS_DIR} | 
| 70 |  |  |  |  |  |  | ||  File::Spec->catdir(File::HomeDir->my_home, '.cpantesters'); | 
| 71 |  |  |  |  |  |  | } | 
| 72 |  |  |  |  |  |  |  | 
| 73 |  |  |  |  |  |  | #--------------------------------------------------------------------------# | 
| 74 |  |  |  |  |  |  | # config_file -- get config file name, given %ENV overrides | 
| 75 |  |  |  |  |  |  | #--------------------------------------------------------------------------# | 
| 76 |  |  |  |  |  |  |  | 
| 77 |  |  |  |  |  |  | sub config_file { | 
| 78 | 9 |  |  | 9 | 1 | 2046 | my ($self) = @_; | 
| 79 | 9 |  | 100 |  |  | 46 | my $path = $ENV{CPAN_TESTERS_CONFIG} || 'config.pl'; | 
| 80 | 9 | 100 |  |  |  | 82 | my $file = File::Spec->file_name_is_absolute($path) | 
| 81 |  |  |  |  |  |  | ? $path | 
| 82 |  |  |  |  |  |  | : File::Spec->catfile($self->config_dir, $path) ; | 
| 83 | 9 |  |  |  |  | 147 | return $file | 
| 84 |  |  |  |  |  |  | } | 
| 85 |  |  |  |  |  |  |  | 
| 86 |  |  |  |  |  |  | #--------------------------------------------------------------------------# | 
| 87 |  |  |  |  |  |  | # _data_dump -- Copied from Module::Build::Dumper | 
| 88 |  |  |  |  |  |  | #--------------------------------------------------------------------------# | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  | sub _data_dump { | 
| 91 | 4 |  |  | 4 |  | 8 | my ($self) = @_; | 
| 92 | 4 |  |  |  |  | 25 | my %data = %$self; | 
| 93 | 4 |  |  |  |  | 47 | return ("do{ my " | 
| 94 |  |  |  |  |  |  | . Data::Dumper->new([\%data],['x'])->Purity(1)->Terse(0)->Dump() | 
| 95 |  |  |  |  |  |  | . '$x; }') | 
| 96 |  |  |  |  |  |  | } | 
| 97 |  |  |  |  |  |  |  | 
| 98 |  |  |  |  |  |  | 1; | 
| 99 |  |  |  |  |  |  |  | 
| 100 |  |  |  |  |  |  | __END__ |