This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Sync with libnet 1.18
[perl5.git] / lib / Shell.pm
index defb5d7..a89db69 100644 (file)
@@ -1,12 +1,14 @@
 package Shell;
-use 5.005_64;
+use 5.006_001;
 use strict;
 use warnings;
+use File::Spec::Functions;
+
 our($capture_stderr, $VERSION, $AUTOLOAD);
 
-$VERSION = '0.4';
+$VERSION = '0.5.2';
 
-sub new { bless \$VERSION, shift } # Nothing better to bless
+sub new { bless \my $foo, shift }
 sub DESTROY { }
 
 sub import {
@@ -28,10 +30,15 @@ sub AUTOLOAD {
     shift if ref $_[0] && $_[0]->isa( 'Shell' );
     my $cmd = $AUTOLOAD;
     $cmd =~ s/^.*:://;
+    my $null = File::Spec::Functions::devnull();
+    $Shell::capture_stderr ||= 0;
     eval <<"*END*";
        sub $AUTOLOAD {
+           shift if ref \$_[0] && \$_[0]->isa( 'Shell' );
            if (\@_ < 1) {
-               \$Shell::capture_stderr ? `$cmd 2>&1` : `$cmd`;
+               \$Shell::capture_stderr ==  1 ? `$cmd 2>&1` : 
+               \$Shell::capture_stderr == -1 ? `$cmd 2>$null` : 
+               `$cmd`;
            } elsif ('$^O' eq 'os2') {
                local(\*SAVEOUT, \*READ, \*WRITE);
 
@@ -83,7 +90,8 @@ sub AUTOLOAD {
                        \$_ = \$_;
                    }
                }
-               push \@arr, '2>&1' if \$Shell::capture_stderr;
+               push \@arr, '2>&1'        if \$Shell::capture_stderr ==  1;
+               push \@arr, '2>$null' if \$Shell::capture_stderr == -1;
                open(SUBPROC, join(' ', '$cmd', \@arr, '|'))
                    or die "Can't exec $cmd: \$!\\n";
                if (wantarray) {
@@ -139,7 +147,7 @@ Here's one that'll whack your mind a little out.
     sub ps;
     print ps -ww;
 
-    cp("/etc/passwd", "/tmp/passwd");
+    cp("/etc/passwd", "/etc/passwd.orig");
 
 That's maybe too gonzo.  It actually exports an AUTOLOAD to the current
 package (and uncovered a bug in Beta 3, by the way).  Maybe the usual
@@ -153,6 +161,9 @@ Larry
 If you set $Shell::capture_stderr to 1, the module will attempt to
 capture the STDERR of the process as well.
 
+If you set $Shell::capture_stderr to -1, the module will discard the 
+STDERR of the process.
+
 The module now should work on Win32.
 
  Jenda