Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Suppress Output 1

Status
Not open for further replies.

CJason

Programmer
Oct 13, 2004
223
US
I am running a perl script that does some "system" calls. I would like to suppress the output from the "system" calls. So, more to the point, is there a way to disable output to the screen at the beginning of my script, then enable it again towards the end?

Thanks in advance!!!!
 
You could reopen STDOUT and/or STDERR file handles from within the script. System calls inherit the system handles from perl.

The other option is to use the shell to redirect them to a null device (how varied on your OS). This is a bit heavier of a choice, as if works a call at a time. You could kill it for the entirety of the perl script and everything inside when you call it, or you can use it on each (or select) system calls you make that you want suppressed.

________________________________________
Andrew

I work for a gift card company!
 
icrf, can you give me an example of both your "reopen" option and "redirecting to null device, for select system calls" option? Thanks much!!!
 
You null device varies depending on your operating system. In the *nix world, it's /dev/null, but in windows/dos, it's just nul. You can do this for the entire main script when you call it, or use the same syntax when making a particular system call from within the script.

I'm not sure how this would work in windows, because the null device 'nul' you get at a command prompt only seems to exist there.
Code:
C:\> perl script.pl >nul 2>&1
or
$ perl script.pl >/dev/null 2>&1
ref:
Code:
open(SAVEOUT, ">&STDOUT");
open(SAVEERR, ">&STDERR");

open(STDOUT, ">/dev/null") || die "Can't redirect stdout";
open(STDERR, ">&STDOUT") || die "Can't dup stdout";

select(STDERR); $| = 1;# make unbuffered
select(STDOUT); $| = 1;# make unbuffered

print STDOUT "stdout 1\n";# this works for
print STDERR "stderr 1\n"; # subprocesses too

close(STDOUT);
close(STDERR);

open(STDOUT, ">&SAVEOUT");
open(STDERR, ">&SAVEERR");

print STDOUT "stdout 2\n";
print STDERR "stderr 2\n";
ref:
Since it sounds like you just want to suppress the output of some system calls, the former method is probably easier, albeit less portable.

________________________________________
Andrew

I work for a gift card company!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top