http://www.perlmonks.org?node_id=834408

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

Hello,

I'm trying to call a Perl script from another passing few arguments. One of my arguments has a path variable which has white spaces embedded in it. I know how to escape the white space when I know the value of the variable in advance. But how do I escape the white spaces in a scalar variable that's being passed as an argument to a script called inside another script.

For instance, we could escape the white space and read $path as one whole string till the end

 my $path = "C:/\"Program Files\"/\"Perl Express\"/example.txt;

Now, I'm trying to pass one such $path variable and a hash as arguments to a script.

@ARGV = ("C:/Program Files/Perl Express/sample.txt", "some hash"); # I + dunno the path value in advance. This is just an example system("C:/Perl/bin/perl.exe /path/to/the/script @ARGV");

When I try to print this ARGV in the other script, it reads C:/Program and Files/Perl as first and second arguments.

Is there a way that I could escape such white spaces?

Thanks,

Replies are listed 'Best First'.
Re: How to escape white space in command line arguments
by repellent (Priest) on Apr 13, 2010 at 06:26 UTC
      Is there a way that I could escape such white spaces?

    Yes, by avoiding the escape effort.

    Keep arguments separate by maintaining them in a list - don't interpolate the array into the string:
    my @args = ("C:/Program Files/Perl Express/sample.txt", "some hash"); system($^X, "/path/to/the/script", @args);
      Hi,

      That works. I was passing the @args in double quotes and that's precisely was the mistake.

      Thanks for helping me out.

      Thanks,
Re: How to escape white space in command line arguments
by Marshall (Canon) on Apr 13, 2010 at 06:50 UTC
    On Windows, you just double quote command line args that have spaces. @ARGV should be a read_only thing for a user. 2 simple Perl scripts and how they can interact:
    #!/usr/bin/perl -w use strict; #file: testargv.pl my $parms = 'a b c abc "some gizmo" d32'; print `perl argv.pl $parms`; __END__ prints a b c abc some gizmo d32 ===== file argv.pl ======== #!/usr/bin/perl -w use strict; #file: argv.pl foreach (@ARGV) { print "$_\n"; } ==example of command line input to argv.pl ==== C:\TEMP>perl argv.pl "some thing" "some other thing" d x "xy z" some thing some other thing d x xy z
      @ARGV should be a read_only thing for a user.
      This raises several questions.
      1. What has the ability to write @ARGV to do with the problem?
      2. Why should it be read_only? Why isn't it read_only if it should?
      3. Who's the user in this case? The programmer? The person running the code?
        Well I just saw this from the OP:
        @ARGV = ("C:/Program Files/Perl Express/sample.txt", "some hash");

        @ARGV is a special Perl variable that contains the command line arguments. A similar thing exists in other languages. In 'C' you also get argc which is the count of the argv strings, e.g. int main(int argc, char **argv), but argc is redundant because argv is a null terminated array of pointers to strings and therefore calculating argc is trivial. Perl uses @ARGV for the purpose of passing command line args and the scalar value of @ARGV is what 'C' calls argc.

        Nobody but the O/S should set @ARGV.

Re: How to escape white space in command line arguments
by Marshall (Canon) on Apr 13, 2010 at 10:45 UTC
    my $path = "C:/\"Program Files\"/\"Perl Express\"/example.txt;

    This should be re-written. There is no need to "escape" double quotes - the whole path name should be in quotes, this \" doesn't make sense. Under Windows Perl, the following Perl $path name is completely valid:

    #!/usr/bin/perl -w use strict; my $path = "C:/Program Files/Perl Express/example.txt"; #there is no need to "escape" the double quotes. print "$path\n"; my $root = "C:/Program Files/Perl Express"; my $somefile = "X Y Z.dat"; my $somefile_path = "$root/$somefile"; print "$somefile_path\n"; __END__ prints: C:/Program Files/Perl Express/example.txt C:/Program Files/Perl Express/X Y Z.dat
      Hi,

      Yes, you're right. For some reason, it wasn't working before so I chose to escape those white spaces which turned out to be totally unnecessary later.

      I really appreciate your response.

      Thanks,

      Off the topic, could someone please tell me if I need to close the thread in this forum? I read the FAQs and I couldn't find any related posts there. I just want to make sure that I follow the forum rules. Thanks!

          Off the topic, could someone please tell me if I need to close the thread in this forum?

        There is no need to. This is a good thread that will, in the future, help people who come by it.
        GoForIt, I'm glad that you got some help from this thread and are able to proceed with your program! Hurray!

        This @ARGV business just means that this is a reserved array for Perl and normally you shouldn't assign to it or grow it by a push or whatever. I've never seen a 'C' or Perl program that did that, but of course "never" is a very long time!

        Perl normally "consumes" items from @ARGV via shift. The equivalent analog in 'C' is argv++. Getopts essentially works this way. Both of these operations make @ARGV smaller. There are also analogous operations that can consume the "rightmost" argument from the command line.

        JavaFan and I are probably in what I would call "aggressive agreement" - there isn't any real difference except that somehow the words via text seem to be in juxtaposition.

Re: How to escape white space in command line arguments
by Anonymous Monk on Apr 13, 2010 at 06:05 UTC
    perldoc -f system
    my( @args ) = ( $^X, "one", "two" ); system( { $args[0] } @args ) == 0 or die "system @args failed: $?"
Re: How to escape white space in command line arguments
by nvivek (Vicar) on Apr 13, 2010 at 06:35 UTC
    If you want to escape the spaces in the string of an array.You try the following,
    my %hash=(1=>"one"); my $str='C:/Program Files/Perl Express/sample.txt'; $str=~s/ /\\ /g; @ARGV = ($str,%hash); system("./script.pl @ARGV");
    In the script.pl,I printed the array @ARGV. It printed the Path and hash keys and values correctly.