Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I expand variables in Perl readpipe handlers?

It seems that variables in backticks are not expanded when passed onto the readpipe function. If I override the readpipe function, how do I expand variables?

BEGIN {
*CORE::GLOBAL::readpipe = sub {print "Run:@_\n"};
}

`ls /root`;
my $dir = "/var";
`ls $dir`;

Running this gives:

Run:ls /root
Run:ls $dir

I am trying to mock external calls for a test code that I am writing. If there is a CPAN module somewhere which can help in taking care of all this, that would help too.

Update:

I have decided to use a really ugly workaround to my problem. It turns out that using readpipe() instead of backticks expands variables correctly. I am using an automatic script cleaner before running my tests which converts all backticks to readpipe() before running the tests.

e.g Running:

$ cat t.pl

BEGIN {
    *CORE::GLOBAL::readpipe = sub {print "Run:@_\n"};
}

`ls /root`;
my $dir = "/var";
`ls $dir`;
readpipe("ls $dir");

Gives:

$ perl t.pl
Run:ls /root
Run:ls $dir
Run:ls /var

I am still looking out for a cleaner solution though.

like image 524
Sandip Bhattacharya Avatar asked Oct 07 '22 13:10

Sandip Bhattacharya


2 Answers

That appears to be a bug in Perl. Use perlbug to report it.

like image 195
ikegami Avatar answered Oct 19 '22 22:10

ikegami


You probably want to use IPC::Run instead.

use IPC::Run 'run';

run [ "ls", $dir ], ">", \my $stdout or die "Cannot run - $!";

Or if you didn't want to capture the output, system() may be better

system( "ls", $dir ) == 0 or die "Cannot system - $!";
like image 38
LeoNerd Avatar answered Oct 20 '22 00:10

LeoNerd