Trouble with @ARGV

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

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:


print "ARGV: `" . "@ARGV" . "`\n";

for my $i (0..$#ARGV) {
        print "Argument[$]: `" . $ARGV[$i] . "`\n";



First case:

ff0000@tsi00588pc:tmp$ ./ "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@tsi00588pc:tmp$ ./ \"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

Thanks a lot.

Re: Trouble with @ARGV

Quoted text here. Click to load it

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@tsi00588pc:tmp$ ./ '"a b c"'


  well, the shell places the parsed arguments in the C equivalent
  of @ARGV and whatever perl doesn't uses itself it places in @ARGV.

Re: Trouble with @ARGV

Hi Makholm,

Thanks for gonna be so fast :-)

Quoted text here. Click to load it
Yes, this works nice...
So, I expand the question: if I want to parse a command line like

$ --option=value_1,value_2="a simple string, for

Do you think it's impossible to preserve the above marked quoting?
I mean "preserve it" without double quoting it or change somewhat
to current shell setting (i don't want to force user habits :-)...


Re: Trouble with @ARGV

ff0000 wrote:
Quoted text here. Click to load it

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;

Site Timeline