Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Directory Structure

by prakash1987 (Initiate)
on Aug 27, 2011 at 11:52 UTC ( #922810=perlquestion: print w/replies, xml ) Need Help??
prakash1987 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I have to folders A and B. Both folder are parallel to each other. In A folder i have and in B folder i have

I want to call a subroutine of in But not working

use B::b;
my $package1 = new package1();
print $package1->abc();

Error can't locate pm in @INC

Replies are listed 'Best First'.
Re: Directory Structure
by Perlbotics (Chancellor) on Aug 27, 2011 at 12:35 UTC

    Try inserting

    use lib '..';
    at top of if your start this program in directory A. It might be useful to add the absolute path - if possible. See lib.

    new package1() doesn't look right. One would have expected something along my $obj1 = B::b->new(); ? Furthermore, unless b is a pragma, it is common to use capitalised module names (here: B::B, see perlmod).
    But maybe this is just a problem of the example?

Re: Directory Structure
by toolic (Bishop) on Aug 27, 2011 at 12:39 UTC
    You should post your exact error message and use code tags.

    You need to add the full directory containing directory B to @INC:

      I am giving exact code and error
      I have two folders one is hello and second one is myfolder. Both the folder are parallel like A and B
      . Folder hello contains and myfolder folder contains
      Both the folder are in eg folder
      use Data::Dumper;
      use eg::myfolder::package1;
      print @INC;
      my $package1 = new package1();
      warn "package ".Dumper($package1);
      my $prnt = package1::DESTROY();
      print "$prnt";
      package package1;
      sub new {
      my($class, $name) = @_; # Class name is in the first parameter
      my $self = { name => $name }; # Anonymous hash reference holds instance attributes
      bless($self, $class); # Say: $self is a $class return $self;

      sub DESTROY {
      return 'package2';
      Error :

      Can't locate eg/myfolder/ in @INC (@INC contains: C:/Perl/site/lib C: /Perl/lib .) at line 3. BEGIN failed--compilation aborted at line 3.

        Your 'package' name doesn't match your 'use', first of all.

        use eg::myfolder::package1; package package1;

        should be

        use eg::myfolder::package1; package eg::myfolder::package1;


        use package1; package package1;

        In either case, you should either execute your script from within the parent directory of the first level of the package name, or have that directory in @INC (via -I, 'use lib', etc). The parent directory of "eg", or the parent directory of "package1" (myfolder), depending on the name you choose. 'print @INC' isn't helping because your script fails to compile, but Perl prints @INC helpfully in your compilation error anyway.

        Writing Modules has a similar issue, with good references to go read on this topic and other issues you'll run into fairly quickly once you get past this one.


        Do not tell lies. You are not giving the exact code nor the exact error. You have "sanitised" both to the point that they do not provide the information we require to provide informed help that directly addresses the problem you are having. There are a multitude of possible mistakes that could result in the type of error you describe. While we can, and do, guess which mistakes are most likely, we can't give a definitive answer if you give us inaccurate fuzzy information.

        Do you want help? If so, give us good information!

        True laziness is hard work
Re: Directory Structure
by zentara (Archbishop) on Aug 27, 2011 at 15:10 UTC
      And end up with programs that only work from B, and its sibling directories?
        And end up with programs that only work from B, and its sibling directories?

        From my reading of the OP's original post, he wanted a relative program and module structure. This works for an A sub dir holding the program Foo-use, and a parallel level module dir B, holding a module The relative nature of the directories means they can be installed anywhere together, and the module in B will be found.

        Make 2 subdirs anywhere in an arbitrary directory, named A and B

        In A, the program:

        #!/usr/bin/perl use lib "../B"; use Foo1 foo; use Tk; # put in to show that @inc is still working fine foo(); print "Goodbye\n";

        In the parallel B module directory, the module

        #module located in B package Foo1; require Exporter; @ISA = qw|Exporter|; @EXPORT_OK = qw|foo|; sub foo { print "Hello World, I'm here\n"; } 1;

        Run it:

        $ ./A/Foo-use Hello World Goodbye

        I'm not really a human, but I play one on earth.
        Old Perl Programmer Haiku ................... flash japh
Re: Directory Structure
by Anonymous Monk on Aug 28, 2011 at 00:11 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://922810]
Approved by Corion
[Eily]: oh no, how am I going to answer to that prince who wanted to leave me all this money?
[Eily]: and all those mails to enlarge my
[Eily]: inbox
[marto]: I'm happy to be the middle man :P
LanX found a bone xylophone for node reaper massage
[Veltro]: NodeReaper did not notice messages from Klaas Vaak, lucky Eily...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (12)
As of 2018-05-23 14:41 GMT
Find Nodes?
    Voting Booth?