Meme Meme - 3 months ago 8
Perl Question

Import same named function from to same named packages

I'm trying to call a function from a package that has the same name with another package in different directory, and the functions are the same name also.

Explanation:
let's say that i have

package1.pm
that exists in
dir1/
as
dir1/package1.pm


inside it
sub fun1
is the one that i want to call

package package1;

sub fun1($$$)
{
#anything;
}


the second package is inside anothee dir:
dir2/package1.pm


package package1;

sub fun1($$$)
{
#anything;
}


suppose that the functions will take the same number of parameters.

is there any way to call the function that i want exactly?

Answer

I will suggest you to name the packages based on the directories (for eg Dir1::Package1, Dir2::Package1) on which they are; and keep them into same base (/lib in this case) directory:

/lib--
     |
      ---/Dir1--
     |        |
     |        --Dir1::Package1
     |
     |
     ---/Dir2--
              |
              -- Dir2::Package1

Then in the script you can fully qualify the subroutine name to call from those packages according to your wish:

#!/usr/bin/perl
use strict;
use warnings;
use lib qw(./lib);
use Dir1::Package1;
use Dir2::Package1;

# call from Dir1 Package1
Dir1::Package1::fun();

# call from Dir2 Package1
Dir2::Package1::fun();