Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

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; }

Replies are listed 'Best First'.
Re: XML::Simple "Bless" error
by chromatic (Archbishop) on Nov 20, 2010 at 03:57 UTC

    This line looks wrong to me:

      $xs = new XML::Simple->new();

    I haven't debugged further, but start by fixing that one.

Re: XML::Simple "Bless" error
by PeterPeiGuo (Hermit) on Nov 20, 2010 at 04:28 UTC

    The following code can reproduce the error you saw:

    package foo; sub new { return(bless({}, shift)); } foo->new()->new(); #extra new!

    Peter (Guo) Pei

Re: XML::Simple "Bless" error
by 7stud (Deacon) on Nov 20, 2010 at 22:11 UTC

    perl provides two syntaxes for calling new() (or any other method):

    #1) The proper way: my $obj = Class::Name->new(); #Note the arrow followed by the method name.

    and:

    #2) The way that may work (i.e. the improper way): my $obj = new Class::Name; #Note that the method name comes first and there is no arrow.

    Use the proper way.

    In no circumstances should you combine the two syntaxes into:

    my $obj = new Class::Name->new();