Re: parsing command line args




Ben Morrow wrote:
Quoth "badarisj@xxxxxxxxx" <badarisj@xxxxxxxxx>:
folks,

when i give a command-line of:
/tmp/test.pl -arg1 -arg2 -arg3 'value with spaces'
to unix-shell, my perl script has access to the command-line arguments
in the following neat array:

0 ARRAY(0x12b06c)

I'm not quite sure what you intended to mean by this line: for a start,
@ARGV certainly doesn't have two 'element 0's.

hi ben,

that was ouput from my perl debugger. sorry for confusing you.


0 '-arg1'
1 '-arg2'
2 '-arg3'
3 'value with spaces'

now if i have a string for the command-line:

$cmd_str = "/tmp/test.pl -arg1 -arg2 -arg3 'value with spaces'" ;

any ideas on parsing the $cmd_str into a @ARGV array i have shown
above?

The Text::ParseWords module will do this for you. I was fairly sure this
was in the FAQ, but a couple of reasonable searches didn't find it, so
I'm not terribly surprised you didn't either :).

perfect. just tested it and it works. The Text::ParseWords module
has the routine 'shellwords' which mentions that it emulates
parsing of the command-line by many unix shells ( which is exactly
what i was looking for).

thanks,
-badari


Ben

--
"If a book is worth reading when you are six, * benmorrow@xxxxxxxxxxxxx
it is worth reading when you are sixty." [C.S.Lewis]

.



Relevant Pages

  • Re: parsing command line args
    ... my perl script has access to the command-line arguments ... in the following neat array: ... any ideas on parsing the $cmd_str into a @ARGV array i have shown ...
    (comp.lang.perl.misc)
  • Re: Perl script finds module in Terminal but not as CGI?
    ... Why would Perl script on the command-line find it but not a CGI executable? ... installed Date::Manip so my normal bash shell is picking it up. ... Got to rewrite a bit of code now (rather have it portable than tied in to only Macs with Fink). ...
    (uk.comp.sys.mac)
  • parsing command line args
    ... to unix-shell, my perl script has access to the command-line arguments ... in the following neat array: ...
    (comp.lang.perl.misc)
  • Re: parsing command line args
    ... to unix-shell, my perl script has access to the command-line arguments ... in the following neat array: ...
    (comp.lang.perl.misc)
  • Desparate, tired programmer -- bug in perl5.6.1/apache1.3.31 ?
    ... anyone know why a Perl script that runs perfectly from the command-line ... will silently refuse to write to its data file when called ... from PHP using the systemcommand? ...
    (comp.lang.perl.misc)