<?xml version="1.0" encoding="windows-1252"?>
<node id="821618" title="Subroutines with the same name, in different packages" created="2010-02-05 12:48:41" updated="2010-02-05 12:48:41">
<type id="115">
perlquestion</type>
<author id="742056">
mboudreau</author>
<data>
<field name="doctext">
&lt;p&gt;I can't find this situation discussed in "Programming Perl" (the camel book), "Perl Cookbook" (Christiansen &amp; 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.&lt;/p&gt;

&lt;p&gt;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).&lt;/p&gt;

&lt;p&gt;I assume lots of packages use "new" as the name of their constructor.&lt;/p&gt;

&lt;p&gt;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.,&lt;/p&gt;
&lt;code&gt;
my $nlmwriter = new UCP::NLMWriter;
$nlmwriter-&gt;writeInstance();
&lt;/code&gt;

&lt;p&gt;What am I missing?&lt;/p&gt;

&lt;p&gt;Package 1&lt;p&gt;
&lt;code&gt;
package UCP::NLMWriter;

use strict;
use vars qw(@ISA @EXPORT @EXPORT_OK $VERSION $debug);

require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(&amp;new &amp;writeInstance);
@EXPORT_OK = ();

$VERSION = 1.0;
$debug = 0;

sub new {
   # stuff here
}

sub writeInstance {
   # stuff here
}
1;
&lt;/code&gt;

&lt;p&gt;Package 2 looks nearly the same, with different code in the "new" and "writeInstance" subroutines.&lt;/p&gt;

&lt;p&gt;My test script:&lt;/p&gt;
&lt;code&gt;
#!/usr/bin/perl -w

use strict;

use UCP::NLMWriter;

use UCP::ManifestWriter;
&lt;/code&gt;
</field>
</data>
</node>
