Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

redirecting output to STDERR and STDOUT

by Anonymous Monk
on Feb 22, 2005 at 14:15 UTC ( #433333=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I'm a novice. I have a situation where I'm working on a perl program that calls a module, passing it a scalar containing the fully qualified filename for a log file, and a scalar containing the fully qualified filename for an error log. However, I would rather have the output go to the STDOUT and STDERR. When I attempt to simply set the scalar $logfile = STDERR; I get an error (Bareword "STDERR" not allowed while "strict subs" in use). What is the correct way to do this?
  • Comment on redirecting output to STDERR and STDOUT

Replies are listed 'Best First'.
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.

    Cheers - L~R

Re: redirecting output to STDERR and STDOUT
by RazorbladeBidet (Friar) on Feb 22, 2005 at 14:24 UTC
    Perhaps check perlopentut (specifically playing with STDIN and STDOUT).
    --------------
    It's sad that a family can be torn apart by such a such a simple thing as a pack of wild dogs
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
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.
      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!
        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
        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.
Re: redirecting output to STDERR and STDOUT
by perlfan (Curate) 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...
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...";
    

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://433333]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (1)
As of 2018-07-21 23:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (450 votes). Check out past polls.

    Notices?