I'm afraid I don't know what you mean with "STDOUT path = 'c:/users/outputs/out.txt';". But I can answer the general question about how to capture a script's STDOUT from within that script. First, and this has the most limited general usefulness, you can use select to change the default output handle for print statements.

But probably most useful is Capture::Tiny, which can capture output not only from the Perl script itself but any external processes it happens to run. Its documentation includes a list of other output-capturing modules, such as ones that use another method for capturing output to a filehandle, tied handles. But I wouldn't necessarily recommend that method either, since it is also not generally applicable (e.g. it won't capture things from external programs run). Update: For the sake of completeness, as another method, the open documentation shows how to re-open STDOUT, but this is a fairly low-level operation and again I'd recommend the help of a module here. In any of the aforementioned cases you can of course save the name of the target file in a variable, or perhaps get it from an environment variable via %ENV or the command line via e.g. @ARGV or Getopt::Long.


In reply to Re: Redirecting Standard Output inside perl by haukex
in thread Redirecting Standard Output inside perl by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.