File Coverage

blib/lib/File/SafeDO.pm
Criterion Covered Total %
statement 34 43 79.0
branch 8 20 40.0
condition 9 27 33.3
subroutine 6 7 85.7
pod 2 3 66.6
total 59 100 59.0


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2              
3             package File::SafeDO;
4 3     3   1806 use strict;
  3         6  
  3         111  
5             #use diagnostics;
6              
7 3     3   14 use vars qw($VERSION @ISA @EXPORT_OK);
  3         6  
  3         456  
8             require Exporter;
9             @ISA = qw(Exporter);
10              
11             $VERSION = do { my @r = (q$Revision: 0.14 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
12              
13             @EXPORT_OK = qw(
14             DO
15             doINCLUDE
16             );
17              
18 3     3   14 use Config;
  3         8  
  3         1529  
19              
20             =head1 NAME
21              
22             File::SafeDO -- safer do file for perl
23              
24             =head1 SYNOPSIS
25              
26             use File::SafeDO qw(
27             DO
28             doINCLUDE
29             );
30              
31             $rv = DO($file,[optional no warnings string])
32              
33             =head1 DESCRIPTION
34              
35             =over 4
36              
37             =item * $rv = DO($file,[optional] "no warnings string");
38              
39             This is a fancy 'do file'. It first checks that the file exists and is
40             readable, then does a 'do file' to pull the variables and subroutines into
41             the current name space. The 'do' is executed with full perl warnings so that
42             syntax and construct errors are reported to STDERR. A string of B
43             warnings> may optionally be specified as a second argument. This is
44             equivalent to saying:
45              
46             no warnings qw(string of no values);
47              
48             See: man perllexwarnings for a full listing of warning names.
49              
50             input: file/path/name,
51             [optional] string of "no" warnings
52             returns: last value in file
53             or undef on error
54             prints warning
55              
56             i.e. DO('myfile','once redefine');
57              
58             This will execute 'myfile' safely and suppress 'once' and 'redefine'
59             warnings to STDERR.
60              
61             =cut
62              
63             sub xDO($;$) {
64 0     0 0 0 my($file,$nowarnings) = @_;
65             return undef unless
66 0 0 0     0 $file &&
      0        
      0        
67             -e $file &&
68             -f $file &&
69             -r $file;
70 0         0 $_ = $Config{perlpath}; # bring perl into scope
71 0 0       0 if ($nowarnings) {
72 0 0       0 return undef if eval q|system($_, '-Mwarnings', "-M-warnings qw($nowarnings)", $file)|;
73             } else {
74 0 0       0 return undef if eval q|system($_, '-w', $file)|;
75             }
76             # poke anonymous subroutine into calling package so vars and subs will import
77 0         0 my $caller = caller;
78             # execute 'do $file;' in calling package
79 0         0 &{eval "package $caller; sub { my \$file = shift; do \$file;};";}($file);
  0         0  
80             }
81              
82             sub DO($;$) {
83 2     2 1 83 my($file,$nowarnings) = @_;
84 2         6 my $caller = caller;
85 2         8 @_ = ($file,$nowarnings,$caller,0);
86 2         12 goto &_doFILE;
87             }
88              
89             =item * $rv = doINCLUDE($file,[optional] "no warnings string");
90              
91             The function is similar to B above with the addition of recursive loads.
92              
93             Function will recursively load a file which returns a hash pointer with the
94             a key of the form:
95            
96             'INCLUDE' => somefile.
97              
98             The file which it loads may contain only HASHs or SUBs. The HASH KEYS will
99             be promoted into the parent hash, augmenting and replacing existing keys
100             already present. Subroutines are simply imported into the name
101             space as is the case with a 'do' or 'require'.
102              
103             =back
104              
105             =cut
106              
107             sub doINCLUDE($;$) {
108 2     2 1 120 my($file,$nowarnings) = @_;
109 2         6 my $caller = caller;
110 2         13 @_ = ($file,$nowarnings,$caller,1);
111 2         16 goto &_doFILE;
112             }
113              
114             sub _doFILE($$$$) {
115 5     5   17 my($file,$nowarnings,$caller,$recurs) = @_;
116             return undef unless
117 5 50 33     270 $file &&
      33        
      33        
118             -e $file &&
119             -f $file &&
120             -r $file;
121 5         2938 $_ = $Config{perlpath}; # bring perl into scope
122 5 100       9090 if ($nowarnings) {
123 3 50       256 return undef if eval q|system($_, '-Mwarnings', "-M-warnings qw($nowarnings)", $file)|;
124             } else {
125 2 50       131 return undef if eval q|system($_, '-w', $file)|;
126             }
127             # poke anonymous subroutine into calling package so vars and subs will import
128             # execute 'do $file;' in calling package
129 5         155 my $rv = &{eval "package $caller; sub { my \$file = shift; do \$file;};";}($file);
  5         1445  
130 5 100 66     217 return $rv unless $recurs &&
      100        
131             UNIVERSAL::isa($rv,'HASH') &&
132             exists $rv->{INCLUDE};
133 1         19 my $rrv = &_doFILE($rv->{INCLUDE},$nowarnings,$caller,1);
134 1 50 33     23 return $rv unless $rrv &&
135             UNIVERSAL::isa($rv,'HASH');
136 1         2 my @keys = keys %{$rrv};
  1         9  
137 1         3 @{$rv}{@keys} = @{$rrv}{@keys};
  1         5  
  1         5  
138 1         22 return $rv;
139             }
140              
141             =head1 DEPENDENCIES
142              
143             none
144              
145             =head1 EXPORT_OK
146              
147             DO
148             doINCLUDE
149              
150             =head1 AUTHOR
151              
152             Michael Robinton, michael@bizsystems.com
153              
154             =head1 COPYRIGHT
155              
156             Copyright 2003 - 2014, Michael Robinton & BizSystems
157             This program is free software; you can redistribute it and/or modify
158             it under the terms of the GNU General Public License as published by
159             the Free Software Foundation; either version 2 of the License, or
160             (at your option) any later version.
161              
162             This program is distributed in the hope that it will be useful,
163             but WITHOUT ANY WARRANTY; without even the implied warranty of
164             MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
165             GNU General Public License for more details.
166              
167             You should have received a copy of the GNU General Public License
168             along with this program; if not, write to the Free Software
169             Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
170              
171             =cut
172              
173             1;