| 1 | package App::PerlShell::Plugin::File; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | ######################################################## | ||||||||||||||||||||||||||||
| 4 | # AUTHOR = Michael Vincent | ||||||||||||||||||||||||||||
| 5 | # www.VinsWorld.com | ||||||||||||||||||||||||||||
| 6 | ######################################################## | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Pod::Usage; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Pod::Find qw( pod_where ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use Exporter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 1 | our @EXPORT = qw( | |||||||||||||||||||||||||||
| 16 | File | ||||||||||||||||||||||||||||
| 17 | file_eval | ||||||||||||||||||||||||||||
| 18 | file_read | ||||||||||||||||||||||||||||
| 19 | ); | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 1 | our @ISA = qw ( Exporter ); | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 0 | ▶ | sub File { | ||||||||||||||||||||||||||
|
Subroutine
File: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 24 | 0 | pod2usage( | |||||||||||||||||||||||||||
| 25 | -verbose => 2, | ||||||||||||||||||||||||||||
| 26 | -exitval => "NOEXIT", | ||||||||||||||||||||||||||||
| 27 | -input => pod_where( {-inc => 1}, __PACKAGE__ ) | ||||||||||||||||||||||||||||
| 28 | ); | ||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | ######################################################## | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 0 | ▶ | sub file_eval { | ||||||||||||||||||||||||||
|
Subroutine
file_eval: not called CC=26 SCAR=63.9
Pod
documented
| |||||||||||||||||||||||||||||
| 34 | 0 | ▶ | if ( $#_ < 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 0 | _help("COMMANDS/file_eval - run Perl commands in file"); | |||||||||||||||||||||||||||
| 36 | 0 | return; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 0 | my %params = ( | |||||||||||||||||||||||||||
| 40 | argv => undef, | ||||||||||||||||||||||||||||
| 41 | line => 0, | ||||||||||||||||||||||||||||
| 42 | verbose => 0 | ||||||||||||||||||||||||||||
| 43 | ); | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 0 | ▶ | if ( @_ == 1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 0 | ( $params{file} ) = @_; | |||||||||||||||||||||||||||
| 47 | } else { | ||||||||||||||||||||||||||||
| 48 | 0 | ▶ | if ( ( @_ % 2 ) == 1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 0 | $params{file} = shift; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | 0 | my %args = @_; | |||||||||||||||||||||||||||
| 52 | 0 | for ( keys(%args) ) { | |||||||||||||||||||||||||||
| 53 | 0 | ▶ | if (/^-?file$/i) { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 54 | 0 | $params{file} = $args{$_}; | |||||||||||||||||||||||||||
| 55 | } elsif (/^-?argv$/i) { | ||||||||||||||||||||||||||||
| 56 | $params{argv} | ||||||||||||||||||||||||||||
| 57 | 0 | = "\@ARGV = qw ( $args{$_} ); # ADDED argv => ...\n"; | |||||||||||||||||||||||||||
| 58 | } elsif (/^-?line$/i) { | ||||||||||||||||||||||||||||
| 59 | 0 | $params{line} = 1; | |||||||||||||||||||||||||||
| 60 | } elsif (/^-?verbose$/i) { | ||||||||||||||||||||||||||||
| 61 | 0 | $params{verbose} = 1; | |||||||||||||||||||||||||||
| 62 | } else { | ||||||||||||||||||||||||||||
| 63 | 0 | die("Unknown parameter: `$_'"); | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 0 | ▶ | if ( not defined $params{file} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 0 | die("No file provided"); | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 0 | my @rets; | |||||||||||||||||||||||||||
| 73 | 0 | my $retType = wantarray; | |||||||||||||||||||||||||||
| 74 | 0 | ▶ | if ($retType) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 0 | $params{line} = 1; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 0 | ▶ | if ( -e $params{file} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 0 | open( my $IN, '<', $params{file} ); | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 1 | ▶ | no strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 82 | 1 | ▶ | use strict 'subs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 0 | ▶ | if ( $params{line} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 0 | ▶ | while (<$IN>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 0 | ▶ | print "$_" if ( $params{verbose} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 0 | ▶ | if ( not defined $retType ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | # skip blank lines and #comments | ||||||||||||||||||||||||||||
| 91 | 0 | ▶ | next if ( ( $_ =~ /^[\n\r]+$/ ) or ( $_ =~ /^\s*#/ ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$_ =~ /^[\n\r]+$/ or $_ =~ /^\s*#/
MC/DC0%0/2
$_ =~ /^[\n\r]+$/ or $_ =~ /^\s*#/
$_ =~ /^[\n\r]+$/
$_ =~ /^\s*#/
Truth table0%0/3
A or B
A= $_ =~ /^[\n\r]+$/B= $_ =~ /^\s*#/
| |||||||||||||||||||||||||||||
| 92 | 0 | chomp $_; | |||||||||||||||||||||||||||
| 93 | 0 | eval $_; | |||||||||||||||||||||||||||
| 94 | 0 | ▶ | warn $@ if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | } else { | ||||||||||||||||||||||||||||
| 96 | 0 | push @rets, $_; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | 0 | close $IN; | |||||||||||||||||||||||||||
| 100 | 0 | ▶ | if ($retType) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 0 | return @rets; | |||||||||||||||||||||||||||
| 102 | } else { | ||||||||||||||||||||||||||||
| 103 | 0 | my $ret = join "", @rets; | |||||||||||||||||||||||||||
| 104 | 0 | return $ret; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | } else { | ||||||||||||||||||||||||||||
| 107 | 0 | my $fullfile; | |||||||||||||||||||||||||||
| 108 | 0 | ▶ | if ( defined $params{argv} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 0 | $fullfile = $params{argv}; | |||||||||||||||||||||||||||
| 110 | 0 | ▶ | print $fullfile if ( $params{verbose} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | 0 | ▶ | while (<$IN>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 0 | ▶ | print "$_" if ( $params{verbose} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 0 | $fullfile .= $_; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 0 | ▶ | if ( not defined wantarray ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 0 | eval $fullfile; | |||||||||||||||||||||||||||
| 119 | 0 | ▶ | warn $@ if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | } else { | ||||||||||||||||||||||||||||
| 121 | 0 | return $fullfile; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | 0 | close($IN); | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | } else { | ||||||||||||||||||||||||||||
| 127 | 0 | die("Cannot find file - `$params{file}'"); | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 0 | ▶ | sub file_read { | ||||||||||||||||||||||||||
|
Subroutine
file_read: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 132 | 0 | ▶ | if ( $#_ < 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | 0 | _help("COMMANDS/file_read - read contents of a file"); | |||||||||||||||||||||||||||
| 134 | 0 | return; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 0 | my ($file) = @_; | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 0 | ▶ | if ( not defined $file ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | 0 | print "file required\n"; | |||||||||||||||||||||||||||
| 141 | 0 | return; | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 0 | ▶ | open my $fh, "<", $file | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | or die "$!"; | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 0 | my @rets = <$fh>; | |||||||||||||||||||||||||||
| 148 | 0 | close $fh; | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 0 | my $retType = wantarray; | |||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 0 | ▶ | if ( not defined $retType ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 153 | 0 | print @rets; | |||||||||||||||||||||||||||
| 154 | } elsif ($retType) { | ||||||||||||||||||||||||||||
| 155 | 0 | return @rets; | |||||||||||||||||||||||||||
| 156 | } else { | ||||||||||||||||||||||||||||
| 157 | 0 | my $ret = join "", @rets; | |||||||||||||||||||||||||||
| 158 | 0 | return $ret; | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 0 | ▶ | sub _help { | ||||||||||||||||||||||||||
|
Subroutine
_help: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 163 | 0 | my ($section) = @_; | |||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 0 | pod2usage( | |||||||||||||||||||||||||||
| 166 | -verbose => 99, | ||||||||||||||||||||||||||||
| 167 | -exitval => "NOEXIT", | ||||||||||||||||||||||||||||
| 168 | -sections => $section, | ||||||||||||||||||||||||||||
| 169 | -input => pod_where( {-inc => 1}, __PACKAGE__ ) | ||||||||||||||||||||||||||||
| 170 | ); | ||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 1 | 1; | |||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | __END__ | ||||||||||||||||||||||||||||