Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Why " subroutined redefined" warning comes?

by sanjay nayak (Sexton)
on Nov 04, 2006 at 07:20 UTC ( [id://582220]=perlquestion: print w/replies, xml ) Need Help??

sanjay nayak has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.
  • Comment on Why " subroutined redefined" warning comes?

Replies are listed 'Best First'.
Re: Why " subroutined redefined" warning comes?
by davido (Cardinal) on Nov 04, 2006 at 07:35 UTC

    Here's an example of why that warning comes:

    use strict; use warnings; sub this { print "Hello world!\n"; } sub this { print "Goodbye world!\n"; } this();

    If you define a subroutine, and then define it again later, you get a warning. I happen to think that's a good thing. I recommend that if you want to get rid of the warning you get rid of it by not doing what it's warning you about.

    But if you really do have a good reason for defining the same subroutine twice, and you just want to squelch it, add this line in the lexical scope where the sub is being redefined:

    no warnings qw/redefine/;

    Dave

Re: Why " subroutined redefined" warning comes?
by joeface (Pilgrim) on Nov 04, 2006 at 19:10 UTC

    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.

      Obviously Spinner.pm is exporting the function into the MAIN namespace. Stop it exporting, then you can do:
      use Spinner.pm; for (1 .. 1000) { Spinner::spin(); } sub spin { print "i am the second spin() sub\n"; }
      and you don't have a duplicate. Depending on the internals of Spinner, you may be able to do:
      use Spinner();
      to stop it importing into the MAIN namespace.

      jdtoronto

        Ahhhh... learned something new, thanks to your reply.

        So, yes, I want Spinner to export the spin() function, to save me from having to do Spinner::spin(), but now I know why I have it that way, and what to change if I decide not to export it.

        You've also shown us a different way to keep the subs separate, if need be.

        Thanks!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://582220]
Approved by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2024-04-20 01:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found