How can I call a Perl package I define in the same file?

Posted by Robert S. Barnes on Stack Overflow See other posts from Stack Overflow or by Robert S. Barnes
Published on 2010-04-12T10:14:05Z Indexed on 2010/04/12 10:53 UTC
Read the original article Hit count: 154

Filed under:
|

I need to define some modules and use them all in the same file. No, I can't change the requirement.

I would like to do something like the following:

{
    package FooObj;

    sub new { ... }

    sub add_data { ... }
}

{
    package BarObj;

    use FooObj;

    sub new { 
        ... 
        # BarObj "has a" FooObj
        my $self = ( myFoo => FooObj->new() );
        ...
    }

    sub some_method { ... }
}

my $bar = BarObj->new();

However, this results in the message:

Can't locate FooObj.pm in @INC ...
BEGIN failed...

How do I get this to work?

© Stack Overflow or respective owner

Related posts about perl

Related posts about object-oriented