File Coverage

blib/lib/Workflow/Config/Perl.pm
Criterion Covered Total %
statement 70 77 90.9
branch 13 18 72.2
condition 2 3 66.6
subroutine 12 14 85.7
pod 1 1 100.0
total 98 113 86.7


line stmt bran cond sub pod time code
1             package Workflow::Config::Perl;
2              
3 4     4   200444 use warnings;
  4         13  
  4         374  
4 4     4   31 use strict;
  4         10  
  4         137  
5 4     4   61 use v5.14.0;
  4         17  
6 4     4   26 use parent qw( Workflow::Config );
  4         10  
  4         38  
7 4     4   448 use Log::Any qw( $log );
  4         11  
  4         41  
8 4     4   3823 use Workflow::Exception qw( configuration_error );
  4         13  
  4         338  
9 4     4   28 use Data::Dumper qw( Dumper );
  4         8  
  4         244  
10 4     4   2986 use English qw( -no_match_vars );
  4         5838  
  4         30  
11              
12             $Workflow::Config::Perl::VERSION = '2.09';
13              
14             sub parse {
15 27     27 1 38553 my ( $self, $type, @items ) = @_;
16              
17 27         151 $self->_check_config_type($type);
18              
19 26 100       140 if ( !scalar @items ) {
20 1         5 return @items;
21             }
22              
23 25         125 my @config_items = Workflow::Config::_expand_refs(@items);
24 25 50       74 return () unless ( scalar @config_items );
25              
26 25         53 my @config = ();
27 25         58 foreach my $item (@config_items) {
28 28         58 my ( $file_name, $method );
29 28 50       73 if ( ref $item ) {
30 0         0 $method = '_translate_perl';
31 0         0 $file_name = '[scalar ref]';
32             }
33              
34             # $item is a filename...
35             else {
36 28         59 $method = '_translate_perl_file';
37 28         55 $file_name = $item;
38             }
39 28         186 $log->info("Will parse '$type' Perl config file '$file_name'");
40 28         314 my $this_config = $self->$method( $type, $item );
41              
42             #warn "This config looks like:";
43             #warn Dumper (\$this_config);
44 26         168 $log->info("Parsed Perl '$file_name' ok");
45              
46 26 100 66     198 if ( exists $this_config->{'type'} ) {
    100          
47 10         50 $log->debug("Adding typed configuration for '$type'");
48 10         55 push @config, $this_config;
49             } elsif ( $type eq 'persister'
50             and ref $this_config->{$type} eq 'ARRAY' )
51             {
52              
53             # This special exception for persister is required because
54             # the config design for persisters was different from the
55             # other config types. It didn't have a top level 'persister'
56             # element. For backward compatibility, I'm adding this
57             # exception here.
58 1         6 $log->debug("Adding multiple configurations for '$type'");
59 1         4 push @config, @{ $this_config->{$type} };
  1         5  
60             } else {
61 15         69 $log->debug("Adding single configuration for '$type'");
62 15         100 push @config, $this_config;
63             }
64             }
65 23         162 return @config;
66             }
67              
68             sub _translate_perl_file {
69 28     28   86 my ( $class, $type, $file ) = @_;
70              
71 28         186 local $INPUT_RECORD_SEPARATOR = undef;
72 28 100       1915 open( CONF, '<', $file )
73             || configuration_error "Cannot read file '$file': $!";
74 26         1337 my $config = <CONF>;
75 26 50       437 close(CONF) || configuration_error "Cannot close file '$file': $!";
76 26         179 my $data = $class->_translate_perl( $type, $config, $file );
77 26     0   273 $log->debug( sub { "Translated '$type' '$file' into: ", Dumper($data) } );
  0         0  
78 26         275 return $data;
79             }
80              
81             sub _translate_perl {
82 26     26   120 my ( $class, $type, $config, $file ) = @_;
83              
84 4     4   5700 no strict 'vars';
  4         14  
  4         1687  
85 26         50 my $data;
86             my $error;
87 26         58 my $warnings = '';
88 26         1457 my $success = do {
89 26         50 local $@;
90              
91 26     0   290 local $SIG{__WARN__} = sub { $warnings .= $_[0] };
  0         0  
92 26         14535 my $rv = eval "\$data = do { $config }; 1;";
93 26         154 $error = $EVAL_ERROR;
94 26         191 $rv;
95             };
96 26 50       109 if ($warnings) {
97 0         0 $warnings =~ s/\r?\n/\\n/g; # don't log line-endings
98 0         0 $log->warn( 'Config evaluation warned: ', $warnings );
99             }
100 26 50       171 if (not $success) {
101 0         0 configuration_error "Cannot evaluate perl data structure ",
102             "in '$file': $error";
103             }
104 26         144 return $data;
105             }
106              
107             1;
108              
109             __END__
110              
111             =pod
112              
113             =head1 NAME
114              
115             Workflow::Config::Perl - Parse workflow configurations as Perl data structures
116              
117             =head1 VERSION
118              
119             This documentation describes version 2.09 of this package
120              
121             =head1 SYNOPSIS
122              
123             # either of these is acceptable
124             my $parser = Workflow::Config->new( 'perl' );
125             my $parser = Workflow::Config->new( 'pl' );
126              
127             my $conf = $parser->parse( 'condition',
128             'my_conditions.pl', 'your_conditions.perl' );
129              
130             =head1 DESCRIPTION
131              
132             Implementation of configuration parser for serialized Perl data
133             structures from files/data. See L<Workflow::Config> for C<parse()>
134             description.
135              
136             =head1 METHODS
137              
138             =head2 parse
139              
140             This method is required implemented by L<Workflow::Config>.
141              
142             It takes two arguments:
143              
144             =over
145              
146             =item * a string indicating the type of configuration. For a complete list of
147             types please refer to L<Workflow::Config>
148              
149             =item * a list of filenames containing at least a single file name
150              
151             =back
152              
153             The method returns a list of configuration parameters.
154              
155             =head1 SEE ALSO
156              
157             =over
158              
159             =item * L<Workflow::Config>
160              
161             =back
162              
163             =head1 COPYRIGHT
164              
165             Copyright (c) 2004-2021 Chris Winters. All rights reserved.
166              
167             This library is free software; you can redistribute it and/or modify
168             it under the same terms as Perl itself.
169              
170             Please see the F<LICENSE>
171              
172             =head1 AUTHORS
173              
174             Please see L<Workflow>
175              
176             =cut