parsing command line args

B

badarisj

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)
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?

thanks,
-badari
 
B

Ben Morrow

Quoth "[email protected] said:
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.
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 :).

Ben
 
T

Tad McClellan

Ben Morrow said:
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.


That's what caused me to not attempt an answer when I saw it.

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,


perldoc -q split

How can I split a [character] delimited string except when inside [character]?
 
B

badarisj

Ben said:
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.
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top