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

Hey guys, not sure if this has been answered here before, but I was looking to feed less a string stored within a variable $output.
Essentially I'm trying to do the following from within the script:

          script.pl|less

Any help would be greatly appreciated!

Replies are listed 'Best First'.
Re: Redirecting Output to shell command from within perl
by haukex (Archbishop) on Feb 17, 2018 at 09:13 UTC

    I think it might be the most robust/portable to go through a temporary file using File::Temp and system:

    use File::Temp qw/tempfile/; my ($fh,$fn) = tempfile(UNLINK=>1); print $fh $output; close $fh; system('less',$fn) == 0 or die "system: \$?=$?";

    But the following two also work; IPC::Run3 also usually works with temporary files internally:

    use IPC::Run3 'run3'; run3 ['less'], \$output or die $!; die "run3: \$?=$?" if $?;

    Or with pure Perl and piped open, available in Perl 5.8 and up - but see this node as well as this node for the pitfalls!

    my @cmd = ('less', '-'); die '@cmd must have more than one element' unless @cmd>1; open my $fh, '|-', @cmd or die $!; print $fh $output; close $fh or die $! ? $! : "pipe: \$?=$?";
Re: Redirecting Output to shell command from within perl (Update "close PIPE")
by LanX (Saint) on Feb 17, 2018 at 01:26 UTC
    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 :)

      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

Re: Redirecting Output to shell command from within perl
by salva (Canon) on Feb 17, 2018 at 10:09 UTC
Re: Redirecting Output to shell command from within perl
by stevieb (Canon) on Feb 17, 2018 at 00:52 UTC

    Welcome to the Monastery and the inquisitive and awesome world of Perl, dracut!

    You need to be a bit more descriptive here...

    Where's the $output variable?

    We need more context as to what you're really trying to do. This works:

    $ cat echo.pl use warnings; use strict; print "hey!\n";

    Then...

    perl echo.pl | less

    Output:

    hey!

    Are you just wanting to run a Perl script through less or more on the command line? Are you attempting to grab certain variables from a Perl script?