Command-Run
view release on metacpan or search on metacpan
The following protections are applied:
- `local $_;`
Prevents the callee from modifying the caller's `$_`. This is
critical when the caller aliases `$_` to important data (e.g.,
greple's `local *_ = shift` to alias `$_` to the content buffer).
Without this protection, a callee's `while (<>)` loop
would set `$_` to `undef` at EOF, destroying the caller's data.
- `local @ARGV`
Prevents the callee from modifying the caller's `@ARGV`.
- `$0` save/restore
Prevents the callee from permanently changing the program name.
# COMPARISON WITH SIMILAR MODULES
There are many modules on CPAN for executing external commands.
lib/Command/Run.pm view on Meta::CPAN
binmode STDIN, $raw ? ':utf8' : ':encoding(utf8)';
} elsif (my $input = $obj->{INPUT}) {
$input->seek(0, 0) or die "seek: $!\n";
open $save_stdin, '<&', \*STDIN or die "dup STDIN: $!\n";
open STDIN, '<&', $input->fileno or die "redirect STDIN: $!\n";
binmode STDIN, $raw ? ':utf8' : ':encoding(utf8)';
}
# Set global state
local $_;
local @ARGV = @command;
my $orig_0;
if (my $name = code_name($code)) {
$orig_0 = $0;
$0 = $name;
}
# Execute
my $result = 0;
eval { $code->(@command) };
if ($@) {
lib/Command/Run.pm view on Meta::CPAN
=over 4
=item C<local $_;>
Prevents the callee from modifying the caller's C<$_>. This is
critical when the caller aliases C<$_> to important data (e.g.,
greple's C<local *_ = shift> to alias C<$_> to the content buffer).
Without this protection, a callee's C<< while (E<lt>E<gt>) >> loop
would set C<$_> to C<undef> at EOF, destroying the caller's data.
=item C<local @ARGV>
Prevents the callee from modifying the caller's C<@ARGV>.
=item C<$0> save/restore
Prevents the callee from permanently changing the program name.
=back
=head1 COMPARISON WITH SIMILAR MODULES
( run in 1.350 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )