Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

hiding perl-wrapped command output

by rockneybot (Novice)
on Feb 02, 2005 at 02:11 UTC ( [id://427116]=perlquestion: print w/replies, xml ) Need Help??

rockneybot has asked for the wisdom of the Perl Monks concerning the following question:

Hi. I have a script that relies heavily on system() calls to external programs. I'd like to be able to hide some or all of the ouput from STDOUT. What is the best way to do this? For instance if I have this:
system "/sbin/sfdisk -uM -q -L -O disksave --no-reread $device <<EOF\n +0,\nEOF"
How do I hide the output or perhaps selectively show the output?

Replies are listed 'Best First'.
Re: hiding perl-wrapped command output
by redhotpenguin (Deacon) on Feb 02, 2005 at 02:46 UTC
    You can use IPC::Run3 to redirect stdout to a perl data structure. Perhaps something like this might do the trick.

    use IPC::Run3; my @cmd = qw( /sbin/sfdisk -uM -q -L -O disksave --no-reread /dev/hdz); my ($in, $out, $err); run3 \@cmd, \$in, \$out, \$err;
Re: hiding perl-wrapped command output
by hsinclai (Deacon) on Feb 02, 2005 at 02:21 UTC
    This isn't exactly "hiding" STDOUT, but you can send STDOUT to a scalar, which would also allow you to check to see if the output of your system command is what you expected, from within the script.
    my $holder; sub command { open local(*STDOUT), '>', \$holder; your_system_command; }
Re: hiding perl-wrapped command output
by fauria (Deacon) on Feb 02, 2005 at 03:13 UTC
    If you use a Unix system, and bash or sh as command interpreters (this does not work under csh), you can allways relay on system file descriptors and redirect them to /dev/null:

    system "ls ~ &>/dev/null"; #Supress both stderr and stdout system "ls ~ 2>/dev/null"; #Supress stderr system "ls ~ 1>/dev/null"; #Supress stdout
Re: hiding perl-wrapped command output
by greenFox (Vicar) on Feb 02, 2005 at 03:08 UTC

    You might want to read this very recent thread.

    --
    Do not seek to follow in the footsteps of the wise. Seek what they sought. -Basho

Re: hiding perl-wrapped command output
by rockneybot (Novice) on Feb 02, 2005 at 03:03 UTC
    I'll try both of these methods. Would something like this also be useful?:
    open CMD, "/sbin/fdisk|", '<', "-uM -q -L -O disksave --no-reread $dev +ice <<EOF\n0,\nEOF" or die "dude, what are you trying to do?";
    Perhaps not, but I'll mess around with it. Also, what does the '*' indicate in open local(*STDOUT)?
      Include  $! in your die statement if you need to learn the error..

      what does the '*' indicate in
      typeglob on handle ... Search for "local on a glob" in this page which explains it..

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://427116]
Approved by greenFox
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2024-04-19 10:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found