| 1 | ## no critic (ControlStructures::ProhibitPostfixControls) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | ## no critic (ControlStructures::ProhibitCascadingIfElse) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | ## no critic (ValuesAndExpressions::ProhibitConstantPragma) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | package Env::Assert::Functions; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 6 | ▶ | use 5.010; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | # ABSTRACT: The functionality of Env::Assert and bin/envassert. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 6 | our $VERSION = '0.016'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | =encoding utf8 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | =for :stopwords env filepath filepaths params | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 6 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 6 | our @EXPORT_OK = qw( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | assert | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | report_errors | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | file_to_desc | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | ENV_ASSERT_MISSING_FROM_ENVIRONMENT | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | ENV_ASSERT_INVALID_CONTENT_IN_VARIABLE | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | ENV_ASSERT_MISSING_FROM_DEFINITION | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 6 | our %EXPORT_TAGS = ( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 'all' => [ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | qw( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | assert | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | report_errors | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | file_to_desc | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | ENV_ASSERT_MISSING_FROM_ENVIRONMENT | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | ENV_ASSERT_INVALID_CONTENT_IN_VARIABLE | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | ENV_ASSERT_MISSING_FROM_DEFINITION | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | ) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | ], | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 'constants' => [ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | qw( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | ENV_ASSERT_MISSING_FROM_ENVIRONMENT | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | ENV_ASSERT_INVALID_CONTENT_IN_VARIABLE | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | ENV_ASSERT_MISSING_FROM_DEFINITION | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | ) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | ], | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 6 | ▶ | use Cwd qw( abs_path ); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 6 | ▶ | use English qw( -no_match_vars ); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 6 | ▶ | use File::Spec; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 6 | ▶ | use IO::File; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 6 | ▶ | use English qw( -no_match_vars ); # Avoids regex performance penalty in perl 5.18 and earlier | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 6 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 6 | ▶ | use constant { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 6 | ENV_ASSERT_MISSING_FROM_ENVIRONMENT => 1, | |||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | ENV_ASSERT_INVALID_CONTENT_IN_VARIABLE => 2, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | ENV_ASSERT_MISSING_FROM_DEFINITION => 3, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 6 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 6 | ▶ | use constant { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 6 | DEFAULT_PARAMETER_BREAK_AT_FIRST_ERROR => 0, | |||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | INDENT => q{ }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 6 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | Env::Assert::Functions - The functionality of Env::Assert and bin/envassert. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | version 0.016 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | use Env::Assert::Functions qw( assert report_errors ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | my %want = ( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | options => { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | exact => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | variables => { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | USER => { regexp => '^[[:word:]]{1}$', required => 1 }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | my %parameters; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | $parameters{'break_at_first_error'} = 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | my $r = assert( \%ENV, \%want, \%parameters ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | if( ! $r->{'success'} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | print report_errors( $r->{'errors'} ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | =head1 STATUS | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | Package Env::Assert is currently being developed so changes in the API are possible, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | though not likely. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | =head1 NOTES | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | Functionality of L<Env::Assert> has been moved to this package since | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | version 0.013. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | L<Env::Assert> has a different API now. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | It can be used by itself at the start of the program, similar to | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | L<Env::Dot>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | No functions are automatically exported to the calling namespace. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | =head2 assert( \%env, \%want, \%params ) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | Ensure your environment, parameter I<env> (hashref), matches with | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | the environment description, parameter I<want> (hashref). | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | Use parameter I<params> (hashref) to specify processing options. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | Supported params: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | =over 8 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | =item break_at_first_error | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | Verify environment only up until the first error. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | Then break and return with only that error. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | Return: hashref: { success => 1/0, errors => hashref, }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 8 | ▶ | sub assert { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
assert: called CC=18 SCAR=29.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 8 | my ($env, $want, $params) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 8 | ▶ | $params = {} if ! $params; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 8 | ▶ | croak 'Invalid options. Not a hash' if( ref $env ne 'HASH' || ref $want ne 'HASH' ); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $env ne "HASH" or ref $want ne "HASH"
MC/DC0%0/2
ref $env ne "HASH" or ref $want ne "HASH"
ref $env ne "HASH"
ref $want ne "HASH"
Truth table33%1/3
A or B
A= ref $env ne "HASH"B= ref $want ne "HASH"
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | # Set default options | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 8 | ▶ | $params->{'break_at_first_error'} //= DEFAULT_PARAMETER_BREAK_AT_FIRST_ERROR; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$params->{"break_at_first_error"} //= 0
MC/DC100%1/1
$params->{"break_at_first_error"} //= 0
$params->{"break_at_first_error"}
Truth table100%2/2
A
A= $params->{"break_at_first_error"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 8 | my $success = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 8 | my %errors; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 8 | my $vars = $want->{'variables'}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 8 | my $opts = $want->{'options'}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 8 | foreach my $var_name (keys %{ $vars }) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 14 | my $var = $vars->{$var_name}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 14 | ▶ | my $required = $var->{'required'}//1; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$var->{"required"} // 1
MC/DC100%1/1
$var->{"required"} // 1
$var->{"required"}
Truth table100%2/2
A
A= $var->{"required"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 14 | ▶ | my $regexp = $var->{'regexp'}//q{.*}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$var->{"regexp"} // ".*"
MC/DC0%0/1
$var->{"regexp"} // ".*"
$var->{"regexp"}
Truth table50%1/2
A
A= $var->{"regexp"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 14 | ▶ | if( ( $opts->{'exact'} || $required ) && ! defined $env->{$var_name} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%4/6
$opts->{"exact"} || $required and not defined $env->{$var_name}
$opts->{"exact"} or $required
MC/DC33%1/3
$opts->{"exact"} || $required and not defined $env->{$var_name}
$opts->{"exact"}
$required
not defined $env->{$var_name}
Truth table80%4/5
A or B and not(C)
A= $opts->{"exact"}B= $requiredC= not defined $env->{$var_name}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 3 | $success = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 3 | $errors{'variables'}->{ $var_name } = { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | type => ENV_ASSERT_MISSING_FROM_ENVIRONMENT, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | message => "Variable $var_name is missing from environment", | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 3 | ▶ | goto EXIT if( $params->{'break_at_first_error'} ); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | elsif( $env->{$var_name} !~ m/$regexp/msx ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 1 | $success = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 1 | $errors{'variables'}->{ $var_name } = { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | type => ENV_ASSERT_INVALID_CONTENT_IN_VARIABLE, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | message => "Variable $var_name has invalid content", | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 1 | ▶ | goto EXIT if( $params->{'break_at_first_error'} ); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | 8 | ▶ | if( $opts->{'exact'} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 4 | foreach my $var_name (keys %{ $env }) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 6 | ▶ | if( ! exists $vars->{ $var_name } ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | 2 | $success = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 2 | $errors{'variables'}->{ $var_name } = { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | type => ENV_ASSERT_MISSING_FROM_DEFINITION, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | message => "Variable $var_name is missing from description", | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 2 | ▶ | goto EXIT if( $params->{'break_at_first_error'} ); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | EXIT: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | 8 | return { success => $success, errors => \%errors, }; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | =head2 report_errors( \%errors ) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | Report errors in a nicely formatted way. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | 3 | ▶ | sub report_errors { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
report_errors: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | 3 | my ($errors) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | 3 | my $out = q{}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | 3 | $out .= sprintf "Environment Assert: ERRORS:\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | 3 | foreach my $error_area_name (sort keys %{ $errors }) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | 2 | $out .= sprintf "%s%s:\n", INDENT, $error_area_name; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | 2 | foreach my $error_key (sort keys %{ $errors->{$error_area_name} }) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | $out .= sprintf "%s%s: %s\n", INDENT . INDENT, $error_key, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | 3 | $errors->{$error_area_name}->{$error_key}->{'message'}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 3 | return $out; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | =head2 file_to_desc( @rows ) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | Extract an environment description from a F<.envdesc> file. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 3 | ▶ | sub file_to_desc { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
file_to_desc: called CC=7 SCAR=21.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | 3 | my @rows = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | 3 | my %desc = ( 'options' => {}, 'variables' => {}, ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | 3 | foreach (@rows) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | # This is envassert meta command | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | ## no critic (RegularExpressions::ProhibitComplexRegexes) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | 17 | ▶ | if( | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | m{ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | ^ [[:space:]]{0,} [#]{2} | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | [[:space:]]{1,} envassert [[:space:]]{1,} | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | [(] opts: [[:space:]]{0,} (?<opts> .*) [)] | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | [[:space:]]{0,} $ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | }msx | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | 0 | my $opts = _interpret_opts( $LAST_PAREN_MATCH{opts} ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 0 | foreach ( keys %{ $opts } ) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | 0 | $desc{'options'}->{$_} = $opts->{$_}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | } elsif( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | # This is comment row | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | m{ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | ^ [[:space:]]{0,} [#]{1} .* $ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | }msx | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | 5 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | } elsif( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | # This is empty row | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | m{ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | ^ [[:space:]]{0,} $ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | }msx | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | 5 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | } elsif( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | # This is env var description | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | m{ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | ^ (?<name> [^=]{1,}) = (?<value> .*) $ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | }msx | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | $desc{'variables'}->{ $LAST_PAREN_MATCH{name} } = { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | regexp => $LAST_PAREN_MATCH{value} | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | 7 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | 3 | return \%desc; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | # Private subroutines | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | 6 | ▶ | sub _interpret_opts { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_interpret_opts: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | 6 | my ($opts_str) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | 6 | my @opts = split qr{ | |||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | [[:space:]]{0,} [,] [[:space:]]{0,} | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | }msx, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | $opts_str; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | 6 | my %opts; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | 6 | foreach (@opts) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | 11 | my ($key, $val) = split qr/=/msx; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | 11 | $opts{$key} = $val; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | 6 | return \%opts; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | =head1 DEPENDENCIES | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | No external dependencies outside Perl's standard distribution. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | L<Env::Dot> is a "sister" to Env::Assert. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | Read environment variables from a F<.env> file directly into you program. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | There is also script F<envdot> which can turn F<.env> file's content | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | into environment variables for different shells. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | Mikko Koivunalho <mikkoi@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | This software is copyright (c) 2023 by Mikko Koivunalho. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | 6 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||