4

I have a Perl application that use mode WWW::Scripter. It parse very huge codes and all works great, just one problem occurs that I cannot understand.

I am getting error

Can't call method "addEventListener" on unblessed reference at /usr/lib/perl5/site_perl/5.8.8/JE.pm line ...

It looks like an error in JE.pm (JavaScript Engine), which seems to be stable. This JE is called from WWW::Scripter, which seems to be also stable module.

Tracking JavaScript source I realized, that such error sometimes (not always) happens on the following part of JavaScript:

var addEvent=(function(){if(document.addEventListener){...

which seems to be correct as well.

Let's take a look at a section of JE.pm, where the error occurs >>

        my ($method, $type) = _split_meth $m;

        $proto->new_method(
          $name => defined $type
            ? $unwrap
              ? sub {
                $self->_cast(
                  scalar shift->value->$method(
                    $self->_unwrap(@_)),
                  $type
                );
              }
              : sub {
                $self->_cast(
                  scalar shift->value->$method(@_),
                  $type
                );
              }
            : $unwrap
              ? sub { shift->value->$m(
                $self->_unwrap(@_)) }
              : sub { shift->value->$m(@_) },
        );

and the "line", where such error occurs is its bottom part, so >>

            : $unwrap
              ? sub { shift->value->$m(
                $self->_unwrap(@_)) }
              : sub { shift->value->$m(@_) },

So what is wrong? What exactly error unblessed reference in this case means?

Denilson Sá Maia
  • 47,466
  • 33
  • 109
  • 111
Ωmega
  • 42,614
  • 34
  • 134
  • 203
  • Some help: `perl -Mdiagnostics -e '$foo={}; $foo->method'` – mob Mar 14 '12 at 21:17
  • [The JE module is currently alpha](https://metacpan.org/module/JE). Try producing a minimal test case provoking the error. Tiny doc, tiny script. Cut away stuff from the input doc that provokes the error to narrow it down. – Lumi Mar 14 '12 at 22:18

1 Answers1

17

An unblessed reference is one where one variable is not a legal reference to an object, but yet you're trying to call a function on it as if it was a legal object.

# perl -e '$x = {}; $x->blue();'
Can't call method "blue" on unblessed reference at -e line 1.

It's likely that $m in your above text is the function name addEventListener but shift->value isn't returning a proper object that has been "blessed". It's a way of stating what package functions should be called inside of. Here's some example code:

package Foo;

sub afunction {
    print "hello world\n";
}

package main;

my $obj = {};
bless $obj, "Foo";    # $obj is now a "Foo"
my $m = "afunction";

$obj->$m();


my $obj = {};
my $m = "afunction";

# this will crash, because we didn't bless it this time
print "about to crash\n";
$obj->$m();
print "we won't get here\n";

And running the above will produce:

hello world
about to crash
Can't call method "afunction" on unblessed reference at test.pl line 21.

Now, why your code above is resulting in an unblessed object you'd have to do lot more debugging to discover.

Adam Katz
  • 14,455
  • 5
  • 68
  • 83
Wes Hardaker
  • 21,735
  • 2
  • 38
  • 69