in reply to Re^2: XML::Parser - Code Reference Error?
in thread XML::Parser - Code Reference Error?

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.

Replies are listed 'Best First'.
Re^4: XML::Parser - Code Reference Error?
by awohld (Hermit) on Jun 10, 2005 at 06:11 UTC
    So I'm trying to print from this array reference, but I'm missing something.
    #!/usr/local/bin/perl -w use strict; use CGI::Carp qw(fatalsToBrowser); use XML::Parser; use CGI; use Data::Dumper; 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->parse($address); print $tmp->[0][1];
    So I'm taking the array reference created by $parse->parse($address) and storing it in $tmp. Then I'm trying to print from the array reference but I get an error saying that $tmp isn't an array refernce.

    What am I missing?
      I'm not sure that XML::Parser is what you need here, at least not for the task you're presenting. See an example of the usage of XML::Parser here.

      Perhaps all you need is XML::Simple ?

      #!/usr/local/bin/perl -w use warnings; use strict; use XML::Simple; use Data::Dumper; my $xs = new XML::Simple(KeepRoot => 1); my $address = "<LOC><HNO></HNO><STN>Eagle Way</STN><MCN>Gotham City</M +CN></LOC>"; my $ref = $xs->XMLin($address); print Dumper($ref); # to print "Eagle way" print $ref->{LOC}->{STN};
      In "Objects"-Style, XML::Parser returns to you a tree of objects (thus the name) that represent the XML you have parsed. That is similar to DOM, but simpler.

      Have a look at the output of
      print Dumper ($tmp);
      It's a quite complex structure: Thus, to access the values you want, you have to write:
      print $tmp->[0]->{Kids}->[1]->{Kids}->[0]->{Text}, "\n", $tmp->[0]->{Kids}->[2]->{Kids}->[0]->{Text};
      which produces
      Eagle Way Gotham City
      Or in a loop:
      for ( @{$tmp->[0]->{Kids}} ) { print $_->{Kids}->[0]->{Text}, "\n"; }


      holli, /regexed monk/