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

I have many legacy scripts which print logs directly to STDOUT and use a scheduler where they pipe the stdout to a log file >>alog.log ; however is there any way inside perl to force these msgs to print to a file rather than console

--just wondering if its possible to do it

Replies are listed 'Best First'.
Re: STDOUT msg routing
by hippo (Archbishop) on Feb 25, 2015 at 10:15 UTC

    Yes, all you need really is open, close and select. If you have the possibilities of multiple scripts/instances writing to the same destination file then you will want to think about locking too.

      oh .okay i actually tried it before i opened a file handle like

      open STDOUT, '>', "files.out"

      and then closed the filehandle at end of script , but it throws a compilation error at first print staement

        The way you showed is the usual way to redirect a filehandle to a (different) file in Perl.

        Maybe you can tell us the compilation error?

Re: STDOUT msg routing
by sandy105 (Scribe) on Feb 25, 2015 at 10:50 UTC

    error message that i got

    syntax error at move.perl line 120, near "print" BEGIN not safe after errors--compilation aborted at move.perl line 170 +.

      Does the previous line statement end in a semicolon?

      .oO( is it a vegetable? ) .oO( is it a movie? )</20_questions>

      --MidLifeXis

      Can you provide some more code surrounding line 170? The following works for me:
      #!/usr/bin/perl use strict; use warnings; open STDOUT, ">", "files.out"; print "test\n";