Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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;

In reply to Subroutines with the same name, in different packages by mboudreau

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others perusing the Monastery: (6)
    As of 2014-09-21 02:48 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (166 votes), past polls