Re: redirecting output to STDERR and STDOUT
by Limbic~Region (Chancellor) on Feb 22, 2005 at 14:22 UTC
|
Anonymous Monk,
I'm working on a perl program that calls a module,...
Without telling us what module, it is difficult to help you. Some modules support more than just filename - such as a pre-existing filehandle. The following might work.
Module::Name->method( log => *STDOUT, error => *STDERR );
Replacing the appropriate pieces with the appropriate information. You might also want to look at IO::Tee so that you can get a log and information to the screen but without telling us what module, we will just be guessing.
| [reply] [d/l] |
Re: redirecting output to STDERR and STDOUT
by RazorbladeBidet (Friar) on Feb 22, 2005 at 14:24 UTC
|
| [reply] |
Re: redirecting output to STDERR and STDOUT
by helgi (Hermit) on Feb 22, 2005 at 15:34 UTC
|
I don't know what the "correct" way to do this is, but here's one way:
#!/usr/bin/perl
use warnings;
use strict;
my $logfile ='/tmp/foo.txt';
open STDERR, ">>", $logfile or die "Cannot redirect STDERR to $logfile
+:$!\n";
my $in = 'C:/tmp/in.txt';
open IN, $in or die "Cannot open $in:$!\n";
close IN;
close STDERR;
--
Regards,
Helgi Briem
hbriem AT simnet DOT is
| [reply] [d/l] |
Re: redirecting output to STDERR and STDOUT
by Scarborough (Hermit) on Feb 22, 2005 at 17:48 UTC
|
Are you saying you wont the module to print to STDOUT and STDERR? If so your going to need to get under the hood of the module and work it out from there as the module is set to print to the files specified I guess. If other code is dependent on this module you might think twice before going down this line. | [reply] |
|
Thank you all for your help. Having researched this program a little further, I'm afraid that what actually needs to be done is a change to a program written in C which is called by the .pm that the perlscript I'm working in calls. It just doesn't appear that the C program allowed for this possibility. Blame it on poor planning I guess.
Anyway, thank you all for some excellent responses!
| [reply] |
|
If you are calling a C program, and need to separate and direct your STDOUT and STDERR, you might be able to do it with the IPC::Open3 module. Just an idea.
I'm not really a human, but I play one on earth.
flash japh
| [reply] |
|
On Solaris and Linux /dev/stdin, /dev/stdout, and /dev/stderr are device names for standard input, standard output, and standard error. /dev/stdin is occasionally useful when piping data to a recaltricant C program which doesn't read from standard input or recognise '-' as a synonym for standard input. Likewise for the others.
In your case you would set $logfile = "/dev/stderr"; of course this will only help you if your OS supports /dev/stderr. /dev/fd/2 might be another option for standard error.
On Solaris, man -s 4 fd for details.
| [reply] [d/l] [select] |
|
|
Re: redirecting output to STDERR and STDOUT
by perlfan (Parson) on Feb 22, 2005 at 15:38 UTC
|
Not elegent, but something like:
In bash:
# test.pl 2>&1 /dev/null
update: removed some code I brain farted on... | [reply] [d/l] |
|
my $err = <STDERR>;
Eh?
STDERR is usually defined for writing only. Reading from it will either throw an error or do nothing. (Unless you've played games with the handles, of course.)
Or am I completely misunderstanding your intent?
| [reply] [d/l] [select] |
Re: redirecting output to STDERR and STDOUT
by comand (Acolyte) on Feb 23, 2005 at 05:42 UTC
|
To copy an already open filehandle into a scalar, you need to
use a GLOB reference:
$logfile = \*STDERR;
print $logfile "This will go to STDERR now...";
| [reply] |