Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Subroutines with the same name, in different packages

by mboudreau (Acolyte)
on Feb 05, 2010 at 17:48 UTC ( #821618=perlquestion: print w/replies, xml ) Need Help??
mboudreau has asked for the wisdom of the Perl Monks concerning the following question:

I can't find this situation discussed in "Programming Perl" (the camel book), "Perl Cookbook" (Christiansen & Torkington), or "Object Oriented Perl" (Conway), but I'm afraid it's a basic OO question with an obvious answer I've just missed. I throw myself on the mercy of the Monks.

I have two packages, "NLMWriter" and "ManifestWriter", that each have subroutines with common names: "new" (a constructor) and "writeInstance" (writes out an XML fragment using data from the newly constructed object).

I assume lots of packages use "new" as the name of their constructor.

When I try to use both packages in a script, I get the "subroutine redefined" error when the second package is loaded. It seems to me that this should be possible, and I should simply have to call each subroutine with the correct package name to avoid confusion, e.g.,

my $nlmwriter = new UCP::NLMWriter; $nlmwriter->writeInstance();

What am I missing?

Package 1

package UCP::NLMWriter; use strict; use vars qw(@ISA @EXPORT @EXPORT_OK $VERSION $debug); require Exporter; @ISA = qw(Exporter); @EXPORT = qw(&new &writeInstance); @EXPORT_OK = (); $VERSION = 1.0; $debug = 0; sub new { # stuff here } sub writeInstance { # stuff here } 1;

Package 2 looks nearly the same, with different code in the "new" and "writeInstance" subroutines.

My test script:

#!/usr/bin/perl -w use strict; use UCP::NLMWriter; use UCP::ManifestWriter;

Replies are listed 'Best First'.
Re: Subroutines with the same name, in different packages
by toolic (Bishop) on Feb 05, 2010 at 17:57 UTC
    Try not exporting those 2 subs:
    @EXPORT = ();
      Indeed. To the OP: exporting OO methods serves no purpose. Exporting puts the sub into the importer's package, eg main.

      But when you call a method, you (usually) use the package name or an instance of that package, eg my $foo = Foo->new(); $foo->frob();. All the calling package (usually) needs to do is to use Foo; so that the class you're going to be using is loaded.

      In short: don't export methods.

      Try not exporting those 2 subs ... don't export methods.

      ... and if you cannot redefine what the module exports by default, suppress importation when you use the module.
          use UCP::NLMWriter ();
          use UCP::ManifestWriter ();

        Thanks, all!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://821618]
Approved by toolic
Front-paged by biohisham
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2017-08-20 06:24 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (313 votes). Check out past polls.