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);
| [reply] [d/l] |
|
| [reply] |
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
| [reply] [d/l] |
|
@ARGV should be a read_only thing for a user.
This raises several questions.
- What has the ability to write @ARGV to do with the problem?
- Why should it be read_only? Why isn't it read_only if it should?
- Who's the user in this case? The programmer? The person running the code?
| [reply] |
|
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.
| [reply] |
|
|
|
|
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
| [reply] [d/l] |
|
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!
| [reply] |
|
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.
| [reply] |
|
|
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.
| [reply] |
|
Re: How to escape white space in command line arguments
by Anonymous Monk on Apr 13, 2010 at 06:05 UTC
|
my( @args ) = ( $^X, "one", "two" );
system( { $args[0] } @args ) == 0
or die "system @args failed: $?"
| [reply] [d/l] |
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.
| [reply] [d/l] |