Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Can locate when called directly, but can't when in @ISA

by Phemto (Acolyte)
on Oct 05, 2007 at 11:02 UTC ( [id://642892]=perlquestion: print w/replies, xml ) Need Help??

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

Hi there. I have two package files: Cloud.pm and Lens.pm.

Both are located in the same directory, and I'm using "use lib" to point to that directory. Lens.pm contains the code

use lib "...module_directory..."; $VERSION = 0.01; @ISA = qw(Exporter Cloud);
My main program contains the same use lib line. When I "use Cloud" from the program it finds it just fine, but when I "use Lens" I get: Can't locate package Cloud for @Lens::ISA at... There must be something obvious that I'm missing. Thinking the "use lib" line wasn't doing what I thought it was, I've also tried moving the files to my current directory so they'd be in @INC, but that didn't fix it.

I'm losing my hair fast enough as it is without this making me pull it out :)

Replies are listed 'Best First'.
Re: Can locate when called directly, but can't when in @ISA
by Gilimanjaro (Hermit) on Oct 05, 2007 at 11:44 UTC

    @ISA doesn't magically load the classes you're inheriting from. The child class Lens still needs to load (use) the parent class Cloud.

    Also, you may want to 'use strict' and 'use warnings' and prefix @ISA with 'our'.

    You can do both by using the 'base' pragma:

    use base qw(Exporter Cloud);

      You can do both by using the 'base' pragma

      ... but before switching to "use base", the OP may want to form his own opinion about the objections a couple of renowned monks have had in the thread 'base' versus @ISA, why?.

        That was helpful information, but I am no an OOP super freak either. For my needs, use base 'parent'; works perfectly and I am able to easily augment the parent's creation class with out any problems:
        use base 'My::Parent::Class'; sub new { my $pkg = shift; my $self = $pkg->SUPER::new(@_); return $self; }
      Wohoo! Was that a rookie mistake, or what? Thanks! Now I just need to figure out why some of my modules aren't returning true values, but that's a separate issue. And yes, I have a "1;" at the end. :->

      Thanks again.

Re: Can locate when called directly, but can't when in @ISA
by rhesa (Vicar) on Oct 05, 2007 at 11:40 UTC
    Are you saying that Lens->isa('Cloud') *and* Cloud->isa('Lens')? You might want to reconsider that!
      Sorry for the confusion. Lens.pm inherits from Cloud.pm, but not the other way around. I used "use Cloud" from the calling program (separate file) to test that Cloud.pm could be found in @INC, which it can. But when I try "use Lens" instead, it finds Lens, but then Lens can't find Cloud.

      Does @ISA not use @INC to find modules? That would be weird. Is this a poor system administration issue? As the SysAdmin, I could easily believe it.

Re: Can locate when called directly, but can't when in @ISA
by lyklev (Pilgrim) on Oct 07, 2007 at 14:19 UTC
    Check if both Lens and Cloud have a packagedeclaration. Blow are my working code snippets:

    Main program:

    #!/usr/bin/perl use strict; use warnings; use lib '/home/me/project/temp'; use Lens;

    In '/home/me/project/temp' are Cloud.pm:

    package Cloud; use strict; use warnings; 1;

    and Lens.pm:

    package Lens; use strict; use warnings; use lib '/home/me/project/temp'; use Cloud; our @ISA = qw(Exporter Cloud); 1;

    Apart from the recommended strict and warnings, all</code> is necessary.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://642892]
Approved by svenXY
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (7)
As of 2024-04-19 17:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found