[ale] Perl parsing problem
Philip Polstra
ppolstra at gmail.com
Thu Nov 10 09:41:01 EST 2005
I would use split instead of what you are doing with the pattern match.
Something like this
($verb, $noun, $cmdline) = split /\s+/, $_, 3;
You can then check to see if $noun and $cmdline are defined to know how many
parameters were passed.
On 11/10/05, Christopher Fowler <cfowler at outpostsentinel.com> wrote:
>
> Perl Guru's:
>
> I'm trying to create a config shell that runs in Linux that will use
> XML-RPC to configure our embedded device. The XML-RPC thing is
> complete. What I'm working on now is parsing the command line.
>
> here is my code:
>
> --------------------------------
> sub prompt {
> print "# ";
> my $cmd = <STDIN>;
> chomp $cmd;
> $cmd =~ m/^(.+?)\s(.+?)\s(.+?)$/;
> my $verb = $1;
> my $noun = $2;
> my $cmdline = $3;
> print "[[$verb]]\n";
> return ($verb, $noun, $cmdline);
> }
> -------------------------------
>
> $1, $2, and $3 substitutions fail when there is only one verb like
> 'exit'. Verbs can be 'show, set, add, delete, or exit'. Nouns can be
> 'port, user, vtun, snmp, etc.' So to show port 1's config I use 'show
> port 1' To show all ports I use 'show port'. I guess I could simply
> use split and split on \s boundaries. for $cmdline I need to be
> smarter. Some items have descriptions that can include a space and I
> will want to cut $cmdline into arguments.
>
> 'set system location "Buford Development"'
> verb = set
> noun = system
> arg[0] = location
> arg[1] = 'Buford Development'
>
> So I'm trying to do shell like parsing. I bet there is a module that
> does all this for me. If not can someone point me in the best
> direction.
>
> Thanks,
> Chris
>
>
> _______________________________________________
> Ale mailing list
> Ale at ale.org
> http://www.ale.org/mailman/listinfo/ale
>
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Ale
mailing list