Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Open pipe to a function?

by Sprad (Hermit)
on Dec 10, 2003 at 21:32 UTC ( [id://313882]=perlquestion: print w/replies, xml ) Need Help??

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

I've written a simple Perl implementation of tee. I'd like to use it in a larger script to log STDERR to a file. Is it possible to put my tee code inside the main script as a function and pipe to it, or do I have to have it in a separate file?

---
A fair fight is a sign of poor planning.

Replies are listed 'Best First'.
Re: Open pipe to a function?
by Roy Johnson (Monsignor) on Dec 10, 2003 at 21:48 UTC
    Have you looked at IO::Tee?

    The PerlMonk tr/// Advocate
Re: Open pipe to a function?
by hardburn (Abbot) on Dec 10, 2003 at 21:44 UTC

    I'd like to use it in a larger script to log STDERR to a file.

    You don't need a seperate program for doing that. You can just reopen STDERR to wherever you like:

    open(STDERR, '>', '/path/to/logfile') or die "Can't open STDERR to /path/to/logfile: $!\n";

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    : () { :|:& };:

    Note: All code is untested, unless otherwise stated

Re: Open pipe to a function?
by tcf22 (Priest) on Dec 10, 2003 at 21:41 UTC
    If you are going to pipe it to a script, then I believe it has to be in another script.

    If all you need to do is write STDERR to a file, why not just do this at the beginning of your script:
    open(STDERR, '>error.txt');
    Update:Try using IO::Tee, like suggested by Roy Johnson. I included a sample in my reply to you below.

    - Tom

      I can't just redirect it, because I also want to see it at runtime.

      ---
      A fair fight is a sign of poor planning.

        You could probably do some tricks with IO::Multiplex. Just save the orginal filehandle like this:

        my $old_stderr = *STDERR;

        And then reopen STDERR with IO::Multiplex with $old_stderr and a filehandle to the log file.

        ----
        I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
        -- Schemer

        : () { :|:& };:

        Note: All code is untested, unless otherwise stated

        How about
        use IO::Tee; open(ERRLOG, '>error.txt'); my $tee = new IO::Tee(\*STDOUT, \*ERRLOG); *STDERR = $tee;

        - Tom

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (7)
As of 2024-04-23 17:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found