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:
package Foo; use strict; sub pdftotext { my $self = shift; my $args = join ' ', @_; my $output = qx{/usr/bin/pdftotext $args}; } 1;
test.pl:
# 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.


In reply to Trouble overriding qx// operator in a test script by windowbreaker

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.