in reply to Redirecting Output to shell command from within perl

you can call less inside Perl,

This hack (somehow) works under win (though with some artefacts* on the console)

$out = join "\n" , 1..100; open PIPE, "| more"; print PIPE $out;

but I don't think you can redirect the final output of your script from within.

Only the parent process should be allowed to do this.

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Wikisyntax for the Monastery

UPDATE
*) adding close PIPE; after the print solves the issue :)

Replies are listed 'Best First'.
Re^2: Redirecting Output to shell command from within perl
by BrowserUk (Patriarch) on Feb 17, 2018 at 21:17 UTC

    After this line:

    open STDOUT, '| wc -l' or die "$! / $^E";

    Anything the program prints, will be piped to wc and the count will show up on the command line when the script exits.

    Add:

    open STDERR, '&STDOUT';

    And wc will see STDERR also.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
    In the absence of evidence, opinion is indistinguishable from prejudice. Suck that fhit
      When I do this on Windows with open STDOUT, '|more' , than the effect is significantly different to calling script.pl|more on the cmd line.

      The console is intercepting some keystrokes and using it for it's own commands, for instance I can't quit with q and I can't scroll to next page with bar .

      Haven't tested on linux with "less" yet.

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Wikisyntax for the Monastery

        If you redirect output to an interactive command from within a program, I would expect different results to having done it from the command line; but then I wouldn't do it, because it doesn't seem useful, but try this:

        open STDOUT, '| tee -i null | more';; print "hello world! $_" for 1.. 1000; close STDOUT; exit;;

        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
        In the absence of evidence, opinion is indistinguishable from prejudice. Suck that fhit