Three-G has asked for the wisdom of the Perl Monks concerning the following question:

Consider the following attempt to create an at job on a WinNT box:

system("at 12:00 program.exe \"the.server.org:1234\"");

The quote marks around the server URL are absolutely required by program.exe to work. When I go back to look at the list of at jobs, the quote marks are gone. I thought escaping them would work. I've even tried this with backticks. Any ideas?

Replies are listed 'Best First'.
Re: Quote marks lost in system() calls
by chipmunk (Parson) on Dec 21, 2000 at 02:21 UTC
    The backslashes are used up in getting the quotes into the double quoted string in Perl, which leaves no backslashes to protect the quotes in the shell. Try adding another level of backslashes: system("at 12:00 program.exe \\\"the.server.org:1234\\\""); Even better, switch to single quotes, and you don't need the extra backslashes after all: system('at 12:00 program.exe \"the.server.org:1234\"');
(tye)Re: Quote marks lost in system() calls
by tye (Sage) on Dec 21, 2000 at 02:23 UTC

    I suspect this is a problem with "at" rather than with Perl.

    system("at 17:00 echo \"go home\""); system("at"); system("at 1 /delete");
    produces:
    Added a new job with job ID = 1 Status ID Day Time Command Line ---------------------------------------------------------------------- +--------- 1 Next 20 17:00 echo "go home"
    for me. But I do recall having a hard time getting certain types of quotes to not disappear w/ "at" jobs under WinNT and Perl wasn't creating the jobs.

    You could always try:

    system('at 12:00 program ""the.server.org:1234""')
    for increasing numbers of "s until it works.

            - tye (but my friends call me "Tye")
Re: Quote marks lost in system() calls
by dws (Chancellor) on Dec 21, 2000 at 04:18 UTC
    Recall that system() behaves differently with one argument than it does with multiple. Perl treats a single argument as though it were a shell command, scanning for and processing shell metacharacters. Having played with it a bit, I'm not 100% convinced that on NT it behaves as one would expect it to coming from Unix.

    Does

    system("at", "12:00", "program.exe", '"the.server.org:1234"');
    
    work any better for you?
      I tried the following on NT 4.0 with ActiveState 5.005_03.
      if ( @ARGV ) {
          foreach my $i ( 0 .. $#ARGV ) {
              print "ARVG[$i]: ", $ARGV[$i], "\n";
          }
          exit(0);
      }
      system("perl $0 foo bar");
      system('perl ' . $0 . ' "foo bar"');
      system("perl $0 foo \"bar baz\"");
      system("perl $0 foo \"\"bar baz\"\"");
      system("perl", $0, "foo", '"bar baz"');
      
      which yielded
      ARVG[0]: foo
      ARVG[1]: bar
      ARVG[0]: foo bar
      ARVG[0]: foo
      ARVG[1]: bar baz
      ARVG[0]: foo
      ARVG[1]: bar
      ARVG[2]: baz
      ARVG[0]: foo
      ARVG[1]: bar baz
      
      Unless I missed something, this suggests that you may be SOL on getting the doublequotes passed through on NT.
        This seems to behave as you desire, though I'm still not sure if its what Three-G wants (from his description, I thought its what I previously posted):
        if ( @ARGV ) { foreach my $i ( 0 .. $#ARGV ) { print "ARVG[$i]: ", $ARGV[$i], "\n"; } exit(0); } system("perl", $0, 'foo', '"\"bar baz\""'); # or this: system("perl", $0, 'foo', qq("\\"bar baz\\"")); ARVG[0]: foo ARVG[1]: "bar baz"
Re: Quote marks lost in system() calls
by runrig (Abbot) on Dec 21, 2000 at 02:49 UTC
    Have you tried this (just a guess, therefore untested)?:
    # Call system with a list instead of a string system qw(at 12:00 program.exe "the.server.org:1234");