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

Hi,
I have been trying to implement the SelfLoader module in my script. I would like to know what is the mistake I am doing. Any suggestion would be welcome.
Thanks
bayruds
In my script
------------
use SelfLoader; sub sub1; use ModuleA; my $val = new ModuleA;
In the moduleA
-------------
package ModuleA; use SelfLoader; sub new; sub A1; use ModuleB __DATA__ sub new { } sub A1 { }
------------
Undefined subroutine &ModuleA::new called at script.pl line 4 (#1) (F) The subroutine indicated hasn't been defined, or if it was, it has + since been undefined.
------------

janitored by ybiC: Balanced <code> tags around codeblock only, as per Monastery convention

Replies are listed 'Best First'.
Re: SelfLoader.pm module
by tilly (Archbishop) on Aug 25, 2004 at 01:28 UTC
    The code that you gave does not run because ModuleA does not return a true value. And because ModuleB is mssing.

    After I fix those obvious problems, then the code that you present runs just fine.

    Please try to cut down the original code to as short a piece of non-working code as you can, and verify that it still doesn't work and then reply with that. (The odds are very good that in the process of doing that, you'll find and fix your problem. But that's how life is.)

      Hi Tilly, I have ModuleB included in the code as another file. I still get th +e following error: Undefined subroutine &ModuleA::new called at script.pl (F) The subroutine indicated hasn't been defined, or if it was, it has + since been undefined. The moduleA sub new returns the object. Is this causing the error? I u +sually end the module with __END__. Please let me know. Bayruds
        You seem unclear on something basic.

        You posted sample code that clearly is not the code that gives you the error. Now you're asking me to tell you what's wrong. That's like giving a working car to a mechanic and asking him to look at it and fix the broken one you left in your garage. He can't because he isn't psychic - he can't tell what's wrong with the one in your garage without seeing it.

        Unless you show us code that fails, nobody can do more than guess. Until you have a simple test case that actually shows your problem, there is little point in asking for help. Because nobody here is psychic, we can't know what's wrong with your real code because we haven't seen it.

        Now you just made a couple of guesses. Let me address them.

        You wonder whether it is a problem for new to return the object. Nope, that can't cause a problem. Perl told you that the function is not even found. Don't worry about what it does.

        You ask whether the presence of __END__ might be a problem. Maybe. If you put __END__ between __DATA__ and the subroutine definition, then SelfLoader won't see the subroutine. That is what the documentation for SelfLoader is trying to explain when it says, The SelfLoader will stop reading from "__DATA__" if it encounters the "__END__" token - just as you would expect. If I saw your real code I could tell you immediately whether that is your problem.

        I can guess as well. My first guess is that, through a typo or otherwise, ModuleA does not define new. Check the function name. Also try to comment out __DATA__ and see if ModuleA compiles. My second guess is that you have two versions of ModuleA, and the one that you're actually loading is not the one that you think you are. You can check this by dumping %INC after you load ModuleA.

        I have a lot of experience. I'd give maybe even odds that one of those guesses is right. If I had a code sample that demonstrates the problem, I'd have close to 100% odds of being able to tell you exactly what is wrong. That's a lot better than guessing.

        Going the other way, you can start with a simple example and slowly expand it into the real one. In your script:

        use ModuleA; my $val = ModuleA->new();
        In ModuleA:
        package ModuleA; use SelfLoader; 1; __DATA__ sub new { my $class = shift; bless { @_ }, $class; }
        You can start with that and slowly tinker to make it match what you want. Add a function. Run it. Add another. Run it. Edit 3 lines. Run it. See what point it breaks at.
Re: SelfLoader.pm module
by jdalbec (Deacon) on Aug 25, 2004 at 01:29 UTC
    I don't think you want __DATA__. That's for in-stream text input. You want sub new {... to be treated as Perl code, so just take the __DATA__ line out.

    Sorry, guess I should check the docs before replying.