I am trying to write a command parser which is like cisco CLI.(still in basic level). Here is the snippet of the code.
use warnings;
use Carp;
use Parse::RecDescent;
$|=1;
my $GlobalConfigMode=q(
Interface: ('fa' |'gigabit' |'atm') module '/' slot {print "SUCCESS"}
+
module: /\d+/<commit>|<error?:invalid module >
slot:/\d+/<commit> |<error?: error near slot: >
);
my $parser= new Parse::RecDescent($GlobalConfigMode) or croak "Could n
+ot create command parser" ;
while(<DATA>){
chomp;
if($parser->Interface($_)){
print "$_ \t success\n" ;
}
else {
print "$_ Command not found\n";
}
print"------------------\n";
}
__DATA__
fa 0/1
atm 0/1
gigabit 0/1
fastethernet co
gigabit 01111
fa 0/1 enable
gigabit 0/1 disable
fastethenet asdf
Problem here is that I get different result if I take this value from STDIN .(ie changing the <DATA> in while loop to <STDIN>) Though basic parsing works, if I give additional characters it does not complain.(ie if I give input as <fa 0/1 junkcharcctrasdfadsf> from commandline)
I am wondering is there any difference in these two mode of inputs ?
Expected result is
1. fa 0/1 --> success
2. fa 0/1 enable -> fail
3. gigabit 0/1 --> success.
4. gigabit 0/1 enable/remove --> fail
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.