Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Redirecting Standard Output inside perl

by Anonymous Monk
on Nov 09, 2017 at 11:21 UTC ( #1203023=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

When I run a perl program, I just redirect it the normal way

>perl > c:/users/outputs/out.txt

But instead of having to write that path in the command line, can I for example do:

STDOUT path = 'c:/users/outputs/out.txt';

or, if it was possible to just take the path writing in the command, and save it to a variable($)?

And the WHY I want this,

1: Not having to write the SDTOUT path every time I want to run it (After having closed the console)

2: I'm manipulating a lot of data and printing to STDOUT (out.txt), and I want to open it again in the same program, so I can read it again, and do new manipulations

I could push it all into an array, but then I risk running out of memory, so I just print to STDOUT.

(And I'm on Windows 10 (Unfortunately))

Replies are listed 'Best First'.
Re: Redirecting Standard Output inside perl
by haukex (Abbot) on Nov 09, 2017 at 11:42 UTC

    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.

Re: Redirecting Standard Output inside perl
by hippo (Canon) on Nov 09, 2017 at 11:37 UTC
      And I'm on Windows 10 (Unfortunately) ... In which case the above for select might well not apply.

      If you are referring to the single-arg select, I don't think you have to worry, perlport says (emphasis mine):

      (Win32, VMS) Only implemented on sockets.

      (RISC OS) Only reliable on sockets.

      Note that the "select FILEHANDLE" form is generally portable.

Re: Redirecting Standard Output inside perl
by syphilis (Chancellor) on Nov 09, 2017 at 11:53 UTC
    1: Not having to write the SDTOUT path every time I want to run it (After having closed the console)

    I find that on Windows 7 I can achieve this sort of thing by creating an environment variable:
    C:\_32\pscrpt>set MY_STDOUT=C:\batch\demo.txt C:\_32\pscrpt>perl >%MY_STDOUT%
    When I do that, the output of the perl program ( is written to C:\batch\demo.txt .
    If you want to be able to do that in a freshly opened console, then you'll want to set the environment variable permanently. (The way I set it, it was only set for the life of that particular console.)

Re: Redirecting Standard Output inside perl
by soonix (Monsignor) on Nov 09, 2017 at 13:15 UTC
    Before knowing Capture::Tiny, I did it with something like
    ... my $mode = '>'; if ($option eq 'append') { $mode = '>>'; }; close STDOUT; open (STDOUT, $mode, $fname) or die "cannot open (STDOUT) $fname: $!"; ...
    updated module name … well, I do like cats :-)

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2018-06-19 21:15 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (115 votes). Check out past polls.