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.