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