File Coverage

blib/lib/Shell/Tools/Extra.pm
Criterion Covered Total %
statement 43 43 100.0
branch 6 8 75.0
condition 4 6 66.6
subroutine 12 12 100.0
pod n/a
total 65 69 94.2


line stmt bran cond sub pod time code
1             #!perl
2             package Shell::Tools::Extra;
3 3     3   270599 use warnings;
  3         5  
  3         89  
4 3     3   12 use strict;
  3         5  
  3         127  
5              
6             our $VERSION = '0.02';
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 ();
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.02 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   13 use Carp;
  3         3  
  3         762  
48              
49             require Shell::Tools;
50             sub import { ## no critic (RequireArgUnpacking)
51 4     4   47 for (my $i=0;$i<@_;$i++) {
52 6 100 66     42 if ( $_[$i] && $_[$i] eq 'Shell' ) {
53 2         8 _import_Shell((caller)[0], $_[$i+1]);
54 2         5 splice @_, $i, 2; $i--;
  2         5  
55             }
56             }
57 4         16 goto &Shell::Tools::import;
58             }
59              
60              
61             =head2 L
62              
63             use Shell::Tools Shell => [ qw/echo cat/ ];
64             use Shell::Tools Shell => 'who';
65              
66             The argument(s) will be passed through as the arguments to
67             L's import.
68             Note that C must be followed by exactly one argument, either
69             a single scalar or an arrayref.
70              
71             This module is optional: If no C arguments are present in C,
72             this module will not be loaded and it does not need to be installed.
73              
74             =cut
75              
76             sub _import_Shell {
77 2     2   4 my ($destpack, $args) = @_;
78 2 50       5 croak "no arguments for Shell import specified" unless $args;
79 2 50 66     12 croak "arguments for Shell import must be an array ref or a scalar"
80             if ref $args && ref $args ne 'ARRAY';
81 2         9 require IPC::Run3::Shell; # CPAN
82 2         24 IPC::Run3::Shell->VERSION('0.52'); # for import_into support
83 2 100       13 IPC::Run3::Shell->import_into($destpack, ref $args ? @$args : $args);
84 2         124 return;
85             }
86              
87              
88             # now switch to Shell::Tools package so "use" statements will export to there
89             package Shell::Tools; ## no critic (ProhibitMultiplePackages)
90             our @EXPORT;
91             our %EXPORT_TAGS;
92              
93              
94             =head2 L
95              
96             try { die "foo" }
97             catch { warn "caught error: $_\n" } # not $@
98             finally { print "finally" };
99              
100             =cut
101              
102 3     3   21 use constant _EXP_TRY_TINY => qw/try catch finally/;
  3         3  
  3         263  
103 3     3   13 use Try::Tiny _EXP_TRY_TINY; # CPAN
  3         5  
  3         280  
104             push @EXPORT, _EXP_TRY_TINY;
105             $EXPORT_TAGS{"Try::Tiny"} = [_EXP_TRY_TINY];
106              
107              
108             =head2 L
109              
110             my $dir = dir('foo', 'bar'); # Path::Class::Dir object
111             my $file = file('bob', 'file.txt'); # Path::Class::File object
112             # interfaces to File::Spec's tempdir and tempfile
113             my $tempdir = Path::Class::tempdir(CLEANUP=>1); # isa Path::Class::Dir
114             my ($fh,$fn) = $tempdir->tempfile(UNLINK=>1); # $fn is NOT an object
115              
116             (Note that L may not work properly with Perl before v5.8.0.)
117              
118             =cut
119              
120 3     3   13 use constant _EXP_PATH_CLASS => qw/dir file/;
  3         3  
  3         166  
121 3     3   13 use Path::Class _EXP_PATH_CLASS; # CPAN
  3         4  
  3         274  
122             push @EXPORT, _EXP_PATH_CLASS;
123             $EXPORT_TAGS{"Path::Class"} = [_EXP_PATH_CLASS];
124              
125              
126             =head2 L
127              
128             {
129             my $dir = pushd('/tmp');
130             # working directory changed to /tmp
131             }
132             # working directory has reverted to previous
133              
134             =cut
135              
136 3     3   13 use constant _EXP_FILE_PUSHD => qw/pushd/;
  3         4  
  3         144  
137 3     3   12 use File::pushd _EXP_FILE_PUSHD; # CPAN
  3         3  
  3         228  
138             push @EXPORT, _EXP_FILE_PUSHD;
139             $EXPORT_TAGS{"File::pushd"} = [_EXP_FILE_PUSHD];
140              
141              
142             =head2 L
143              
144             my @files = File::Find::Rule->file->name('*.pm')->in(@INC);
145              
146             =cut
147              
148 3     3   44 use File::Find::Rule ();
  3         5  
  3         70  
149              
150              
151             1;
152             __END__