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

I am writing some cgi scripts, and in one, I'd like to be able to run some subroutines in another given certain conditions. The first script, login.cgi, and the second, choosecats.cgi, have a few subroutines in common, and when I load choosecats.cgi like this:
require "choosecat.cgi"; &choosecat::load;

I get a warning about redefining subroutines. I know that I can say "no warnings;" at the top of choosecats.cgi to supress these warnings, but is there a better way to deal with this? Also, if I must use "no warnings", I'd like to be able to specify a condition which must be true in order for the warnings to be turned off. This conditional is useful for running the scripts on a development server versus a production server. I tried "if ($nodebug){ no warnings; }", but I get the error, the script outputs nothing. Thanks for any help you guys can give.

Replies are listed 'Best First'.
Re: subroutine redefined error when switching scripts
by Zaxo (Archbishop) on Aug 09, 2003 at 01:08 UTC

    That sounds like some refactoring is in order. Try collecting common code into namespaces with the package directive. Break each namespace into a *.pm file of its own and use them to provide the functions. You can distinguish between similarly named functions by namespace.

    Sourcing scripts with require as you do is fine, but the reorganization I suggest will take you further.

    After Compline,
    Zaxo

Re: subroutine redefined error when switching scripts
by bobn (Chaplain) on Aug 09, 2003 at 01:07 UTC

    WARNING. This "answer" is non-responsive to the quesion.

    perl is warning you for a reason - redefining a subroutine is nearly always due to error. Factor your scripts to a common set, or use modules to avoid namespace collision.

    --Bob Niederman, http://bob-n.com
Re: subroutine redefined error when switching scripts
by chromatic (Archbishop) on Aug 09, 2003 at 06:34 UTC

    The other posters are right and their solutions are better ways to accomplish what you intend to do.

    In the spirit of explaining how to do what you're asking literally, though, you can disable (or unimport) a pragma conditionally, after it's been used or required, with this idiom:

    warnings->unimport() if $nodebug;

    You'll probably have to pass the argument redefine, but again, the other posters give better alternatives to this approach.

Re: subroutine redefined error when switching scripts
by sgifford (Prior) on Aug 11, 2003 at 07:39 UTC

    If choosecat is really in its own package, and it's a different package from login, you shouldn't have any trouble. The different packages provide different namespaces, so Perl will know the subs are different. If neither declares a package, they'll both run in main, in which case something like &choosecat::load; won't work, since there's no such package as choosecat.

    If both run in package main, you can work around this by temporarily creating a new package, then loading in choosecat.cgi in that package:

    eval { package choosecat; require "choosecat.cgi"; &choosecat::test; };
    .

    That said, I agree with the other posters that refactoring your code so that functions needed by more than one program are in a seperate library is probably what you really want to do.