File Coverage

blib/lib/Shell/Tools/Extra.pm
Criterion Covered Total %
statement 46 46 100.0
branch 6 8 75.0
condition 4 6 66.6
subroutine 13 13 100.0
pod n/a
total 69 73 94.5


line stmt bran cond sub pod time code
1             #!perl
2             package Shell::Tools::Extra;
3 3     3   290877 use warnings;
  3         6  
  3         109  
4 3     3   13 use strict;
  3         4  
  3         156  
5              
6             our $VERSION = '0.04';
7              
8             =head1 Name
9              
10             Shell::Tools::Extra - Perl extension to reduce boilerplate in Perl shell scripts (Extra modules)
11              
12             =head1 Synopsis
13              
14             use Shell::Tools::Extra; # is the same as the following:
15            
16             use Shell::Tools; # turns on warnings and strict and exports many funcs
17             use Try::Tiny qw/try catch finally/;
18             use Path::Class qw/dir file/;
19             use File::pushd 'pushd';
20             use File::Find::Rule 'rule';
21            
22             # and
23             use Shell::Tools::Extra Shell => [ IPC_RUN3_SHELL_ARGS ];
24             # is the same as
25             use IPC::Run3::Shell IPC_RUN3_SHELL_ARGS;
26              
27             =head1 Description
28              
29             This module exports a collection of functions from selected Perl modules
30             from CPAN, in addition to those from L.
31              
32             =head1 Version
33              
34             This document describes version 0.04 of Shell::Tools::Extra.
35              
36             =head1 Exports
37              
38             This module exports the following modules and functions.
39              
40             Like L,
41             each module has an L tag that is the same name as the module.
42              
43             =cut
44              
45             ## no critic (ProhibitConstantPragma)
46              
47 3     3   12 use Carp;
  3         5  
  3         879  
48              
49             require Shell::Tools;
50             sub import { ## no critic (RequireArgUnpacking)
51 4     4   60 for (my $i=0;$i<@_;$i++) {
52 6 100 66     40 if ( $_[$i] && $_[$i] eq 'Shell' ) {
53 2         7 _import_Shell(scalar caller, $_[$i+1]);
54 2         4 splice @_, $i, 2; $i--;
  2         6  
55             }
56             }
57 4         16 goto &Shell::Tools::import;
58             }
59              
60              
61             =head2 L
62              
63             use Shell::Tools::Extra Shell => 'echo';
64             # = use IPC::Run3::Shell 'echo'; # import "echo"
65             use Shell::Tools::Extra Shell => [ qw/cat who/ ];
66             # = use IPC::Run3::Shell qw/cat who/; # import "cat" and "who"
67             use Shell::Tools::Extra Shell => [ [ d => '/bin/date' ] ];
68             # = use IPC::Run3::Shell [ d => '/bin/date' ]; # alias "d" to "date"
69              
70             The word C followed by either a string or an array reference
71             may be placed anywhere in the import list,
72             which will cause the L module
73             to be loaded with those arguments.
74             If no C arguments are present in C,
75             this module will not be loaded and it does not need to be installed.
76              
77             =cut
78              
79             sub _import_Shell {
80 2     2   5 my ($destpack, $args) = @_;
81 2 50       5 croak "no arguments for Shell import specified" unless $args;
82 2 50 66     7 croak "arguments for Shell import must be an array ref or a scalar"
83             if ref $args && ref $args ne 'ARRAY';
84 2         8 require IPC::Run3::Shell; # CPAN
85 2         30 IPC::Run3::Shell->VERSION('0.52'); # for import_into support
86 2 100       14 IPC::Run3::Shell->import_into($destpack, ref $args ? @$args : $args);
87 2         139 return;
88             }
89              
90              
91             # now switch to Shell::Tools package so "use" statements will export to there
92             package Shell::Tools; ## no critic (ProhibitMultiplePackages)
93             our @EXPORT;
94             our %EXPORT_TAGS;
95              
96              
97             =head2 L
98              
99             try { die "foo" }
100             catch { warn "caught error: $_\n" } # not $@
101             finally { print "finally" };
102              
103             =cut
104              
105 3     3   18 use constant _EXP_TRY_TINY => qw/try catch finally/;
  3         3  
  3         278  
106 3     3   17 use Try::Tiny _EXP_TRY_TINY; # CPAN
  3         4  
  3         320  
107             push @EXPORT, _EXP_TRY_TINY;
108             $EXPORT_TAGS{"Try::Tiny"} = [_EXP_TRY_TINY];
109              
110              
111             =head2 L
112              
113             my $dir = dir('foo', 'bar'); # Path::Class::Dir object
114             my $file = file('bob', 'file.txt'); # Path::Class::File object
115             # interfaces to File::Spec's tempdir and tempfile
116             my $tempdir = Path::Class::tempdir(CLEANUP=>1); # isa Path::Class::Dir
117             my ($fh,$fn) = $tempdir->tempfile(UNLINK=>1); # $fn is NOT an object
118              
119             (Note that L may not work properly with Perl before v5.8.0.)
120              
121             =cut
122              
123 3     3   15 use constant _EXP_PATH_CLASS => qw/dir file/;
  3         4  
  3         166  
124 3     3   12 use Path::Class _EXP_PATH_CLASS; # CPAN
  3         5  
  3         264  
125             push @EXPORT, _EXP_PATH_CLASS;
126             $EXPORT_TAGS{"Path::Class"} = [_EXP_PATH_CLASS];
127              
128              
129             =head2 L
130              
131             {
132             my $dir = pushd('/tmp');
133             # working directory changed to /tmp
134             }
135             # working directory has reverted to previous
136              
137             =cut
138              
139 3     3   12 use constant _EXP_FILE_PUSHD => qw/pushd/;
  3         4  
  3         161  
140 3     3   16 use File::pushd _EXP_FILE_PUSHD; # CPAN
  3         3  
  3         237  
141             push @EXPORT, _EXP_FILE_PUSHD;
142             $EXPORT_TAGS{"File::pushd"} = [_EXP_FILE_PUSHD];
143              
144              
145             =head2 L
146              
147             my @files = rule->file->name('*.pm')->in(@INC);
148             my $rule = rule->dir->name(qr/te?mp/i)->start($ENV{HOME});
149             while ( defined( my $tmpdir = $rule->match ) ) {
150             ...
151             }
152              
153             =cut
154              
155 3     3   35 use constant _EXP_FILE_FIND_RULE => qw/rule/;
  3         3  
  3         134  
156 3     3   11 use File::Find::Rule _EXP_FILE_FIND_RULE; # CPAN
  3         3  
  3         25  
157             push @EXPORT, _EXP_FILE_FIND_RULE;
158             $EXPORT_TAGS{"File::Find::Rule"} = [_EXP_FILE_FIND_RULE];
159              
160              
161             1;
162             __END__