in reply to Re: join this
in thread splitting command-line arguments

I'm sorry, but I guess I wasn't clear about the situation. The arguments I'm trying to parse into the array are not arguments to my script, they're arguments to what will become a child process. So my script is getting them from a file that contains entries like the one in my original post, I can easily parse out the command name, but I'm having trouble getting the argument list into a form that I could pass to, say system() for example.

Thanks for the quick responses.
Ira.

"So... What do all these little arrows mean?" ~unknown

Replies are listed 'Best First'.
Re: Re: Re: join this
by Coyote (Deacon) on May 26, 2001 at 07:48 UTC
    Do you know the format of the command lines that you will be getting from the file? If so, you can still use Getopt::Long or Getopt::Std if you localize @ARGV. Here's a bit of code I've used in the past to accomplish a similar task. This code uses Getopt::Std and the parse_cmd function returns a hash rather than an array so it doesn't meet IraTarball's exact specs, but it should be a nudge in the right direction. Here goes --

    use strict; use Getopt::Std; use Data::Dumper; sub parse_cmd{ my $cmdline = shift; local @ARGV; @ARGV = split('\s+',$cmdline); shift @ARGV; my %options; getopts("x:y:z:abc", \%options); return %options; } my %opts = parse_cmd("someprg -x foo -y bar -z baz -ab"); print Dumper(\%opts);
    Output from perl test.pl -f commandline.txt:

    $VAR1 = { 'x' => 'foo', 'y' => 'bar', 'a' => 1, 'z' => 'baz', 'b' => 1 };

    The logic would be the same for Getopt::Long or any of the other command line parsing options on cpan. Of course if you don't know what possible command line arguments are going to be passed, then you may want either look at something like Parse::RecDecent or look at the code of Getopt::Long.

    ----
    Coyote