Difference between revisions of "Perl XML: : Dumper"
From Teknologisk videncenter
m (→The client side) |
m (→The client side) |
||
(One intermediate revision by the same user not shown) | |||
Line 34: | Line 34: | ||
<source lang="perl" line> | <source lang="perl" line> | ||
use XML::Dumper; | use XML::Dumper; | ||
− | my $c = xml2pl( $xml ); # Making a HASH reference | + | my $c = xml2pl( $xml ); # Making a HASH reference |
my %data=%$c; # Dereferencing the hash reference | my %data=%$c; # Dereferencing the hash reference | ||
Line 48: | Line 48: | ||
loadServer:sun.tekkom.dk | loadServer:sun.tekkom.dk | ||
</source> | </source> | ||
− | + | Thats it folks. | |
− | |||
[[Category:Perl]] | [[Category:Perl]] |
Latest revision as of 11:16, 22 March 2009
XML Dumper is simple and easy to use, yet it is very versatile.
Dumping hash'es
Had some problems dumping hashes trying to send the hash between a Server and Client, but it's really simple enough.
The server side
In this example I use XML::Dumper as an object.
1 use XML::Dumper;
2 my $dump = new XML::Dumper;
3
4 my %clientorder = ( "LCnumber" => 7,
5 "LCsessions" => 10,
6 "loadServer" => "sun.tekkom.dk",
7 "loadPort" => 80);
8
9 $xml = $dump->pl2xml( \%clientorder );
10 print $xml;
prints the following
<perldata>
<hashref memory_address="0x28452d0">
<item key="LCnumber">7</item>
<item key="LCsessions">10</item>
<item key="loadPort">80</item>
<item key="loadServer">sun.tekkom.dk</item>
</hashref>
</perldata>
I really don't know how to avoid the memory_address, but I have to use the reference slash in line 9, because pl2xml only works on scalars.
Now send the entire XML output to the Client.
The client side
In this example I use XML::Dumper as function calls.
NOTE: Assuming the scalar $xml contains the xml output from the server, described above.
1 use XML::Dumper;
2 my $c = xml2pl( $xml ); # Making a HASH reference
3 my %data=%$c; # Dereferencing the hash reference
4
5 foreach my $key(keys %data) {
6 print $key.":".$data{$key}."\n";
7 }
prints the following
loadPort:80
LCsessions:10
LCnumber:7
loadServer:sun.tekkom.dk
Thats it folks.