Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re: A nice little question for the insane

by dragonchild (Archbishop)
on Apr 06, 2005 at 03:05 UTC ( #445153=note: print w/replies, xml ) Need Help??

in reply to A nice little question for the insane

Don't use a regex?

Seriously, parsing something that has balanced anythings with regular expressions is notoriously hard to get right, especially if you want to be able to report back exactly where your user went wrong. They're more of a yes-or-no kind of deal.

If you want to parse a command line, it's better to use an actual parsing routine. The one in tilly's Text::xSV should be very close to what you're looking for. Actually, you could probably use Text::xSV with some very slight modifications. Well, one modification - you need to be able to specify the SEP as a regex of \s+.

  • Comment on Re: A nice little question for the insane

Replies are listed 'Best First'.
Re^2: A nice little question for the insane
by jdporter (Canon) on Apr 06, 2005 at 03:51 UTC
    That's probably the best answer, IMHO. Here's a more hackish approach:
    chomp( @a = qx( for x in $_; do echo \$x; done ) );
    There's already a tool that knows how to parse command lines! :-)

    Update: fixed bugs in code, which was untested. Thanks, tlm!

        I don't agree. It looks like what the OP is doing is sort of similar as what you expect from the shell - taking the argument string, and chopping it up into pieces. That isn't what any of the getopt tools are doing - they are taking the pieces and processing that.

        In parsing/compiling terms, getopt and Getopt::* are parsers. The OP is lexing: turning a string into tokens. And that's what the shell is doing as well (which makes the qx trick work - although the shell will parse things different than the OP does).

Re^2: A nice little question for the insane
by kelan (Deacon) on Apr 06, 2005 at 12:48 UTC

    Another option is the core module Text::ParseWords, which includes a shellwords function that likely does just the right thing.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://445153]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (8)
As of 2018-10-22 08:17 GMT
Find Nodes?
    Voting Booth?
    When I need money for a bigger acquisition, I usually ...

    Results (119 votes). Check out past polls.