[ale] Perl parsing problem

Christopher Fowler cfowler at outpostsentinel.com
Thu Nov 10 09:55:04 EST 2005


That work good.

I'm trying to identify a better way to parse the XML via XML::Parser;
Normally I use the xml subs method but I intend to have everything
readable in one perl program.  I'm also using OOP.  So I want a package
for each noun but I do not want a package for each noun and one for xml
parsing of data for each noun.  Here is what my code looks like

----------------------------------------
  # Retrieve ENS Hostname
  my $ens_network = ENSNetwork->new($ua);
  $ens_network->load();


{
  package ENSNetwork;

  sub new {
    my ($proto, $ua) = @_;
    my $class = ref($proto) || $proto;

    my $self = {
      'server' => $ua,
      'errstr'     => undef,
    };
    bless ($self,$class);
    return $self;

  }

  sub load {
    my ($self) = shift;
    my $server = Frontier::Client->new( debug => 0,
      url => "http://$ens_ip/cgi-bin/xml-rpc",ua => $self->{'server'} );
    my $result = $server->call('get_network_config', ("0"));
    $result = MIME::Base64::decode_base64($result);
    print "$result\n";

    # Need to parse the data!

  }
}

----------------------------------------

Here is what XML data looks like when retrieved:
----------------------------------------
<?xml version="1.0"?>
<config>
  <network id="1">
    <active>enabled</active>
    <fqdn>localhost.localdomain.com</fqdn>
    <dhcp>disabled</dhcp>
    <address>192.168.2.137</address>
    <broadcast>192.168.2.255</broadcast>
    <netmask>255.255.255.0</netmask>
    <gateway>192.168.2.254</gateway>
    <nameserver1>192.168.2.254</nameserver1>
    <nameserver2>0.0.0.0</nameserver2>
    <nameserver3>0.0.0.0</nameserver3>
    <forwarding>disabled</forwarding>
  </network>
  <network id="2">
    <active>disabled</active>
    <fqdn>localhost.localdomain.com</fqdn>
    <dhcp>enabled</dhcp>
    <address>0.0.0.0</address>
    <broadcast>0.0.0.0</broadcast>
    <netmask>0.0.0.0</netmask>
    <gateway>0.0.0.0</gateway>
    <nameserver1>0.0.0.0</nameserver1>
    <nameserver2>0.0.0.0</nameserver2>
    <nameserver3>0.0.0.0</nameserver3>
    <forwarding>disabled</forwarding>
  </network>
</config>
--------------------------------------


Can I have a package within a package:

{
  package ENSNetwork;

  { 
    package ens_network_xml_subs;
  }
}

That makes reading a pain.  If that is doable how would the XML::Parser
gain access to the current object's data?  Normally I declare a hash as
readable by the whole program like this:


my %device = ()

{ 
  package xml_subs;

  sub device {
    my(undef, undef, %attrs) = @_;
    $device{'id'} = $attrs{'id'};
  }

}



On Thu, 2005-11-10 at 09:41 -0500, Philip Polstra wrote:
> 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
> 
> _______________________________________________
> Ale mailing list
> Ale at ale.org
> http://www.ale.org/mailman/listinfo/ale




More information about the Ale mailing list