| 1 | #!/usr/bin/env perl | ||||||||||
| 2 | ## no critic (ControlStructures::ProhibitPostfixControls) | ||||||||||
| 3 | ## no critic (ValuesAndExpressions::ProhibitConstantPragma) | ||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 2 | ▶ | use open ':std', IO => ':encoding(UTF-8)'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | # ABSTRACT: Ensure that the environment variables match what you need, or abort. | ||||||||||
| 9 | |||||||||||
| 10 | # PODNAME: envassert | ||||||||||
| 11 | |||||||||||
| 12 | 2 | our $VERSION = '0.018'; | |||||||||
| 13 | |||||||||||
| 14 | 2 | ▶ | use English qw( -no_match_vars ); # Avoids regex performance penalty in perl 5.18 and earlier | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 2 | ▶ | use Getopt::Long qw( :config auto_version auto_help ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 2 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 2 | ▶ | use Pod::Usage; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | |||||||||||
| 19 | 2 | ▶ | use Env::Assert::Functions qw( :all ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | |||||||||||
| 21 | 2 | local $OUTPUT_AUTOFLUSH = 1; | |||||||||
| 22 | |||||||||||
| 23 | 2 | ▶ | use constant { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 24 | 2 | YEAR_START => 1900, | |||||||||
| 25 | MONTH_START => 1, | ||||||||||
| 26 | ENV_DESC_FILENAME => '.envdesc', | ||||||||||
| 27 | INDENT => q{ }, | ||||||||||
| 28 | 2 | }; | |||||||||
| 29 | |||||||||||
| 30 | 2 | my $man = 0; | |||||||||
| 31 | 2 | my $break_at_first_error = 0; | |||||||||
| 32 | 2 | my $env_desc_filename = ENV_DESC_FILENAME; | |||||||||
| 33 | 2 | my $exact; | |||||||||
| 34 | 2 | my $stdin = 0; | |||||||||
| 35 | 2 | ▶ | GetOptions( | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | 'man' => \$man, | ||||||||||
| 37 | 'break-at-first-error|b!' => \$break_at_first_error, | ||||||||||
| 38 | 'env-description|e=s' => \$env_desc_filename, | ||||||||||
| 39 | 'exact|x!' => \$exact, | ||||||||||
| 40 | 'stdin!' => \$stdin, | ||||||||||
| 41 | ) or pod2usage(2); | ||||||||||
| 42 | 0 | ▶ | pod2usage( -exitval => 0, -verbose => 2 ) if $man; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | |||||||||||
| 44 | 0 | ▶ | sub main { | ||||||||
|
Subroutine
main: not called CC=9 SCAR=45.0
| |||||||||||
| 45 | |||||||||||
| 46 | 0 | my @env_desc_rows; | |||||||||
| 47 | 0 | ▶ | if ($stdin) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 48 | 0 | @env_desc_rows = <>; | |||||||||
| 49 | } else { | ||||||||||
| 50 | 0 | ▶ | open my $fh, q{<}, $env_desc_filename or croak "Cannot open file '$env_desc_filename'"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | 0 | @env_desc_rows = <$fh>; | |||||||||
| 52 | 0 | ▶ | close $fh or croak "Cannot close file '$env_desc_filename'"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 53 | } | ||||||||||
| 54 | |||||||||||
| 55 | 0 | ▶ | my %desc = file_to_desc( $stdin ? q{STDIN} : $env_desc_filename, @env_desc_rows ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | 0 | my %parameters; | |||||||||
| 57 | 0 | ▶ | $parameters{'break_at_first_error'} = $break_at_first_error | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | if $break_at_first_error; | ||||||||||
| 59 | 0 | ▶ | $desc{'opts'}->{OPTION_ENV_EXACT} = $exact | ||||||||
|
Branch: 1 branch
| |||||||||||
| 60 | if defined $exact; | ||||||||||
| 61 | 0 | my $r = assert( \%ENV, \%desc, \%parameters ); | |||||||||
| 62 | 0 | ▶ | if ( !$r->{'success'} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | 0 | ▶ | print {*STDOUT} report_errors( $r->{'errors'} ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | or croak 'Cannot print errors to STDOUT'; | ||||||||||
| 65 | 0 | return 1; | |||||||||
| 66 | } | ||||||||||
| 67 | 0 | return 0; | |||||||||
| 68 | } | ||||||||||
| 69 | |||||||||||
| 70 | 0 | exit main(@ARGV); | |||||||||
| 71 | |||||||||||
| 72 | __END__ | ||||||||||