expat has asked for the wisdom of the Perl Monks concerning the following question:
Here is my code:
use v5.16; use XML::Simple; my $HOME = $ENV{HOMEPATH}; my $fn = "$HOME" . "\\documents\\output.txt"; open (my $out,">$fn") or die "$fn open failed for write $!"; my @fields = qw(name medium subject url); # read in the xml from file my $site_xml = XMLin( 'C:/Users/mark/Documents/sitesout.xml', forcearray => 1 ); # print a header for the output print($out, '-' x 70, "\n"); printf ($out,"%-35s%-15s%8s%8s\n", 'name', 'medium', 'subject', 'url' +); print($out, '-' x 70, "\n"); # fetch the data, dereferencing (probably incorrectly) for my $site ( @{ $site_xml->{site} } ) { # try to print to a file printf ($out, "%-35s", $site->{ ( $fields[0] ) }->[0] ); printf ($out, "%-15s", $site->{ ( $fields[1] ) }->[0] ); printf($out, "%8s", $site->{ ( $fields[2] ) }->[0] ); printf($out, "%8s\n", $site->{ ( $fields[3] ) }->[0] ); #print to standard output #printf ("%-35s", $site->{ ( $fields[0] ) }->[0] ); #printf ("%-15s", $site->{ ( $fields[1] ) }->[0] ); #printf("%8s", $site->{ ( $fields[2] ) }->[0] ); #printf("%8s\n", $site->{ ( $fields[3] ) }->[0] ); } close $out;
I'm trying to learn something about processing XML in Perl, starting with the XML::Simple package. The entire program is in the code I've posted here. You'll see that there are 2 sections of code to print the info captured by XMLin(). One is a printf to standard output (presently commented out), the other printf to a file.
Printing to standard output gives the expected results. Printing to a file yields only 'GLOB(0x60d658)' repeated more times than I care to count, and a 0 byte file created.
I am perplexed, any pointers would be appreciated.
I'm on a Windows 7 64 bit laptop, running activestate perl v5.16
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: XML::Simple and output
by Athanasius (Archbishop) on Feb 16, 2014 at 04:30 UTC | |
by soonix (Chancellor) on Feb 16, 2014 at 16:10 UTC | |
by choroba (Cardinal) on Feb 16, 2014 at 20:14 UTC | |
by soonix (Chancellor) on Feb 17, 2014 at 08:14 UTC | |
by expat (Initiate) on Feb 17, 2014 at 01:17 UTC |