File Coverage

lib/Workflow/Config/Perl.pm
Criterion Covered Total %
statement 66 68 97.0
branch 13 16 81.2
condition 3 6 50.0
subroutine 12 12 100.0
pod 1 1 100.0
total 95 103 92.2


line stmt bran cond sub pod time code
1             package Workflow::Config::Perl;
2              
3 3     3   1594 use warnings;
  3         5  
  3         120  
4 3     3   15 use strict;
  3         5  
  3         103  
5 3     3   15 use base qw( Workflow::Config );
  3         6  
  3         433  
6 3     3   19 use Log::Log4perl qw( get_logger );
  3         6  
  3         28  
7 3     3   231 use Workflow::Exception qw( configuration_error );
  3         7  
  3         181  
8 3     3   17 use Data::Dumper qw( Dumper );
  3         4  
  3         130  
9 3     3   17 use English qw( -no_match_vars );
  3         14  
  3         27  
10              
11             $Workflow::Config::Perl::VERSION = '1.62';
12              
13             sub parse {
14 26     26 1 13079 my ( $self, $type, @items ) = @_;
15 26   33     131 my $log ||= get_logger();
16              
17 26         2279 $self->_check_config_type($type);
18              
19 25 100       65 if ( !scalar @items ) {
20 1         4 return @items;
21             }
22              
23 24         55 my @config_items = Workflow::Config::_expand_refs(@items);
24 24 50       56 return () unless ( scalar @config_items );
25              
26 24         34 my @config = ();
27 24         62 foreach my $item (@config_items) {
28 27         39 my ( $file_name, $method );
29 27 50       51 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 27         39 $method = '_translate_perl_file';
37 27         37 $file_name = $item;
38             }
39 27         118 $log->info("Will parse '$type' Perl config file '$file_name'");
40 27         9874 my $this_config = $self->$method( $type, $item );
41              
42             #warn "This config looks like:";
43             #warn Dumper (\$this_config);
44 25         121 $log->info("Parsed Perl '$file_name' ok");
45              
46 25 100 66     8606 if ( exists $this_config->{'type'} ) {
    100          
47 10         66 $log->debug("Adding typed configuration for '$type'");
48 10         3355 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         8 $log->debug("Adding multiple configurations for '$type'");
59 1         329 push @config, @{ $this_config->{$type} };
  1         4  
60             } else {
61 14         69 $log->debug("Adding single configuration for '$type'");
62 14         4743 push @config, $this_config;
63             }
64             }
65 22         135 return @config;
66             }
67              
68             sub _translate_perl_file {
69 27     27   64 my ( $class, $type, $file ) = @_;
70 27         73 my $log = get_logger();
71              
72 27         955 local $INPUT_RECORD_SEPARATOR = undef;
73 27 100       1161 open( CONF, '<', $file )
74             || configuration_error "Cannot read file '$file': $!";
75 26         6519 my $config = <CONF>;
76 26 50       370 close(CONF) || configuration_error "Cannot close file '$file': $!";
77 26         111 my $data = $class->_translate_perl( $type, $config, $file );
78 25     25   174 $log->debug( sub { "Translated '$type' '$file' into: ", Dumper($data) } );
  25         543  
79 25         13906 return $data;
80             }
81              
82             sub _translate_perl {
83 26     26   106 my ( $class, $type, $config, $file ) = @_;
84 26         82 my $log = get_logger();
85              
86 3     3   2747 no strict 'vars';
  3         12  
  3         350  
87 26         906 local $EVAL_ERROR = undef;
88 26         3186 my $data = eval $config;
89 26 100       130 if ($EVAL_ERROR) {
90 1         9 configuration_error "Cannot evaluate perl data structure ",
91             "in '$file': $EVAL_ERROR";
92             }
93 25         58 return $data;
94             }
95              
96             1;
97              
98             __END__
99              
100             =pod
101              
102             =head1 NAME
103              
104             Workflow::Config::Perl - Parse workflow configurations as Perl data structures
105              
106             =head1 VERSION
107              
108             This documentation describes version 1.62 of this package
109              
110             =head1 SYNOPSIS
111              
112             # either of these is acceptable
113             my $parser = Workflow::Config->new( 'perl' );
114             my $parser = Workflow::Config->new( 'pl' );
115              
116             my $conf = $parser->parse( 'condition',
117             'my_conditions.pl', 'your_conditions.perl' );
118              
119             =head1 DESCRIPTION
120              
121             Implementation of configuration parser for serialized Perl data
122             structures from files/data. See L<Workflow::Config> for C<parse()>
123             description.
124              
125             =head1 METHODS
126              
127             =head2 parse
128              
129             This method is required implemented by L<Workflow::Config>.
130              
131             It takes two arguments:
132              
133             =over
134              
135             =item * a string indicating the type of configuration. For a complete list of
136             types please refer to L<Workflow::Config>
137              
138             =item * a list of filenames containing at least a single file name
139              
140             =back
141              
142             The method returns a list of configuration parameters.
143              
144             =head1 SEE ALSO
145              
146             =over
147              
148             =item * L<Workflow::Config>
149              
150             =back
151              
152             =head1 COPYRIGHT
153              
154             Copyright (c) 2004-2023 Chris Winters. All rights reserved.
155              
156             This library is free software; you can redistribute it and/or modify
157             it under the same terms as Perl itself.
158              
159             Please see the F<LICENSE>
160              
161             =head1 AUTHORS
162              
163             Please see L<Workflow>
164              
165             =cut