File Coverage

blib/lib/Env/AsYaml.pm
Criterion Covered Total %
statement 46 66 69.7
branch 1 12 8.3
condition 8 15 53.3
subroutine 15 17 88.2
pod 2 2 100.0
total 72 112 64.2


line stmt bran cond sub pod time code
1             package Env::AsYaml;
2             # Last modified: Tue Sep 02 2025 01:10:52 PM -04:00 [EDT]
3             # First created: Sat Aug 09 2025 05:38:14 PM -04:00 [EDT]
4              
5 1     1   267511 use v5.18;
  1         4  
6 1     1   6 use strict;
  1         9  
  1         41  
7 1     1   4832 use utf8;
  1         335  
  1         6  
8 1     1   40 use warnings;
  1         1  
  1         108  
9              
10             =head1 NAME/ABSTRACT
11              
12             Env::AsYaml is intended to be a tool for examination of the environment in
13             which the user is running programs, starting processes or for otherwise
14             troubleshooting the system.
15              
16             =head1 VERSION
17              
18             Version 0.35
19              
20             =cut
21              
22             our $VERSION = '0.35';
23              
24             =head1 SYNOPSIS
25              
26             This module checks the environment it's running in and prints it to STDOUT as
27             YAML. Env vars that are lists (such as C<$PATH>) are formatted in YAML as lists.
28              
29             use Env::AsYaml; # auto-imports 'showPathLists' and 'showScalars'
30              
31             =cut
32              
33 1     1   6 use vars qw( @Wanted @Bare );
  1         2  
  1         361  
34             # ---------------------- ### ---------------------- #
35             BEGIN {
36 2         4 @Wanted = map { push @Bare=> $_; q%@% .$_ } grep {
  2         9  
37 1 50 100 1   18 $_ eq "PERL5LIB"
  21   66     176  
      33        
      66        
38             || $_ eq "PATH"
39             || /^XDG_[A-Z]+_DIRS\z/
40             || ( /^[_A-Z0-9]+PATH\z/ && !/^XDG_.+_PATH\z/ )
41             || /PSModulePath/i # does not work on cygwin, why?
42             } sort keys %ENV;
43              
44 1     1   112 eval "use Env qw/@Wanted/ ;";
  1         5652  
  1         7679  
  1         7  
45             }
46             # ---------------------- ### ---------------------- #
47              
48 1     1   824 use Env::Paths::2yaml;
  1         4  
  1         71  
49 1     1   718 use Env::Scalars::scalars2yaml;
  1         24  
  1         68  
50 1     1   8 use YAML::Any;
  1         3  
  1         34  
51 1     1   1484 use Getopt::Std;
  1         8790  
  1         128  
52             require Exporter;
53             our @ISA = qw(Exporter);
54             our @EXPORT = qw(showPathLists showScalars);
55 1     1   880 use Data::Dump::Color;
  1         15351  
  1         148  
56             $Data::Dump::Color::INDEX = 0;
57             $Data::Dump::Color::COLOR = 'true';
58              
59             =head1 EXPORTS
60              
61             These subroutines are available to import ("@EXPORT_OK").
62             showPathLists showScalars
63              
64             =head1 SUBROUTINES/METHODS
65              
66             =head2 showPathLists
67              
68             Use Env::Paths::2yaml to transmute all env path lists into YAML serialization.
69              
70             =head2
71              
72             =cut
73              
74             sub showPathLists {
75 1     1   12 use Env::Paths::2yaml qw( ToYaml );
  1         2  
  1         182  
76 0 0   0 1   my $dd = defined $_[0] ? 'true' : '';
77             # It's nasty to hard-code it this way but this stuff in my env is just
78             # in the way:
79 0 0 0       @Bare = grep { $_ ne 'ORIGINAL_PATH'
  0            
80             && $_ ne 'AMDRMSDKPATH'
81             && $_ ne 'HOMEPATH' } @Bare;
82              
83 0           my( $accumulator , @all_docs );
84 0           for my $kstr ( @Bare ) {
85 1     1   8 no strict 'refs'; # a symbolic reference below:
  1         3  
  1         244  
86 0           my $seq = ToYaml( $kstr, @{$kstr} );
  0            
87 0           my $yaml_segment = join q[]=> @$seq;
88 0           $accumulator .= qq[\n---\n] . $yaml_segment;
89             }
90 0           print $accumulator;
91              
92             # Load YAML here, to dump the data in color if desired.
93 0           @all_docs = Load( $accumulator );
94              
95 0 0         if ($dd) { # Dump as perl data, in vivid technicolor.
96 0           print qq[\n];
97 0           dd( @all_docs );
98             }
99             }
100              
101             =head2 showScalars
102              
103             Print simple scalar strings present in the environment.
104              
105             =cut
106              
107 1     1   10 use Env::Scalars::scalars2yaml qw( s2yaml );
  1         2  
  1         190  
108             sub showScalars {
109 0     0 1   my $simples = s2yaml();
110 0 0         my $dd = defined $_[0] ? 'true' : '';
111 0           say qq[\n---];
112 0           say for @$simples;
113 0 0         if ($dd) {
114 0           dd( $simples );
115             }
116             }
117              
118             __END__