in reply to making a library

Actually if you are taking small steps toward an OO module then you have too much stuff in your module. You can trim it to just:

use strict; use warnings; package Foo; sub bar { print "Hello \n"; } 1;

With the driver script you showed and Foo.pm in the same directory that will print "Hello" as expected. That's it. Nothing more is required. If you copy Foo.pm to your Perl's site/lib folder it will still work and now you can use Foo.pm from anywhere.

Unless you are preparing a module for CPAN, there need be nothing more to making your library available than that.

True laziness is hard work

Replies are listed 'Best First'.
Re^2: making a library
by adieu (Initiate) on Mar 08, 2011 at 09:07 UTC
    Hi, Tried following your suggestion but the same problem persists. Foo2->bar(); ###Cant locate object method "bar" via package Foo2.

      With Foo.pm in the same directory as test.pl (assuming that is the current directory when you run test.pl) where test.pl contains:

      use strict; use warnings; use Foo; Foo->bar(); Foo::bar();

      I get:

      Hello Hello

      Using either the name space variant (Foo::bar) or the class method variant (Foo->bar) of calling bar makes no difference for this code because the "Foo" parameter passed into the class method call variant is not used.

      The important thing is that the path to Foo.pm is in @INC. To check that you could alter test.pl to:

      use Foo; print join "\n", @INC, ''; Foo->bar(); Foo::bar();

      and check that the list of paths includes the path to Foo.pm. You also need to ensure that the file name case and package case match: use Foo everywhere - not foo, FOO or some other variant, but Foo everywhere.

      True laziness is hard work
      have you changed "use Foo1" to "use Foo2" in the script that calls the subroutine? Have you changed the name of the package to Foo2 as well?
      What you are doing at the moment isn't object-oreinted code, so you should call the subroutine simply with Foo2::bar. Or just simply "bar()" because you exported "bar" from pacakge Foo2 into the local namespace of the script that uses Foo2.
        Yes sir, I did all that. I created a new module altogether called Foo2.
        Yes sir I did all that. I created a new module called Foo2. Is it coz of some problem related to strawberry?