Hi, I have a file with records written in XML format and I am trying to use XML::Simple to parse the data.

However when I run the code I get the following error "Attempt to bless into a reference at /usr/local/share/perl/5.10.1/XML/Simple.pm line 128, <$xmlfh> line 22"

I dont understand the reference to the variable $xmlfh?

The code is as follows:

use strict; use XML::Simple; use Data::Dumper; #-------------# # Variables #-------------# my $logfile="c:\\FTP_Scripts\\logfile.txt"; my $numArgs= $#ARGV + 1; my $argnum; my $error_time_stamp; my $err; my $debug=0; my $xmlfile; #name of xml file including file path my $xmlfh; #xml file handle my $csvfile; #name of csv output file my $csvfh; #csv file handle my $csvdir; #csv file directory my $current_line; my @data_set; # array to hold all the records in the xml file my $dataset_id = 'REC id'; #sub-string used to identify the lines wit +h data my $current_timestamp; my $previous_timestamp; #-------------# # Main #-------------# ## Get command line parameters if($numArgs < 1){ print_usage(); exit; } if($ARGV[0]){ $xmlfile=$ARGV[0]; $error_time_stamp = geterror_time_stamp(); logit("$error_time_stamp > Input XML file: $xmlfile"); } if($ARGV[1]){ $csvdir=$ARGV[1]; $error_time_stamp = geterror_time_stamp(); logit("$error_time_stamp > Output directory: $csvdir"); } ## Open data file for reading if( open($xmlfh, '<', "$xmlfile") ){ $error_time_stamp = geterror_time_stamp(); logit("$error_time_stamp >Successfully opened file: $xmlfile"); } else{ $error_time_stamp = geterror_time_stamp(); logit("$error_time_stamp > Failed to open file: $xmlfile"); } my $result; my $line_num = 0; ## Get the lines with data, string starts with "^REC id.*" while( $current_line = <$xmlfh> ){ chomp ($current_line); # Identify the lines that begin with "REC id" $result = index($current_line,$dataset_id); if( $result == 1 ){ $data_set[$line_num] = $current_line; $line_num = $line_num + 1; } } my $xml_ref; my $xs; my $x; for($x==0; $x < $line_num; $x++){ print "\n\n$x : $data_set[$x]\n"; $xs = new XML::Simple->new(); $xml_ref = $xs->XMLin($data_set[$x]); print Data::Dumper->Dump($xml_ref); } close $xmlfh; exit 0; #--------------# # Sub-routines #--------------# sub logit { my $line=shift; open(LOG,">>$logfile") or die("$! Can't open $logfile"); if($debug){ print "$line\n"; } print LOG "$line\n"; close LOG; } sub geterror_time_stamp { my ($sec, $min, $hour, $day, $month, $yr19, @rest) = localtime(time) +; $error_time_stamp = $month . "-" . $day . "-" . ($yr19 + 1900) . " " + . $hour . ":" . $min . ":" . $sec; return $error_time_stamp; } sub print_usage{ print "\nNo files were specified,\nCorrect usage is:\n"; print "xml-csv.pl <XML input file> [output directory, optional]\n\ +n"; return; }

In reply to XML::Simple "Bless" error by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.