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

Hello
I have looked at previous nodes but still don't see how to stop my error appearing

I am getting a 'can't call method 'toString' on unblessed reference'
Am I correct in saying that this perhaps is because the object I m trying to print to string is not the correct type?

I am looping through an array to print the contents to the screen
This array is compiled by adding the returned array to an array which is then looped through and printed.

The code to do this is:

foreach my $root (@docs){ foreach my $object (@$root) { print $object->toString(); } }

The array @docs is made up up XML of either
<Folder>
or
<Placemark>
These are added to the array from the subroutine which retuns either the placemark array or the folder array
if ($size eq "1"){ my @placemark = $doc->getElementsByTagName('Placemark'); if(@placemark){ @placemark = convertCoords(@placemark); } return [@folder]; } else { my @placemark = $doc->getElementsByTagName('Placemark'); @placemark = convertCoords(@placemark); return [@placemark]; }
The <Folder> elements are printed out although the <Placemark> elements are not. I am not sure why the <Placemark> elements are not a blessed reference and am happy to expand/include more code if required.

Any help would be so much appreciated.

Niall

Replies are listed 'Best First'.
Re: unblessed reference
by moritz (Cardinal) on Sep 17, 2008 at 12:21 UTC
    It specifically tells you that $object is not an object at all, despite the name of the variable ;-)

    You can print ref($object), "\n"; to see what it actually is.

Re: unblessed reference
by BrowserUk (Patriarch) on Sep 17, 2008 at 12:24 UTC
Re: unblessed reference
by kyle (Abbot) on Sep 17, 2008 at 12:25 UTC

    Whatever is in your array, @$root, are not objects at all, so they can't dispatch method calls. They're merely references. In Perl an object is a blessed reference, hence the error message, "can't call method '...' on unblessed reference". You can get better explanations of errors like this if you put use diagnostics at the top of your program.

    To have a look at what those references actually are, use Data::Dumper or some other method listed in How can I visualize my complex data structure?

Re: unblessed reference
by Anonymous Monk on Sep 17, 2008 at 12:26 UTC
    Am I correct in saying that this perhaps is because the object I m trying to print to string is not the correct type?
    No, its because you don't have an object, observe
    #!/usr/bin/perl -- use strict; use warnings; use diagnostics; use CGI(); my $cgi = CGI->new(); my $poop = []; $cgi->header; $poop->nonsense; __END__ Can't call method "nonsense" on unblessed reference at temp.pl line 13 + (#1) (F) A method call must know in what package it's supposed to run. + It ordinarily finds this out from the object reference you supply, bu +t you didn't supply an object reference in this case. A reference isn't + an object reference until it has been blessed. See perlobj. Uncaught exception from user code: Can't call method "nonsense" on unblessed reference at temp.pl + line 13. at temp.pl line 13
    If you don't know what you have, you can use Data::Dumper to find out, example
    #!/usr/bin/perl -- use strict; use warnings; use diagnostics; use CGI(); my $cgi = CGI->new(); my $poop = []; use Data::Dumper; print Dumper( $cgi, $poop ); __END__ $VAR1 = bless( { '.parameters' => [], 'use_tempfile' => 1, '.charset' => 'ISO-8859-1', '.fieldnames' => {}, 'param' => {}, 'escape' => 1 }, 'CGI' ); $VAR2 = [];
      $ua = LWP::UserAgent->new; $ua->cookie_jar ($cookie_jar={}); # instead of $ua->cookie_jar ( +{} ); $request = HTTP::Request->new('GET', "$RevAddr" ); print "\$request = ", $request, "\n"; print Dumper ($request), "\n";

      $request = HTTP::Request=HASH(0x1080540) $VAR1 = bless( { '_content' => '', '_uri' => bless( do{\(my $o = 'http://www.oracle.com/ +technetwork/java/javase/downloads/jre7-downloads-1880261.html')}, 'UR +I::http' ), '_headers' => bless( {}, 'HTTP::Headers' ), '_method' => 'GET' }, 'HTTP::Request' );

      What am I doing wrong?

        Never mind. Wrong thread