[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[no subject]
- <!--x-content-type: text/plain -->
- <!--x-date: Thu, 10 Nov 2005 09:55:04 -0500 -->
- <!--x-from-r13: psbjyre ng bhgcbfgfragvary.pbz (Quevfgbcure Tbjyre) -->
- <!--x-message-id: [email protected] -->
- <!--x-reference: [email protected] -->
- <!--x-reference: [email protected] --> "http://www.w3.org/TR/html4/loose.dtd">
- <!--x-subject: [ale] Perl parsing problem -->
- <li><em>date</em>: Thu, 10 Nov 2005 09:55:04 -0500</li>
- <li><em>from</em>: cfowler at outpostsentinel.com (Christopher Fowler)</li>
- <li><em>in-reply-to</em>: <<a href="msg00160.html">[email protected]</a>></li>
- <li><em>references</em>: <<a href="msg00159.html">[email protected]</a>> <<a href="msg00160.html">[email protected]</a>></li>
- <li><em>subject</em>: [ale] Perl parsing problem</li>
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 => "<a rel="nofollow" href="http://$ens_ip/cgi-bin/xml-rpc",ua">http://$ens_ip/cgi-bin/xml-rpc",ua</a> => $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
> <a rel="nofollow" href="http://www.ale.org/mailman/listinfo/ale">http://www.ale.org/mailman/listinfo/ale</a>
>
> _______________________________________________
> Ale mailing list
> Ale at ale.org
> <a rel="nofollow" href="http://www.ale.org/mailman/listinfo/ale">http://www.ale.org/mailman/listinfo/ale</a>
</pre>
<!--X-Body-of-Message-End-->
<!--X-MsgBody-End-->
<!--X-Follow-Ups-->
<hr>
<ul><li><strong>Follow-Ups</strong>:
<ul>
<li><strong><a name="00164" href="msg00164.html">[ale] Perl parsing problem</a></strong>
<ul><li><em>From:</em> cfowler at outpostsentinel.com (Christopher Fowler)</li></ul></li>
</ul></li></ul>
<!--X-Follow-Ups-End-->
<!--X-References-->
<ul><li><strong>References</strong>:
<ul>
<li><strong><a name="00159" href="msg00159.html">[ale] Perl parsing problem</a></strong>
<ul><li><em>From:</em> cfowler at outpostsentinel.com (Christopher Fowler)</li></ul></li>
<li><strong><a name="00160" href="msg00160.html">[ale] Perl parsing problem</a></strong>
<ul><li><em>From:</em> ppolstra at gmail.com (Philip Polstra)</li></ul></li>
</ul></li></ul>
<!--X-References-End-->
<!--X-BotPNI-->
<ul>
<li>Prev by Date:
<strong><a href="msg00160.html">[ale] Perl parsing problem</a></strong>
</li>
<li>Next by Date:
<strong><a href="msg00162.html">[ale] Could not solve SUSE 10.0 slow performance with Asus Intel cpu/chipset</a></strong>
</li>
<li>Previous by thread:
<strong><a href="msg00160.html">[ale] Perl parsing problem</a></strong>
</li>
<li>Next by thread:
<strong><a href="msg00164.html">[ale] Perl parsing problem</a></strong>
</li>
<li>Index(es):
<ul>
<li><a href="maillist.html#00161"><strong>Date</strong></a></li>
<li><a href="threads.html#00161"><strong>Thread</strong></a></li>
</ul>
</li>
</ul>
<!--X-BotPNI-End-->
<!--X-User-Footer-->
<!--X-User-Footer-End-->
</body>
</html>