Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: OO base class lookup failing

by Somni (Friar)
on Oct 16, 2007 at 03:24 UTC ( [id://645106]=note: print w/replies, xml ) Need Help??


in reply to OO base class lookup failing
in thread OO base class lookup failing

This is what I meant by reducing the code to the minimum necessary to exhibit the problem:

use warnings; use strict; { package ONE; sub _field_one { print "In ONE::f\n" } } { package THREE; our @ISA = qw(ONE); sub new { bless {}, $_[0] } sub __handle_key { my $f = \&_field_one; $_[1]->_field_one(); return 1; } } package main; my $t = THREE->new(); print "Toplevel: "; $t->_field_one(); $t->__handle_key($t);
This was accomplished by cutting out whole swathes of code, then running the program to verify the error was still apparent. If it was, continue cutting, if it wasn't back off some of the edits and try again. This can be a valuable technique if you truly don't understand what's going on.

What I discovered is that the subroutine reference, \&_field_one, was important. Removal of that would make the error go away. Tk wasn't even involved, and could be removed from the equation entirely. In fact, the code can be reduced even further:

{ package Bar; sub frob {} } { package Foo; use base qw(Bar); \&frob; } Foo->frob();

The problem is, apparently, that when you take a reference to a subroutine that did not already exist the symbol table is updated. This causes a method lookup to find a Foo::frob subroutine, but there is no actual code for this subroutine, so the method call ultimately fails.

The simple solution is, don't take references to subroutines that don't exist in your local namespace. You take a reference to THREE::_field_one and store it in @single_key_functions, but there is no such subroutine. This was an error on your part, I suspect.

Incidentally, had you gone over to using strings (i.e. $func = "_space"; $s->$func($key_event, $key_code)) as I originally suggested this error would have disappeared, as you would have removed the \&_field_one from @single_key_functions.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://645106]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (2)
As of 2025-07-18 01:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.