windowbreaker has asked for the wisdom of the Perl Monks concerning the following question:
I am testing a function in one of my modules which utilizes qx// to do it's work. I'd like to verify exactly how it's calling qx// from within my test script by overriding qx// and capturing the arguments. I've done this by overriding CORE::GLOBAL::readpipe, which according to Programming Perl 3nd ed, pg 772, is the internal function that implements the qx// operator.
The problem is that the variable given to qx// as an argument is not being expanded in my test script. Here is the sample code. Foo.pm is the module that uses the qx// operator. test.pl is my test script which showcases the problem.
Foo.pm:test.pl:package Foo; use strict; sub pdftotext { my $self = shift; my $args = join ' ', @_; my $output = qx{/usr/bin/pdftotext $args}; } 1;
# Override the qx// operator to capture args to $qx_args my $qx_args; BEGIN { *CORE::GLOBAL::readpipe = sub { $qx_args = $_[0] } } # Load module that uses qx// use Foo; # Call method which will use our overridden qx// function Foo->pdftotext('/tmp/myfile.pdf'); # print arguments to qx// print "qx// args were: [$qx_args]\n";
This prints the following output:
qx// args were: [/usr/bin/pdftotext $args]
As you can see, $args is not being expanded. I've verified that qx// is evaluated in a double-quote interpolative context, unless the delimiters used are single-quotes, which they are not in this case.
I believe the problem lies somewhere in the anonymous sub that I'm using to override readpipe. My question is how can I modify that sub to properly expand the variable $args?
I am also open to a better way of doing this from my test script if my approach is wrong. I could not find a Test:: module on CPAN that overrides qx// or readline.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Trouble overriding qx// operator in a test script
by Khen1950fx (Canon) on May 30, 2011 at 19:20 UTC | |
|
Re: Trouble overriding qx// operator in a test script
by ikegami (Patriarch) on May 30, 2011 at 18:48 UTC | |
|
Re: Trouble overriding qx// operator in a test script
by flexvault (Monsignor) on May 31, 2011 at 12:44 UTC |