Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

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

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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?

    What's my password?
    Create A New User
    NodeReaper listens to Dies Irae

    How do I use this? | Other CB clients
    Other Users?
    Others musing on the Monastery: (7)
    As of 2018-04-24 15:23 GMT
    Find Nodes?
      Voting Booth?