Trouble with @ARGV
am 09.04.2008 11:17:28 von ff0000
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@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@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
Re: Trouble with @ARGV
am 09.04.2008 11:24:46 von Peter Makholm
ff0000 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@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.
Re: Trouble with @ARGV
am 09.04.2008 11:43:26 von ff0000
Hi Makholm,
Thanks for gonna be so fast :-)
> This would work:
> ff0000@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
Re: Trouble with @ARGV
am 09.04.2008 12:37:16 von Mpapec
ff0000 wrote:
>> This would work:
>> ff0000@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;
Re: Trouble with @ARGV
am 09.04.2008 13:03:51 von ff0000
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
Re: Trouble with @ARGV
am 09.04.2008 13:04:01 von ff0000
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
Re: Trouble with @ARGV
am 09.04.2008 13:05:55 von ff0000
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
Re: Trouble with @ARGV
am 09.04.2008 13:13:19 von ff0000
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
Re: Trouble with @ARGV
am 09.04.2008 17:44:46 von Ron Bergin
On Apr 9, 4:13 am, ff0000 wrote:
> 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
Personally, I'd use a module that is designed for retrieving command
line options.
Getopt::Long
http://search.cpan.org/~jv/Getopt-Long-2.37/lib/Getopt/Long. pm
Getopt::Simple
http://search.cpan.org/~rsavage/Getopt-Simple-1.49/lib/Getop t/Simple.pm
Re: Trouble with @ARGV
am 09.04.2008 23:34:21 von ff0000
Hi Ron,
> Personally, I'd use a module that is designed for retrieving command
> line options.
> Getopt::Longhttp://search.cpan.org/~jv/Getopt-Long-2.37/lib/ Getopt/Long.pm
> Getopt::Simplehttp://search.cpan.org/~rsavage/Getopt-Simple- 1.49/lib/Getopt/Simple.pm
Yes, i know them, but i try to avoid extra modules in order to have a
script that
runs with a standard Perl installation...
However Getopt::Simple seems really too simple, while Getopt::Long
lacks some features
i need (more powerfull callback usage per option, number of value per
options, ...).
That's all! :-)
See you and thank you again.
ff0000
Re: Trouble with @ARGV
am 10.04.2008 01:19:14 von Ron Bergin
On Apr 9, 2:34 pm, ff0000 wrote:
> Hi Ron,
>
> > Personally, I'd use a module that is designed for retrieving command
> > line options.
> > Getopt::Longhttp://search.cpan.org/~jv/Getopt-Long-2.37/lib/ Getopt/Long.pm
> > Getopt::Simplehttp://search.cpan.org/~rsavage/Getopt-Simple- 1.49/lib/Getopt/Simple.pm
>
> Yes, i know them, but i try to avoid extra modules in order to have a
> script that
> runs with a standard Perl installation...
That's a very short sided and IMO a bad guideline to follow.
BTW, both of those are core modules, so they do come with the standard
Perl installation.
http://perldoc.perl.org/index-modules-G.html
> However Getopt::Simple seems really too simple, while Getopt::Long
> lacks some features
What features do you feel it lacks?
Getopt::Simple is a wrapper for Getopt::long which provides a simple
interface. Are you saying that you don't like to use clean/simple
approaches that do the job well?
> i need (more powerfull callback usage per option, number of value per
> options, ...).
You can write your own callbacks sub that do anything you want, so how
is that not powerful enough?
Re: Trouble with @ARGV
am 10.04.2008 01:23:43 von Jim Gibson
In article
,
ff0000 wrote:
> Hi Ron,
>
> > Personally, I'd use a module that is designed for retrieving command
> > line options.
> > Getopt::Long
> >
> >
> > Getopt::Simple
> >
> Yes, i know them, but i try to avoid extra modules in order to have a
> script that
> runs with a standard Perl installation...
> However Getopt::Simple seems really too simple, while Getopt::Long
> lacks some features
> i need (more powerfull callback usage per option, number of value per
> options, ...).
Getopt::Long is included with modern Perl standard installations and is
quite powerful.
I do not know what you mean by "callback usage per option" or "number
of value per options". Your original example was parsing multiple
values of a single option, e.g. --opt=value1,value2,value3.
Getopt::Long can handle this in two ways. Firstly, as shown above and
then parsed with split:
GetOptions( "opt=s" => \$opt );
@args = split(/,/,$opt);
and used thusly:
test.pl --opt=value1,value2,value3
Secondly by putting any number of option values into an array:
my @args;
GetOptions( "opt=s" => \@args );
and called thusly:
test.pl --opt=value1 --opt=value2 --opt=value3
Perhaps if you are more clear about what it is that you are trying to
accomplish, someone can recommend alternative ways to do it.
--
Jim Gibson
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Re: Trouble with @ARGV
am 10.04.2008 08:48:35 von ff0000
Hi again :-),
> I do not know what you mean by "callback usage per option" or "number
> of value per options".
Oh, my damned bad english... Sorry, I'll try to explain in a better
way.
First of all i don't use any modules because i'm learning Perl and i
want,
for example, writing my own debugging module, options parsing
module, ...
(when i'll achieve a good point, i'll move into modules' world to
compare
and surely improve my code).
You could think i'm fool... Mmmmh... So i am :-)...
> Your original example was parsing multiple
> values of a single option, e.g. --opt=value1,value2,value3.
> Getopt::Long can handle this in two ways. Firstly, as shown above and
> then parsed with split:
>
> GetOptions( "opt=s" => \$opt );
> @args = split(/,/,$opt);
>
> and used thusly:
>
> test.pl --opt=value1,value2,value3
>
> Secondly by putting any number of option values into an array:
>
> my @args;
> GetOptions( "opt=s" => \@args );
>
> and called thusly:
>
> test.pl --opt=value1 --opt=value2 --opt=value3
I see these features yet.
> Perhaps if you are more clear about what it is that you are trying to
> accomplish, someone can recommend alternative ways to do it.
Our code handles options' features such min and max arguments per
options
and options' description (for automatic help building).
So it's a bit difficult to merge Getopt::Long with our features.
I hope i give you a better explanation this time.
Bye and thanks.
ff0000