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

When I'm trying to parse:
<LOC><HNO></HNO><STN>Eagle Way</STN><MCN>Gotham City</MCN></LOC>
And I get an error: "Software error: Not a CODE reference at mycode.pl line 15 (my %tmp = $parse->($address);)." when I try to assign the parse to a hash. I can print the parse straight out and it works.
#!/usr/local/bin/perl -w use strict; use CGI::Carp qw(fatalsToBrowser); use XML::Parser; use CGI; my $q = CGI->new(); print $q->header(); my $parse = new XML::Parser(Style => 'Objects'); my $address = "<LOC><HNO></HNO><STN>Eagle Way</STN><MCN>Gotham City</M +CN></LOC>"; my %tmp = $parse->($address); print "$parse->($address)";
What I'm trying to do is to create a hash from the XML code.

What's this "CODE Reference" error?

Replies are listed 'Best First'.
Re: XML::Parser - Code Reference Error?
by mifflin (Curate) on Jun 10, 2005 at 04:55 UTC
    I think you meant..
    my $tmp = $parse->parse($address);
    the $parse object needs a method
    Also, the parse method seems to return a array reference, not a hash.
    use strict; use warnings; use XML::Parser; use Data::Dumper; my $parse = new XML::Parser(Style => 'Objects'); my $address = "<LOC><HNO></HNO><STN>Eagle Way</STN><MCN>Gotham City</M +CN></LOC>"; print Dumper $parse->parse($address);
    this produces..
    C:\tmp>test.pl $VAR1 = [ bless( { 'Kids' => [ bless( { 'Kids' => [] }, 'main::HNO' ), bless( { 'Kids' => [ bless( { 'Text' => + 'Eagle Way' }, 'main::C +haracters' ) ] }, 'main::STN' ), bless( { 'Kids' => [ bless( { 'Text' => + 'Gotham City' }, 'main::C +haracters' ) ] }, 'main::MCN' ) ] }, 'main::LOC' ) ];
    You might want to consider XML::Simple. It has a much simpler interface
    use strict; use warnings; use XML::Simple; use Data::Dumper; my $address = "<LOC><HNO></HNO><STN>Eagle Way</STN><MCN>Gotham City</M +CN></LOC>"; my $ref = XMLin($address); print Dumper $ref;
    This produces...
    C:\tmp>test.pl $VAR1 = { 'MCN' => 'Gotham City', 'HNO' => {}, 'STN' => 'Eagle Way' };
      I've never worked with an array reference before.

      So how would I print the 'Text'=>'Eagle Way' from that array reference?
      So it looks like this data structure is a multidimensional array? I'm having a hard time understanding it.

      What does Data::Dumper mean by 'bless'?
        my @array = (1, 2, 7, 10); my $array_ref = \@array; print $array[2]; # prints 7 print $array_ref->[2]; # prints 7 (difference is the -> )

        bless basically means that it's not just a reference, it's a reference to an object of some class.

        Read the documentation for Data::Dumper - it's a great tool to see how these complex data structures are built.