Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modify IO layers used by backticks

Tags:

perl

i.e. being able to do

$result = `my command`;

using :raw, :utf8, etc.

Any special variables I don't know about, alternative methods or modules that can be used?

like image 731
OrangeDog Avatar asked Dec 22 '22 10:12

OrangeDog


2 Answers

Well I don't know if there are any special variables, but why not use open() for that task? You can specify the encoding on pipes like you would on files:

open(my $cmdin, "-|:raw", "your command");
my $result = join('', <$cmdin>);
close($cmdin);
like image 99
vstm Avatar answered Dec 31 '22 17:12

vstm


Use popen:

open (my $fd, "-|", $prog, @args) 
    or die "Couldn't start $prog: $!";
do_whatever($fd);
while (<$fd>) { ... };

Or if that's not enough you should look at IPC::Open2 and its cousin Open3.

like image 24
Dallaylaen Avatar answered Dec 31 '22 16:12

Dallaylaen