|
From: ff0000 on 9 Apr 2008 05:17 Hi, I'm in trouble and doubt (is it a Perl or shell (Bash) fault? :-) while understanding the @ARGV behaviour... Here's a simple (newbie) script: -- #!/usr/bin/perl print "ARGV: `" . "@ARGV" . "`\n"; for my $i (0..$#ARGV) { print "Argument[${i}]: `" . $ARGV[$i] . "`\n"; } 1; -- First case: ff0000(a)tsi00588pc:tmp$ ./test.pl "a b c" ARGV: `a b c` Argument[0]: `a b c` The "a b c" quoting has been eating up; ok let's protect it: ff0000(a)tsi00588pc:tmp$ ./test.pl \"a b c\" ARGV: `"a b c"` Argument[0]: `"a` Argument[1]: `b` Argument[2]: `c"` Ouch! The protection has splitted the string... :-/... Is there a way (without using extra modules) to preserve quoting through @ARGV? Thanks a lot. ff0000
From: Peter Makholm on 9 Apr 2008 05:24 ff0000 <ff0000.it(a)gmail.com> writes: > I'm in trouble and doubt (is it a Perl or shell (Bash) fault? :-) It is you shell that parses the command line and places it it @ARGV, so you question is a bash question and not really a perl question. This would work: ff0000(a)tsi00588pc:tmp$ ./test.pl '"a b c"' //Makholm note) well, the shell places the parsed arguments in the C equivalent of @ARGV and whatever perl doesn't uses itself it places in @ARGV.
From: ff0000 on 9 Apr 2008 05:43 Hi Makholm, Thanks for gonna be so fast :-) > This would work: > ff0000(a)tsi00588pc:tmp$ ./test.pl '"a b c"' Yes, this works nice... So, I expand the question: if I want to parse a command line like this: $ perl_script.pl --option=value_1,value_2="a simple string, for value_2" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do you think it's impossible to preserve the above marked quoting? I mean "preserve it" without double quoting it or change somewhat relative to current shell setting (i don't want to force user habits :-)... Bye. ff0000
From: Matija Papec on 9 Apr 2008 06:37 ff0000 wrote: >> This would work: >> ff0000(a)tsi00588pc:tmp$ ./test.pl '"a b c"' > Yes, this works nice... > So, I expand the question: if I want to parse a command line like > this: > > $ perl_script.pl --option=value_1,value_2="a simple string, for > value_2" > > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ > > Do you think it's impossible to preserve the above marked quoting? > I mean "preserve it" without double quoting it or change somewhat > relative > to current shell setting (i don't want to force user habits :-)... Perhaps you could look at @ARGV as a string, and set your own parsing rules. my $aline = "@ARGV"; # .. # remove leading "--", etc. my %opt = map { split /=/ } split /,/, $aline; use Data::Dumper; print Dumper \%opt;
From: ff0000 on 9 Apr 2008 07:03 Hi, > Perhaps you could look at @ARGV as a string, and set your own parsing rules. > > my $aline = "@ARGV"; > # .. > # remove leading "--", etc. > > my %opt = map { split /=/ } split /,/, $aline; > > use Data::Dumper; > print Dumper \%opt; I can't split in such way, because i invoke this: $ perl_script.pl --option=value_1,value_2="a simple, string" but to perl arrive this @ARGV: --option=value_1,value_2=a simple, string and your dump isn't pretty good: $ ./test.pl --option=value_1,value_2="a simple, string" $VAR1 = { ' string' => undef, 'value_2' => 'a simple', '--option' => 'value_1' }; :-|... Maybe i could avoid this mess (and take me to an easy parsing) using a space separator instead of ','... Thank you all! :-) ff0000
|
Next
|
Last
Pages: 1 2 3 Prev: FAQ 8.37 How do I find out if I'm running interactively or not? Next: problem using system() |