Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I convert Data::Dumper output back into a Perl data structure?
    text
    copied!<p>I was wondering if you could shed some lights regarding the code I've been doing for a couple of days.</p> <p>I've been trying to convert a Perl-parsed hash back to XML using the XMLout() and XMLin() method and it has been quite successful with this format.</p> <pre><code>#!/usr/bin/perl -w use strict; # use module use IO::File; use XML::Simple; use XML::Dumper; use Data::Dumper; my $dump = new XML::Dumper; my ( $data, $VAR1 ); Topology:$VAR1 = { 'device' =&gt; { 'FOC1047Z2SZ' =&gt; { 'ChassisID' =&gt; '2009-09', 'Error' =&gt; undef, 'Group' =&gt; { 'ID' =&gt; 'A1', 'Type' =&gt; 'Base' }, 'Model' =&gt; 'CATALYST', 'Name' =&gt; 'CISCO-SW1', 'Neighbor' =&gt; {}, 'ProbedIP' =&gt; 'TEST', 'isDerived' =&gt; 0 } }, 'issues' =&gt; [ 'TEST' ] }; # create object my $xml = new XML::Simple (NoAttr=&gt;1, RootName=&gt;'data', SuppressEmpty =&gt; 'true'); # convert Perl array ref into XML document $data = $xml-&gt;XMLout($VAR1); #reads an XML file my $X_out = $xml-&gt;XMLin($data); # access XML data print Dumper($data); print "STATUS: $X_out-&gt;{issues}\n"; print "CHASSIS ID: $X_out-&gt;{device}{ChassisID}\n"; print "GROUP ID: $X_out-&gt;{device}{Group}{ID}\n"; print "DEVICE NAME: $X_out-&gt;{device}{Name}\n"; print "DEVICE NAME: $X_out-&gt;{device}{name}\n"; print "ERROR: $X_out-&gt;{device}{error}\n"; </code></pre> <p>I can access all the element in the XML with no problem. </p> <p>But when I try to create a file that will house the parsed hash, problem arises because I can't seem to access all the XML elements. I guess, I wasn't able to unparse the file with the following code.</p> <pre><code>#!/usr/bin/perl -w use strict; #!/usr/bin/perl # use module use IO::File; use XML::Simple; use XML::Dumper; use Data::Dumper; my $dump = new XML::Dumper; my ( $data, $VAR1, $line_Holder ); #this is the file that contains the parsed hash my $saveOut = "C:/parsed_hash.txt"; my $result_Holder = IO::File-&gt;new($saveOut, 'r'); while ($line_Holder = $result_Holder-&gt;getline){ print $line_Holder; } # create object my $xml = new XML::Simple (NoAttr=&gt;1, RootName=&gt;'data', SuppressEmpty =&gt; 'true'); # convert Perl array ref into XML document $data = $xml-&gt;XMLout($line_Holder); #reads an XML file my $X_out = $xml-&gt;XMLin($data); # access XML data print Dumper($data); print "STATUS: $X_out-&gt;{issues}\n"; print "CHASSIS ID: $X_out-&gt;{device}{ChassisID}\n"; print "GROUP ID: $X_out-&gt;{device}{Group}{ID}\n"; print "DEVICE NAME: $X_out-&gt;{device}{Name}\n"; print "DEVICE NAME: $X_out-&gt;{device}{name}\n"; print "ERROR: $X_out-&gt;{device}{error}\n"; </code></pre> <p>Do you have any idea how I could access the $VAR1 inside the text file? </p> <p>Regards,<br> newbee_me </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload