greatshots has asked for the wisdom of the Perl Monks concerning the following question:

puzzle guy again,

I am using 2 modules in my code

use File::Compare; use String::Compare;
both calls functions using 'compare($string,$string2) and compare($file1,$file2) and its giving error.. one returns error code whereas other returns percentage value.
how to solve this?

Replies are listed 'Best First'.
Re: problem when using two modules with same subroutine
by davido (Cardinal) on Oct 19, 2006 at 04:32 UTC

    File::Compare allows you to specifically load cmp() as an alternative to compare(). The easiest way to do it would be to simply specify in the 'use' line:

    use File::Compare qw/cmp/;

    This has two effects; first, it imports cmp(), and second, since you're specifying an import list and specifically not listing compare(), the latter will not be imported, thus resolving the namespace clash.


    Dave

Re: problem when using two modules with same subroutine
by GrandFather (Saint) on Oct 19, 2006 at 02:03 UTC

    It may be that what you want is someting like:

    use strict; use warnings; use String::Compare qw(); use File::Compare qw(); my $str1 = "fred.txt"; my $str2 = "fred1.txt"; String::Compare::compare ($str1, $str2); File::Compare::compare ($str1, $str2);

    but since you don't describe the error it's a bit hard to tell.


    DWIM is Perl's answer to Gödel
Re: problem when using two modules with same subroutine
by madbombX (Hermit) on Oct 19, 2006 at 02:03 UTC
    Try adding the actual module name to the function call since you are importing both subroutines into the same namespace (the main namespace). Something like the following should do the trick.

    use strict; use warnings; use File::Compare; use String::Compare; # tmp.pl and tmp.pl.other are copies of the same file print File::Compare::compare("tmp.pl", "tmp.pl.other"). "\n"; print String::Compare::compare("foo", "boo"). "\n"; output: 1 0.6