Re: Running perl from a test on Windows

Eirik Berg Hanssen <[email protected]>
Newsgroups gmane.comp.lang.perl.quality-assurance
Message-ID <CAHAeAG6Ai=QiGwDiDbvEPerkRZg3jma5buqr2qe5-jzKfNGM3A@mail.gmail.com>
On Mon, Jan 14, 2013 at 9:06 PM, Gabor Szabo <[email protected]> wrote:

> On Mon, Jan 14, 2013 at 9:59 PM, Buddy Burden <[email protected]>
> wrote:
> > Guys,
> >
> > Okay, my Google-fu is failing me, so hopefully one of you guys can help
> me out.
> >
> > For a test, I need to run a snippet of Perl and collect the output.
> > However, if it rus in the current interpreter, it will load a module
> > that I need not to be loaded ('cause I'm also going to test if my code
> > properly loads it).  So I want to run it in a separate instance of
> > Perl.
> >
> > First (naive) attempt:
> >
> >     my $output = `$^X -e '$cmd'`;
> >
> > This works fine on Linux, but fails on Windows.  Happily, as soon as I
> > saw the failures, I recognized I had a quoting problem.  No worries, I
> > said: let's just bypass the shell altogether:
>

  On Windows, that still leaves a quoting problem, I believe.
IPC::System::Simple certainly does not seem to handle it: Unless I misread
it entirely, it ends up sending "$^C -e $cmd" as the command line to
Win32::Process::Create.

  Let's see ...

C:\Windows\system32>perl -MIPC::System::Simple=capturex -e "print
capturex($^X, '-le', qq{print(qq(--ARG\@{[++\$i]}-->\$_))for\@ARGV;
die(q/Oops - that was odd/)})"
--ARG1-->die(q/Oops
--ARG2-->-
--ARG3-->that
--ARG4-->was
--ARG5-->odd/)

C:\Windows\system32>

  Oh yes.  It's not doing any quoting.



> I am not sure if this helps but in Windows you need to put the
> double-quotes around  $cmd
>
>     my $output = qx{$^X -e "$cmd"};
>
> and of course inside $cmd you should use single quotes and not double
> quotes if you need
> some quotation.
>
> Oh the joy :)
>

  Or, since $^X presumably is a perl, use C<< qq() >> and/or C<< \x22 >>
instead of C<< "" >>. :)

C:\Windows\system32>perl -e "print qx!$^X -le
\x22print(qq(--ARG\@{[++\$i]}-->\$_))for\@ARGV; die(q/Oops - that was
odd/)\x22!"
Oops - that was odd at -e line 1.

C:\Windows\system32>


Eirik
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.