| 1 | package Backticks; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Filter::Simple; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use File::Temp qw(tempfile); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Class::ISA; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use IPC::Open3; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use overload '""' => \&stdout; # Object stringifies to command's stdout | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | # Always report errors from a context outside of this package | ||||||||||||||||||||||||||||
| 16 | 1 | $Carp::Internal{ (__PACKAGE__) }++; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | =head1 NAME | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | Backticks - Use `backticks` like objects! | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | =cut | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 1 | our $VERSION = '1.0.9'; | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | This module turns backticks into full objects which you can | ||||||||||||||||||||||||||||
| 29 | query in interesting ways. | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | use Backticks; | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | my $results = `ls -a /`; # Assign a Backticks object to $results | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | print $results->stdout; # Get the command's STDOUT | ||||||||||||||||||||||||||||
| 36 | print $results->stderr; # Get the command's STDERR | ||||||||||||||||||||||||||||
| 37 | print $results->merged; # Get STDOUT and STDERR together | ||||||||||||||||||||||||||||
| 38 | print $results->success; # Will be true when command exited clean | ||||||||||||||||||||||||||||
| 39 | print $results; # Get the command's STDOUT... the object | ||||||||||||||||||||||||||||
| 40 | # stringifies to the command's output | ||||||||||||||||||||||||||||
| 41 | # so you can use it most places you | ||||||||||||||||||||||||||||
| 42 | # use normal backticks | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | You can have failed commands automatically die your perl script | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | $Backticks::autodie = 1; | ||||||||||||||||||||||||||||
| 47 | `perl -e 'print STDERR "OUCH!\n"; exit 1'`; | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | Which dies with the following message: | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | Error executing `perl -e 'warn "OUCH!\n"; exit 1'`: | ||||||||||||||||||||||||||||
| 52 | Failed with non-zero exit code 1 | ||||||||||||||||||||||||||||
| 53 | Error output: | ||||||||||||||||||||||||||||
| 54 | OUCH! | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | You can automatically chomp output: | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | $Backticks::chomped = 1; | ||||||||||||||||||||||||||||
| 59 | my $chomped = `perl -e "print qq{Hello\n}"`; | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | You can even access parameters instantly in object mode by calling methods | ||||||||||||||||||||||||||||
| 62 | immediately after the backticks! | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | say `echo foo`->stdout; # Shows 'foo' | ||||||||||||||||||||||||||||
| 65 | say `perl -e "warn 'Hello!'"`->stderr; # Shows 'Hello!' | ||||||||||||||||||||||||||||
| 66 | say `perl -e "exit 1"`->exitcode; # Shows '1' | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | You can also use a perl object-oriented interface instead of using the | ||||||||||||||||||||||||||||
| 69 | `backticks` to create objects, the following command is the same as the first | ||||||||||||||||||||||||||||
| 70 | one above: | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | my $results = Backticks->run("ls -la /"); | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | Alternately, you can create a command and run it later: | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | my $command = Backticks->new("ls -la /"); | ||||||||||||||||||||||||||||
| 77 | # ... do some stuff | ||||||||||||||||||||||||||||
| 78 | $command->run(); | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | Creating commands as an object affords you the opportunity to override | ||||||||||||||||||||||||||||
| 81 | Backticks package settings, by passing them as hash-style params: | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | $Backticks::chomped = 0; | ||||||||||||||||||||||||||||
| 84 | my $chomped_out = Backticks->run( | ||||||||||||||||||||||||||||
| 85 | 'echo "Hello there!"', | ||||||||||||||||||||||||||||
| 86 | 'chomped' => 1, | ||||||||||||||||||||||||||||
| 87 | ); | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | =head1 PACKAGE VARIABLES | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | =head2 $Backticks::autodie | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | If set to 1, then any command which does not have a true success() will cause | ||||||||||||||||||||||||||||
| 94 | the Perl process to die. Defaults to 0. | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | This setting was the original onus for this module. By setting autodie you can | ||||||||||||||||||||||||||||
| 97 | change a script which as a bunch of unchecked system calls in backticks to | ||||||||||||||||||||||||||||
| 98 | having the results all checked using only two lines of code. | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | =head2 $Backticks::chomped | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | If set to 1, then STDOUT and STDERR will remove a trailing newline from the | ||||||||||||||||||||||||||||
| 103 | captured contents, if present. Defaults to 0. | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | It's very rare when you get output from a command and you don't want its | ||||||||||||||||||||||||||||
| 106 | output chomped, or at least it's rare when chomping will cause a problem. | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =head2 $Backticks::debug | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | If set to 1, then additional debugging information will be output to STDERR. | ||||||||||||||||||||||||||||
| 111 | Defaults to 0. | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | If you are running deployment scripts in which the output of every command | ||||||||||||||||||||||||||||
| 114 | needs to be logged, this can be a handy way of showing everything about each | ||||||||||||||||||||||||||||
| 115 | command which was run. | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | =cut | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | # Default values for all object fields | ||||||||||||||||||||||||||||
| 120 | 1 | my %field_defaults = ( | |||||||||||||||||||||||||||
| 121 | 'command' => '', | ||||||||||||||||||||||||||||
| 122 | 'error' => '', | ||||||||||||||||||||||||||||
| 123 | 'stdout' => '', | ||||||||||||||||||||||||||||
| 124 | 'stderr' => '', | ||||||||||||||||||||||||||||
| 125 | 'merged' => '', | ||||||||||||||||||||||||||||
| 126 | 'returncode' => 0, | ||||||||||||||||||||||||||||
| 127 | 'debug' => 0, | ||||||||||||||||||||||||||||
| 128 | 'autodie' => 0, | ||||||||||||||||||||||||||||
| 129 | 'chomped' => 0, | ||||||||||||||||||||||||||||
| 130 | ); | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | # These object fields are settable | ||||||||||||||||||||||||||||
| 133 | 1 | my %field_is_settable = map { $_ => 1 } qw(command debug autodie chomped); | |||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | # These settable object fields cause the object to be reset when they're set | ||||||||||||||||||||||||||||
| 136 | 1 | my %field_causes_reset = map { $_ => 1 } qw(command); | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | # These object fields are removed when the ->reset method is called | ||||||||||||||||||||||||||||
| 139 | 1 | my %field_does_reset = map { $_ => 1 } qw(error stdout stderr returncode); | |||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | # These object fields default to package variables of the same name | ||||||||||||||||||||||||||||
| 142 | 1 | my %field_has_package_var = map { $_ => 1 } qw(debug autodie chomped); | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | # Implement the source filter in Filter::Simple | ||||||||||||||||||||||||||||
| 145 | 58 | ▶ | FILTER_ONLY quotelike => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 146 | 58 | s{^`(.*?)`$} | |||||||||||||||||||||||||||
| 147 | { | ||||||||||||||||||||||||||||
| 148 | 4 | my $cmd = $1; | |||||||||||||||||||||||||||
| 149 | 4 | $cmd =~ s|\\|\\\\|gs; | |||||||||||||||||||||||||||
| 150 | 4 | $cmd =~ s|"|\\"|gs; | |||||||||||||||||||||||||||
| 151 | 4 | "Backticks->run(\"$cmd\")"; | |||||||||||||||||||||||||||
| 152 | }egsx; | ||||||||||||||||||||||||||||
| 153 | }, | ||||||||||||||||||||||||||||
| 154 | 1 | ▶ | all => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=9.2
| |||||||||||||||||||||||||||||
| 155 | # The variable $Backticks::filter_debug indicates that we | ||||||||||||||||||||||||||||
| 156 | # should print the input source lines as the appear after processing | ||||||||||||||||||||||||||||
| 157 | $Backticks::filter_debug | ||||||||||||||||||||||||||||
| 158 | 0 | ▶ | && warn join '', map {"Backticks: $_\n"} split /\n/, $_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | 1 | }; | |||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | # Determine if we're being called as a valid class or instance method | ||||||||||||||||||||||||||||
| 162 | # Return a 1 if we're a class method, a 0 if we're an instance method, | ||||||||||||||||||||||||||||
| 163 | # or if neither then croak complaining that it's a problem | ||||||||||||||||||||||||||||
| 164 | 184 | ▶ | sub _class_method { | ||||||||||||||||||||||||||
|
Subroutine
_class_method: called CC=6 SCAR=20.3
| |||||||||||||||||||||||||||||
| 165 | 184 | my $source = $_[0]; | |||||||||||||||||||||||||||
| 166 | 184 | ▶ | if ( blessed $source ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 167 | 174 | ▶ | return 0 if $source->isa('Backticks'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | ▶ | elsif ( defined $source && not ref $source ) { | |||||||||||||||||||||||||||
|
Condition33%1/3
defined $source and not ref $source
MC/DC0%0/2
defined $source and not ref $source
defined $source
not ref $source
Truth table33%1/3
A and not(B)
A= defined $sourceB= not ref $source
| |||||||||||||||||||||||||||||
| 170 | # Since we're checking through Class::ISA, this should work for | ||||||||||||||||||||||||||||
| 171 | # subclasses of this module (if we ever have any) | ||||||||||||||||||||||||||||
| 172 | return 1 | ||||||||||||||||||||||||||||
| 173 | 10 | ▶ | if scalar( grep { $_ eq 'Backticks' } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | Class::ISA::self_and_super_path($source) ); | ||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | 0 | croak "Must be called as a class or instance method"; | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | # Get the instance object (if called as an instance method) or the last run's | ||||||||||||||||||||||||||||
| 180 | # object (if called as a class method) | ||||||||||||||||||||||||||||
| 181 | 174 | ▶ | sub _self { | ||||||||||||||||||||||||||
|
Subroutine
_self: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 182 | 174 | ▶ | if ( _class_method(@_) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | 1 | ▶ | defined($Backticks::last_run) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | || croak "No previous Backticks command was run"; | ||||||||||||||||||||||||||||
| 185 | 1 | return $Backticks::last_run; | |||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | 173 | return $_[0]; | |||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | # Generic accessor to get the field for the current object (if called | ||||||||||||||||||||||||||||
| 191 | # as an instance method) or the last run's object (if called as a class | ||||||||||||||||||||||||||||
| 192 | # method) | ||||||||||||||||||||||||||||
| 193 | 115 | ▶ | sub _get { | ||||||||||||||||||||||||||
|
Subroutine
_get: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | # Resolve the object being operated upon (class or instance) | ||||||||||||||||||||||||||||
| 196 | 115 | my $self = _self( shift @_ ); | |||||||||||||||||||||||||||
| 197 | 115 | my $field = shift @_; # The field being operated upon for this object | |||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | 115 | ▶ | exists( $field_defaults{$field} ) || croak "Unrecognized field '$field'"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | # Firstly, try to get the value from the object | ||||||||||||||||||||||||||||
| 202 | 115 | ▶ | return $self->{$field} if defined( $self->{$field} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | # If not found in the object, then get the value from the package var | ||||||||||||||||||||||||||||
| 205 | 32 | ▶ | if ( $field_has_package_var{$field} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 206 | 1 | ▶ | my $pkg_var = eval { no strict 'refs'; ${ 'Backticks::' . $field } }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 207 | 19 | ▶ | return $pkg_var if defined( $pkg_var ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | # Otherwise return the default value for the field | ||||||||||||||||||||||||||||
| 211 | 31 | return $field_defaults{$field}; | |||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 6 | ▶ | sub _set { | ||||||||||||||||||||||||||
|
Subroutine
_set: called CC=5 SCAR=16.5
| |||||||||||||||||||||||||||||
| 215 | # Resolve the object being operated upon (class or instance) | ||||||||||||||||||||||||||||
| 216 | 6 | my $self = _self( shift @_ ); | |||||||||||||||||||||||||||
| 217 | 6 | my $field = shift @_; # The field being operated upon for this object | |||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | 6 | ▶ | exists( $field_defaults{$field} ) || croak "Unrecognized field '$field'"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | 6 | ▶ | if ( scalar @_ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 222 | croak "Field '$field' cannot be set." | ||||||||||||||||||||||||||||
| 223 | 6 | ▶ | unless $field_is_settable{$field}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 224 | 6 | $self->{$field} = shift @_; | |||||||||||||||||||||||||||
| 225 | 6 | ▶ | $self->reset if $field_causes_reset{$field}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | =head1 CLASS METHODS | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | =head2 Backticks->new( 'command', [ %params ] ) | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | Creates a new Backticks object but does not run it yet. %params may contain | ||||||||||||||||||||||||||||
| 234 | boolean values for this instance's 'debug', 'autodie' and 'chomped' settings. | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | =cut | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | 5 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | 5 | ▶ | _class_method(@_) || croak "Must be called as a class method!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 241 | 5 | my $self = bless {}, shift @_; | |||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | # Set the command | ||||||||||||||||||||||||||||
| 244 | 5 | $self->_set( 'command', shift @_ ); | |||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | # Set all of the fields passed into ->new | ||||||||||||||||||||||||||||
| 247 | 5 | my %params = @_; | |||||||||||||||||||||||||||
| 248 | 5 | $self->_set( $_, $params{$_} ) foreach keys %params; | |||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | 5 | return $self; | |||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | =head2 Backticks->run( 'command', [ %params ] ) | ||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | Behaves exactly like Backticks->new(...), but after the object is created it | ||||||||||||||||||||||||||||
| 256 | immediately runs the command before returning the object. | ||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | =head2 `command` | ||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | This is a source filter alias for: | ||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | Backticks->run( 'command' ) | ||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | It will create a new Backticks object, run the command, and return the object | ||||||||||||||||||||||||||||
| 265 | complete with results. Since Backticks objects stringify to the STDOUT of the | ||||||||||||||||||||||||||||
| 266 | command which was run, the default behavior is very similar to Perl's normal | ||||||||||||||||||||||||||||
| 267 | backticks. | ||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | =head1 OBJECT METHODS | ||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | =head2 $obj->run() | ||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | Runs (or if the command has already been run, re-runs) the $obj's command, | ||||||||||||||||||||||||||||
| 274 | and returns the object. Note this is the only object method that can't be | ||||||||||||||||||||||||||||
| 275 | called in class context (Backticks->run) to have it work on the last executed | ||||||||||||||||||||||||||||
| 276 | command as described in the "Accessing the Last Run" secion below. If you | ||||||||||||||||||||||||||||
| 277 | need to re-run the last command, use Backticks->rerun instead. | ||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | =cut | ||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | 5 | ▶ | sub run { | ||||||||||||||||||||||||||
|
Subroutine
run: called CC=20 SCAR=31.7
Pod
documented
| |||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | # Get a new object if called as a class method or the | ||||||||||||||||||||||||||||
| 284 | # referenced object if called as an instance method | ||||||||||||||||||||||||||||
| 285 | 5 | ▶ | my $self = _class_method(@_) ? new(@_) : $_[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | 5 | $self->reset; | |||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | 5 | $self->_debug_warn( "Executing command `" . $self->command . "`:" ); | |||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | # Run in an eval to catch any perl errors | ||||||||||||||||||||||||||||
| 292 | 5 | eval { | |||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | 5 | local $/ = "\n"; | |||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | # Open the command via open3, specifying IN/OUT/ERR streams | ||||||||||||||||||||||||||||
| 297 | 5 | ▶ | my $pid = open3( \*P_STDIN, \*P_STDOUT, \*P_STDERR, $self->command ) | ||||||||||||||||||||||||||
|
Condition50%1/2
open3(\*P_STDIN, \*P_STDOUT, \*P_STDERR, $self->command) || die($!)
MC/DC0%0/1
open3(\*P_STDIN, \*P_STDOUT, \*P_STDERR, $self->command) || die($!)
open3(\*P_STDIN, \*P_STDOUT, \*P_STDERR, $self->command)
Truth table50%1/2
A
A= open3(\*P_STDIN, \*P_STDOUT, \*P_STDERR, $self->command)
| |||||||||||||||||||||||||||||
| 298 | || die $!; | ||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | 4 | close P_STDIN; # Close the command's STDIN | |||||||||||||||||||||||||||
| 301 | 4 | while (1) { | |||||||||||||||||||||||||||
| 302 | 5 | ▶ | if ( not eof P_STDOUT ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 303 | 3 | $self->{'stdout'} .= my $out = <P_STDOUT>; | |||||||||||||||||||||||||||
| 304 | 3 | $self->{'merged'} .= $out; | |||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||
| 306 | 5 | ▶ | if ( not eof P_STDERR ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 307 | 4 | $self->{'stderr'} .= my $err = <P_STDERR>; | |||||||||||||||||||||||||||
| 308 | 4 | $self->{'merged'} .= $err; | |||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||
| 310 | 5 | ▶ | last if eof(P_STDOUT) && eof(P_STDERR); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
eof P_STDOUT and eof P_STDERR
MC/DC50%1/2
eof P_STDOUT and eof P_STDERR
eof P_STDOUT
eof P_STDERR
Truth table66%2/3
A and B
A= eof P_STDOUTB= eof P_STDERR
| |||||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | 4 | ▶ | waitpid( $pid, 0 ) || die $!; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | 4 | ▶ | if ($?) { $self->{'returncode'} = $? } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | }; | ||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||
| 319 | 5 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 320 | # If $@ was set then perl had a problem running the command | ||||||||||||||||||||||||||||
| 321 | 1 | $self->_add_error($@); | |||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||
| 323 | elsif ( $self->returncode == -1 ) { | ||||||||||||||||||||||||||||
| 324 | # If we got a return code of -1 then we weren't able to run the | ||||||||||||||||||||||||||||
| 325 | # command (the most common cause of this is the command didn't exist | ||||||||||||||||||||||||||||
| 326 | # or we didn't have permissions to run it) | ||||||||||||||||||||||||||||
| 327 | 0 | $self->_add_error("Failed to execute: $!"); | |||||||||||||||||||||||||||
| 328 | } | ||||||||||||||||||||||||||||
| 329 | elsif ( $self->signal ) { | ||||||||||||||||||||||||||||
| 330 | # If we have a non-zero signal then the command went askew | ||||||||||||||||||||||||||||
| 331 | 0 | my $err = "Died with signal " . $self->signal; | |||||||||||||||||||||||||||
| 332 | 0 | ▶ | if ( $self->coredump ) { $err .= " with coredump"; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 333 | 0 | $self->_add_error($err); | |||||||||||||||||||||||||||
| 334 | } | ||||||||||||||||||||||||||||
| 335 | elsif ( $self->exitcode ) { | ||||||||||||||||||||||||||||
| 336 | # If we have a non-zero exit code then the command went askew | ||||||||||||||||||||||||||||
| 337 | 3 | $self->_add_error( | |||||||||||||||||||||||||||
| 338 | "Failed with non-zero exit code " . $self->exitcode ); | ||||||||||||||||||||||||||||
| 339 | } | ||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | # Perform a chomp if requested | ||||||||||||||||||||||||||||
| 342 | 5 | ▶ | if ( $self->chomped ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 343 | # Defined checks are here so we don't auto-vivify the fields... | ||||||||||||||||||||||||||||
| 344 | # We don't actually use chomp here because on Win32, chomp doesn't | ||||||||||||||||||||||||||||
| 345 | # nix the carriage return. | ||||||||||||||||||||||||||||
| 346 | 1 | ▶ | defined( $self->{'stdout'} ) && $self->{'stdout'} =~ s/\r?\n$//; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 347 | 1 | ▶ | defined( $self->{'stderr'} ) && $self->{'stderr'} =~ s/\r?\n$//; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 348 | 1 | ▶ | defined( $self->{'merged'} ) && $self->{'merged'} =~ s/\r?\n$//; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 349 | } | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | # Print debugging information | ||||||||||||||||||||||||||||
| 352 | 5 | $self->_debug_warn( $self->as_table ); | |||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | # If we are expected to die unless we have a success, then do so... | ||||||||||||||||||||||||||||
| 355 | 5 | ▶ | if ( $self->autodie && not $self->success ) { croak $self->error_verbose } | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->autodie and not $self->success
MC/DC50%1/2
$self->autodie and not $self->success
$self->autodie
not $self->success
Truth table66%2/3
A and not(B)
A= $self->autodieB= not $self->success
| |||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | # Make it so we can get at the last command run through class methods | ||||||||||||||||||||||||||||
| 358 | 4 | $Backticks::last_run = $self; | |||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | 4 | return $self; | |||||||||||||||||||||||||||
| 361 | } | ||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | =head2 $obj->rerun() | ||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | Re-runs $obj's command, and returns the object. | ||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | =cut | ||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||
| 369 | 0 | ▶ | sub rerun { _self(@_)->run } | ||||||||||||||||||||||||||
|
Subroutine
rerun: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | =head2 $obj->reset() | ||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||
| 373 | Resets the object back to a state as if the command had never been run | ||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||
| 375 | =cut | ||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | 10 | ▶ | sub reset { | ||||||||||||||||||||||||||
|
Subroutine
reset: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 378 | 10 | my $self = _self(@_); | |||||||||||||||||||||||||||
| 379 | 10 | delete $self->{$_} foreach grep { $field_does_reset{$_} } keys %$self; | |||||||||||||||||||||||||||
| 380 | } | ||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||
| 382 | =head2 $obj->as_table() | ||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||
| 384 | Returns a summary text table about the command. | ||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||
| 386 | =cut | ||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||
| 388 | 5 | ▶ | sub as_table { | ||||||||||||||||||||||||||
|
Subroutine
as_table: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 389 | 5 | my $self = _self(@_); | |||||||||||||||||||||||||||
| 390 | 5 | my $out = ''; | |||||||||||||||||||||||||||
| 391 | 5 | _tbl( \$out, 'Command', $self->command); | |||||||||||||||||||||||||||
| 392 | 5 | ▶ | $self->error && _tbl( \$out, 'Error', $self->error ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 393 | 5 | ▶ | $self->stdout && _tbl( \$out, 'STDOUT', $self->stdout ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 394 | 5 | ▶ | $self->stderr && _tbl( \$out, 'STDERR', $self->stderr ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 395 | 5 | ▶ | $self->merged && _tbl( \$out, 'Merged', $self->merged ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 396 | 5 | ▶ | if ( $self->returncode ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 397 | 3 | _tbl( \$out, 'Return Code', $self->returncode ); | |||||||||||||||||||||||||||
| 398 | 3 | _tbl( \$out, 'Exit Code', $self->exitcode ); | |||||||||||||||||||||||||||
| 399 | 3 | _tbl( \$out, 'Signal', $self->signal ); | |||||||||||||||||||||||||||
| 400 | 3 | _tbl( \$out, 'Coredump', $self->coredump ); | |||||||||||||||||||||||||||
| 401 | } | ||||||||||||||||||||||||||||
| 402 | 5 | return $out; | |||||||||||||||||||||||||||
| 403 | } | ||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | # Adds rows to the provided string ref for as_table above | ||||||||||||||||||||||||||||
| 406 | 30 | ▶ | sub _tbl { | ||||||||||||||||||||||||||
|
Subroutine
_tbl: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 407 | 30 | my $out = shift; # String reference to add the row to | |||||||||||||||||||||||||||
| 408 | 30 | my $name = shift; # Name of the field being displayed | |||||||||||||||||||||||||||
| 409 | 30 | my $val = shift; # Value of the field being displayed | |||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||
| 411 | # Show undefined values as the string "undef" | ||||||||||||||||||||||||||||
| 412 | 0 | ▶ | if ( not defined $val ) { $val = 'undef'; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||
| 414 | # Indent multi-line values | ||||||||||||||||||||||||||||
| 415 | 30 | $val = join( "\n" . ( ' ' x 14 ), split "\n", $val ); | |||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||
| 417 | # Append the row | ||||||||||||||||||||||||||||
| 418 | 30 | $$out .= sprintf "%-11s : %s\n", $name, $val; | |||||||||||||||||||||||||||
| 419 | } | ||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||
| 421 | =head2 $obj->command() | ||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||
| 423 | Returns a string containing the command that this object is/was configured to | ||||||||||||||||||||||||||||
| 424 | run. | ||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||
| 426 | =head2 $obj->stdout(), $obj->stderr(), $obj->merged() | ||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||
| 428 | Returns a string containing the contents of STDOUT or STDERR of the command | ||||||||||||||||||||||||||||
| 429 | which was run. If chomped is true, then this value will lack the trailing | ||||||||||||||||||||||||||||
| 430 | newline if one happened in the captured output. Merged is the combined output | ||||||||||||||||||||||||||||
| 431 | of STDOUT and STDERR. | ||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||
| 433 | =head2 $obj->returncode(), $obj->exitcode(), $obj->coredump(), $obj->signal() | ||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||
| 435 | Returns an integer, indicating a $?-based value at the time the command was | ||||||||||||||||||||||||||||
| 436 | run: | ||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||
| 438 | =over 4 | ||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||
| 440 | =item returncode = $? | ||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||
| 442 | =item exitcode = $? >> 8 | ||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||
| 444 | =item coredump = $? & 128 | ||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||
| 446 | =item signal = $? & 127 | ||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||
| 448 | =back | ||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||
| 450 | =head2 $obj->error(), $obj->error_verbose() | ||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||
| 452 | Returns a string containing a description of any errors encountered while | ||||||||||||||||||||||||||||
| 453 | running the command. In the case of error_verbose, it will also contain the | ||||||||||||||||||||||||||||
| 454 | command which was run and STDERR's output. | ||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||
| 456 | =cut | ||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||
| 458 | 16 | ▶ | sub command { _get( shift(@_), 'command' ) } | ||||||||||||||||||||||||||
|
Subroutine
command: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 459 | 14 | ▶ | sub error { _get( shift(@_), 'error' ) } | ||||||||||||||||||||||||||
|
Subroutine
error: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 460 | 33 | ▶ | sub returncode { _get( shift(@_), 'returncode' ) } | ||||||||||||||||||||||||||
|
Subroutine
returncode: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 461 | 11 | ▶ | sub stdout { _get( shift(@_), 'stdout' ) } | ||||||||||||||||||||||||||
|
Subroutine
stdout: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 462 | 11 | ▶ | sub stderr { _get( shift(@_), 'stderr' ) } | ||||||||||||||||||||||||||
|
Subroutine
stderr: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 463 | 10 | ▶ | sub merged { _get( shift(@_), 'merged' ) } | ||||||||||||||||||||||||||
|
Subroutine
merged: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 464 | 3 | ▶ | sub coredump { _self(@_)->returncode & 128 } | ||||||||||||||||||||||||||
|
Subroutine
coredump: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 465 | 11 | ▶ | sub exitcode { _self(@_)->returncode >> 8 } | ||||||||||||||||||||||||||
|
Subroutine
exitcode: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 466 | 7 | ▶ | sub signal { _self(@_)->returncode & 127 } | ||||||||||||||||||||||||||
|
Subroutine
signal: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||
| 468 | 1 | ▶ | sub error_verbose { | ||||||||||||||||||||||||||
|
Subroutine
error_verbose: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||||||||||
| 469 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 470 | 1 | ▶ | return '' unless $self->error; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 471 | 1 | my $err = "Error executing `" . $self->command . "`:\n" . $self->error; | |||||||||||||||||||||||||||
| 472 | 1 | ▶ | if ( $self->stderr ne '' ) { $err .= "\nError output:\n" . $self->stderr } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 473 | 1 | return $err; | |||||||||||||||||||||||||||
| 474 | } | ||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||
| 476 | =head2 $obj->success() | ||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||
| 478 | Returns a 1 or 0, indicating whether or not the command run had an error or | ||||||||||||||||||||||||||||
| 479 | return code. | ||||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||
| 481 | =cut | ||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||
| 483 | 3 | ▶ | sub success { | ||||||||||||||||||||||||||
|
Subroutine
success: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||
| 484 | 3 | my $self = _self(@_); | |||||||||||||||||||||||||||
| 485 | 3 | ▶ | return ( $self->error eq '' ) ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 486 | } | ||||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||
| 488 | =head2 $obj->autodie(), $obj->chomped(), $obj->debug() | ||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||
| 490 | Returns a 1 or 0, if the corresponding $Backticks::xxx variable has been | ||||||||||||||||||||||||||||
| 491 | overridden within this object (as passed in as parameters during ->new()). | ||||||||||||||||||||||||||||
| 492 | Otherwise it will return the value of the corresponding $Backticks::xxx field | ||||||||||||||||||||||||||||
| 493 | as default. | ||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||
| 495 | =cut | ||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||
| 497 | 5 | ▶ | sub autodie { _get( shift(@_), 'autodie' ) } | ||||||||||||||||||||||||||
|
Subroutine
autodie: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 498 | 5 | ▶ | sub chomped { _get( shift(@_), 'chomped' ) } | ||||||||||||||||||||||||||
|
Subroutine
chomped: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 499 | 10 | ▶ | sub debug { _get( shift(@_), 'debug' ) } | ||||||||||||||||||||||||||
|
Subroutine
debug: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||
| 501 | # Append to this instance or the last run instance's error field | ||||||||||||||||||||||||||||
| 502 | 4 | ▶ | sub _add_error { | ||||||||||||||||||||||||||
|
Subroutine
_add_error: called CC=2 SCAR=7.4
| |||||||||||||||||||||||||||||
| 503 | 4 | my $self = _self( shift @_ ); | |||||||||||||||||||||||||||
| 504 | 0 | ▶ | if ( $self->{'error'} ) { $self->{'error'} .= "\n"; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 505 | 4 | $self->{'error'} .= join "\n", @_; | |||||||||||||||||||||||||||
| 506 | 4 | chomp $self->{'error'}; | |||||||||||||||||||||||||||
| 507 | } | ||||||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||||||
| 509 | # Print debugging output to STDERR if debugging is enabled | ||||||||||||||||||||||||||||
| 510 | 10 | ▶ | sub _debug_warn { | ||||||||||||||||||||||||||
|
Subroutine
_debug_warn: called CC=3 SCAR=14.2
| |||||||||||||||||||||||||||||
| 511 | 10 | ▶ | _self( shift @_ )->debug || return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 512 | 0 | warn "$_\n" foreach split /\n/, @_; | |||||||||||||||||||||||||||
| 513 | } | ||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||
| 515 | =head1 ACCESSING THE LAST RUN | ||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||
| 517 | Any of the instance $obj->method's above can also be called as | ||||||||||||||||||||||||||||
| 518 | Backticks->method and will apply to the last command run through the Backticks | ||||||||||||||||||||||||||||
| 519 | module. So: | ||||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||
| 521 | `run a command`; | ||||||||||||||||||||||||||||
| 522 | print Backticks->stderr; # Will show the STDERR for `run a command`! | ||||||||||||||||||||||||||||
| 523 | print Backticks->success; # Will show success for it... | ||||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||
| 525 | $foo = Backticks->run('another command'); | ||||||||||||||||||||||||||||
| 526 | print Backticks->stdout; # Output for the above line | ||||||||||||||||||||||||||||
| 527 | |||||||||||||||||||||||||||||
| 528 | If you want to access the last run object more explicitly, you can find it at: | ||||||||||||||||||||||||||||
| 529 | |||||||||||||||||||||||||||||
| 530 | $Backticks::last_run | ||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||
| 532 | =head1 NOTES | ||||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||
| 534 | =over 4 | ||||||||||||||||||||||||||||
| 535 | |||||||||||||||||||||||||||||
| 536 | =item No redirection | ||||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||
| 538 | Since we're not using the shell to open subprocesses (behind the scenes we're | ||||||||||||||||||||||||||||
| 539 | using L<open3>) you can't redirect input or output. But that shouldn't be a | ||||||||||||||||||||||||||||
| 540 | problem, since getting the redirected output is likely why you're using this | ||||||||||||||||||||||||||||
| 541 | module in the first place. ;) | ||||||||||||||||||||||||||||
| 542 | |||||||||||||||||||||||||||||
| 543 | =item STDERR is captured by default | ||||||||||||||||||||||||||||
| 544 | |||||||||||||||||||||||||||||
| 545 | Since we're capturing STDERR from commands which are run, the default behavior | ||||||||||||||||||||||||||||
| 546 | is different from Perl's normal backticks, which will print the subprocess's | ||||||||||||||||||||||||||||
| 547 | STDERR output to the perl process's STDERR. In other words, command error | ||||||||||||||||||||||||||||
| 548 | streams normally trickle up into Perl's error stream, but won't under this | ||||||||||||||||||||||||||||
| 549 | module. You can always just print it yourself: | ||||||||||||||||||||||||||||
| 550 | |||||||||||||||||||||||||||||
| 551 | warn `command`->stderr; | ||||||||||||||||||||||||||||
| 552 | |||||||||||||||||||||||||||||
| 553 | =item Source filtering | ||||||||||||||||||||||||||||
| 554 | |||||||||||||||||||||||||||||
| 555 | The overriding of `backticks` is provided by Filter::Simple. Source filtering | ||||||||||||||||||||||||||||
| 556 | can be weird sometimes... if you want to use this module in a purely | ||||||||||||||||||||||||||||
| 557 | traditional Perl OO style, simply turn off the source filtering as soon as you | ||||||||||||||||||||||||||||
| 558 | load the module: | ||||||||||||||||||||||||||||
| 559 | |||||||||||||||||||||||||||||
| 560 | use Backticks; | ||||||||||||||||||||||||||||
| 561 | no Backticks; | ||||||||||||||||||||||||||||
| 562 | |||||||||||||||||||||||||||||
| 563 | This way the class is loaded, but `backticks` are Perl-native. You can still | ||||||||||||||||||||||||||||
| 564 | use Backticks->run() or Backticks->new() to create objects even after the | ||||||||||||||||||||||||||||
| 565 | "no Backticks" statement. | ||||||||||||||||||||||||||||
| 566 | |||||||||||||||||||||||||||||
| 567 | =item Using Perl's backticks with Backticks | ||||||||||||||||||||||||||||
| 568 | |||||||||||||||||||||||||||||
| 569 | If you want to use Perl's normal backticks functionality in conjunction with | ||||||||||||||||||||||||||||
| 570 | this module's `backticks`, simply use qx{...} instead: | ||||||||||||||||||||||||||||
| 571 | |||||||||||||||||||||||||||||
| 572 | use Backticks; | ||||||||||||||||||||||||||||
| 573 | `command`; # Uses the Backticks module, returns an object | ||||||||||||||||||||||||||||
| 574 | qx{command}; # Bypasses Backticks module, returns a string | ||||||||||||||||||||||||||||
| 575 | |||||||||||||||||||||||||||||
| 576 | =item Module variable scope | ||||||||||||||||||||||||||||
| 577 | |||||||||||||||||||||||||||||
| 578 | The module's variables are shared everywhere it's used within a perl runtime. | ||||||||||||||||||||||||||||
| 579 | If you want to make sure that the setting of a Backticks variable is limited to | ||||||||||||||||||||||||||||
| 580 | the scope you're in, you should use 'local': | ||||||||||||||||||||||||||||
| 581 | |||||||||||||||||||||||||||||
| 582 | local $Backticks::chomped = 1; | ||||||||||||||||||||||||||||
| 583 | |||||||||||||||||||||||||||||
| 584 | This will return $Backticks::chomped to whatever its prior state was once it | ||||||||||||||||||||||||||||
| 585 | leaves the block. | ||||||||||||||||||||||||||||
| 586 | |||||||||||||||||||||||||||||
| 587 | =back | ||||||||||||||||||||||||||||
| 588 | |||||||||||||||||||||||||||||
| 589 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 590 | |||||||||||||||||||||||||||||
| 591 | Anthony Kilna, C<< <anthony at kilna.com> >> - L<http://anthony.kilna.com> | ||||||||||||||||||||||||||||
| 592 | |||||||||||||||||||||||||||||
| 593 | =head1 BUGS | ||||||||||||||||||||||||||||
| 594 | |||||||||||||||||||||||||||||
| 595 | Please report any bugs or feature requests to | ||||||||||||||||||||||||||||
| 596 | C<bug-backticks at rt.cpan.org>, | ||||||||||||||||||||||||||||
| 597 | or through the web interface at | ||||||||||||||||||||||||||||
| 598 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Backticks>. I will be | ||||||||||||||||||||||||||||
| 599 | notified, and then you'll automatically be notified of progress on your | ||||||||||||||||||||||||||||
| 600 | bug as I make changes. | ||||||||||||||||||||||||||||
| 601 | |||||||||||||||||||||||||||||
| 602 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 603 | |||||||||||||||||||||||||||||
| 604 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||
| 605 | |||||||||||||||||||||||||||||
| 606 | perldoc Backticks | ||||||||||||||||||||||||||||
| 607 | |||||||||||||||||||||||||||||
| 608 | You can also look for information at: | ||||||||||||||||||||||||||||
| 609 | |||||||||||||||||||||||||||||
| 610 | =over 4 | ||||||||||||||||||||||||||||
| 611 | |||||||||||||||||||||||||||||
| 612 | =item * RT: CPAN's request tracker (report bugs here) | ||||||||||||||||||||||||||||
| 613 | |||||||||||||||||||||||||||||
| 614 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Backticks> | ||||||||||||||||||||||||||||
| 615 | |||||||||||||||||||||||||||||
| 616 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||||||||||||||||
| 617 | |||||||||||||||||||||||||||||
| 618 | L<http://annocpan.org/dist/Backticks> | ||||||||||||||||||||||||||||
| 619 | |||||||||||||||||||||||||||||
| 620 | =item * CPAN Ratings | ||||||||||||||||||||||||||||
| 621 | |||||||||||||||||||||||||||||
| 622 | L<http://cpanratings.perl.org/d/Backticks> | ||||||||||||||||||||||||||||
| 623 | |||||||||||||||||||||||||||||
| 624 | =item * Search CPAN | ||||||||||||||||||||||||||||
| 625 | |||||||||||||||||||||||||||||
| 626 | L<http://search.cpan.org/dist/Backticks/> | ||||||||||||||||||||||||||||
| 627 | |||||||||||||||||||||||||||||
| 628 | =back | ||||||||||||||||||||||||||||
| 629 | |||||||||||||||||||||||||||||
| 630 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 631 | |||||||||||||||||||||||||||||
| 632 | Copyright 2012 Kilna Companies. | ||||||||||||||||||||||||||||
| 633 | |||||||||||||||||||||||||||||
| 634 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 635 | under the terms of either: the GNU General Public License as published | ||||||||||||||||||||||||||||
| 636 | by the Free Software Foundation; or the Artistic License. | ||||||||||||||||||||||||||||
| 637 | |||||||||||||||||||||||||||||
| 638 | See http://dev.perl.org/licenses/ for more information. | ||||||||||||||||||||||||||||
| 639 | |||||||||||||||||||||||||||||
| 640 | =cut | ||||||||||||||||||||||||||||
| 641 | |||||||||||||||||||||||||||||
| 642 | 1 | 1; # End of Backticks | |||||||||||||||||||||||||||