dimmesdale has asked for the wisdom of the Perl Monks concerning the following question:

I have a (probably simple) question about invoking system commands with perl. I know about the `command` operators, but the question involves somewhat of syntax/parsing.

I need to call a perl script test.pl with two arguments. The arguments are variables, and I need them interpolated. The second argument I need to actually combine two variables with a '\' (it's a directory path), and have them, of course, interpolated as well.

Now, I can imagine that
`perl test.pl "$var1" "$var2\\$var3"`
won't work (and is quite atrocious). I tested several variants (making a temporary variable) and none seemed to work (let it be noted that I took the quotes out, and made a test.pl file to print its given arguments; on several tests I got a 'Bad command/filename, etc' error, and several nothing happened at all, save a short pause).

WHAT WILL WORK?

Replies are listed 'Best First'.
(jeffa) Re: A question with commands
by jeffa (Bishop) on Jun 24, 2002 at 15:02 UTC
    Try system:
    system('perl', 'test.pl', $var1, $var2.'\\'.$var3);
    You should probably fully qualify the path to test.pl, $ENV{HOME} is handy for this:
    system( 'perl', $ENV{HOME}.'\path\to\test.pl', $var1, qq|$var2\\$var3|, );
    (udpated node - yada yada yada, read question closer)

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      For the record this is more or less equivelent to doing
      `cmd.exe /c perl.exe test.pl $var1 $vars\\$var3`
      The only difference is that the cmd.exe call will be appropriately qualified, both in terms of OS (the w9x series has a slighly different syntax) and in terms of using the correct systempath to get to cmd.exe

      Yves / DeMerphq
      ---
      Writing a good benchmark isnt as easy as it might look.

Re: Variable interpolation using back ticks
by talexb (Chancellor) on Jun 24, 2002 at 14:59 UTC
      Now, I can imagine that
      `perl test.pl "$var1" "$var2\\$var3"`
      won't work (and is quite atrocious).
    The back tick does variable interpolation so I believe that you should be able to do `perl test.pl $var1 $var2\\$var3` and have it work. Assuming that Perl is in the path and that test.pl is in the local directory.

    --t. alex

    "Mud, mud, glorious mud. Nothing quite like it for cooling the blood!" --Michael Flanders and Donald Swann

      Well actually perl isn't in the path. . . but that's a long story (soon to be fixed, hopefully).

      The command that I'm using looks like this:

      `E:\perl\bin\perl test.pl $arg1 $arg2`;

      The problem is that it still gives me a bad command/filename warning (Windows 98, {active}perl running on MS-DOS prompt)

      Update:

      System procedure works. Thanks boss.