Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Redirecting Output to shell command from within perl

by haukex (Archbishop)
on Feb 17, 2018 at 09:13 UTC ( [id://1209377]=note: print w/replies, xml ) Need Help??


in reply to Redirecting Output to shell command from within perl

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: \$?=$?";

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1209377]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2024-04-20 04:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found