Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Comment on

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

Admittedly, I'm not the sharpest tool in the shed when it comes to packages, modules, namespaces, and OO-related Perl stuff, but I worked around the same warning earlier today.

I have a module called "Spinner.pm", that contains a subroutine called "spin()". I created another sub in the main script called "spin()". So, Perl didn't seem to know whether to use the sub in Main::, or the sub in Spinner.pm.

The script that threw the error:

use Spinner.pm; for (1 .. 1000) { spin(); } sub spin { print "i am the second spin() sub\n"; }

I got the warning: "Subroutine spin redefined at script.pl line 21". Perl then went ahead and used the spin() sub that was in the main script.

I got around this by doing the following:

use Spinner.pm; for (1 .. 1000) { spin(); spin2::spin(); } package spin2; sub spin { print "i am the second spin() sub\n"; }

By explicitly specifying package::subroutine(), Perl knew which one to use. I got the functionality of both calls, with no warnings.

So, although I agree with davido, and would advise against giving a sub the same name as one that already exists in one of your loaded modules (or in your main script), if you have to do it, you have to put the "duplicate" sub in a different package. Note that I have no idea how this would affect your variable scoping, or whatever else is going on in your script.


In reply to Re: Why " subroutined redefined" warning comes? by joeface
in thread Why " subroutined redefined" warning comes? by sanjay nayak

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 making s'mores by the fire in the courtyard of the Monastery: (9)
    As of 2014-11-27 20:55 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My preferred Perl binaries come from:














      Results (188 votes), past polls