in reply to Re: use a package multiple times
in thread use a package multiple times

Alright lost in translation. Below a recreated version of the problem. Please pardon that class 1 & 2 look identical here, in code how they use the return value of myCommonLib::bar is where they differ. I'm providing 2 versions of myCommonLib.pm because I've tried it multiple ways, both produce the same error, that function class2::bar() doesn't exist.
# This is tmp.pl use strict; use warnings; push(@INC, 'c:\perl\programs'); # this is where all code lives use class1; # Note, switching order here flips the error to class 1 use class2; my $C1 = new class1; my $C2 = new class2; $C1 -> FOO; $C2 -> FOO; # This line hits the error "undefined Class2::bar" # End tmp.pl file # start class1.pm file package class1; use strict; use warnings; use myCommonLib; #Contains function bar() sub new { ... } # builds an object, blesses it etc sub FOO { bar(); } 1; # End class1.pm # start class2.pm file package class2; use strict; use warnings; use myCommonLib; #Contains function bar() sub new { ... } # builds an object, blesses it etc sub FOO { bar(); # This line errors. myCommonLib::bar(); # Produces the similiar error that # "myCommonLib::bar()" doesn't exist } 1; # End class2.pm # File myCommonLib.pm Version 1 use strict; use warnings; sub bar { print "hello world"; } # End file myCommonLib.pm Version 1 # File myCommonLib.pm Version 2 package myCommonLib; use strict; use warnings; sub bar { print "hello world"; } 1; # End file myCommonLib.pm Version 2

Replies are listed 'Best First'.
Re^3: use a package multiple times
by Anonymous Monk on Dec 20, 2010 at 14:49 UTC
    push(@INC, 'c:\perl\programs'); # this is where all code lives

    use happens at compile time, so if you want that to have an effect, you need to put it in a BEGIN block