Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

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 a.pl and in B folder i have b.pm.

I want to call a subroutine of b.pm in a.pl. But not working

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


Error can't locate pm in @INC

Comment on Directory Structure
Re: Directory Structure
by Perlbotics (Abbot) on Aug 27, 2011 at 12:35 UTC

    Try inserting

    use lib '..';
    at top of a.pl 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 (Chancellor) 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 hello.pl and myfolder folder contains package1.pm
      Both the folder are in eg folder
      hello.pl
      #!/usr/local/bin/perl
      use Data::Dumper;
      use eg::myfolder::package1;
      print @INC;
      my $package1 = new package1();
      warn "package ".Dumper($package1);
      my $prnt = package1::DESTROY();
      print "$prnt";

      package1.pm
      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';
      }
      1;
      Error :

      Can't locate eg/myfolder/package1.pm in @INC (@INC contains: C:/Perl/site/lib C: /Perl/lib .) at hello.pl line 3. BEGIN failed--compilation aborted at hello.pl 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;

        or

        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.

        --Dave

        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 Foo1.pm. 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 Foo1.pm

        #module Foo1.pm 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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (12)
As of 2014-09-01 12:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (7 votes), past polls