http://www.perlmonks.org?node_id=998725

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

Hello; as usual I humbly admit my lack of Perl knowledge and ask for your kind assistance. I have tried the documentation but nothing leaps out at me.

I have observed that the following (poorly styled code, duplicate subroutine names and all) compiles and runs just fine:

#/usr/bin/perl -w use strict; sub s1 { print "hi\n"; } sub s1 { print "hi 2 you\n"; } print "calling s1...\n"; s1();

OK, that seems to run the second "s1()".

Cut to a fairly large "command processor" I developed to send individual test control statements from a simple prompt to some machines we run. One of the options I have added to this tool is the ability for users to define their own Perl subroutines in an external file, and then issue a command to "include" that file in the test control tool. I suck this file in via a "require", and then the user can issue his own customized commands running his own Perl code. Works great.

My dilemma is: The base tool has dozens of subroutines, and I fear that a user will inadvertently define a sub with the same name as one of my base ones. This could be a Bad Thing. I had originally thought Perl would yell at me when I tried to "require" such a file, but apparently Perl does not mind duplicate subroutine names (thus my example above.)

I could solve this by keeping a list of all my subroutine names and then comparing them with the ones the user proposes and refusing to load his file if I pick up a duplicate. But I was wondering (out of laziness) if there was already a way for Perl to pick up on this and flag a problem on the fly, without my having to write a checker procedure.

Thanks muchly (as usually).

-Ken

"This bounty hunter is my kind of scum: Fearless and inventive." --J.T. Hutt