Why can't my Perl object find its skip() method, even though I can call it as a subroutine?

StackOverflow https://stackoverflow.com/questions/1280225

  •  16-09-2019
  •  | 
  •  

Question

I'm working on a Perl module and whenever I call the skip() method I wrote in the following way:

$cursor->skip(4);

I get:

Undefined subroutine &MyModule::Cursor::skip called at t/tester.pl line 24.
        (in cleanup) invalid object at t/tester.pl line 24.

When I call it like:

MyModule::Cursor::skip($cursor, 4);

Perl finds it!

Oddly, if I name "skip" anything else ("skipper", "hello"), this syntax works:

$cursor->skipper(4);

I thought maybe skip() was a "secret" reserved key word or something, but I've also got methods named sort() and next() (which I know are reserved), and those work fine.

I'd really like to name this method "skip." Does anyone know why Perl can't find it?

Was it helpful?

Solution

skip() is exported from Test::More, which you might have loaded since your executable is named t/tester.pl.

What does ref($cursor) yield you? It should be a blessed MyModule::Cursor object, but the "invalid object" error might be suggesting the object was not constructed properly.

EDIT: perldiag gives another clue: "in cleanup" signifies that a problem was encountered by the object's destructor. Assuming you don't already have a destructor in the object, create a MyModule::Cursor::DESTROY method that Data::Dumps the object to see what it looks like at this time.

A concise snippet of example code that exhibits this behaviour would be very helpful.

OTHER TIPS

Without actual code, it's difficult to debug this.

Do you use MyModule::Cursor in your test code? When you replaced skip with skipper, were you calling it in exactly the same way from your test module? Are you able to use skip from a throw-away (one-liner or very short script)?

Where I'm going with this is looking for an error in your test code, rather than the module.

UPDATE: You're not doing something like declaring methods on MyModule::Cursor in two different files, are you? The error message you're getting tells me it has a blessed reference to an object of type MyModule::Cursor, so it knows about the class; but then it can't find the definition of skip. Do you happen to declare part of MyModule::Cursor in one file, and skip in another, and your test module isn't including the second file? Or do you have a syntax error somewhere around your definition of skip -- a missing semi-colon or unpaired curly brace? (But then again, why would MyModule::Cursor::skip work where $cursor->skip does not?)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top