Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

best way to handle Subroutine redefined warning

by keiusui (Monk)
on Feb 24, 2011 at 17:37 UTC ( #890026=perlquestion: print w/ replies, xml ) Need Help??
keiusui has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I am currently running the following Perl script:

#!/usr/bin/perl -T use CGI qw/:standard/; use CGI::Carp 'fatalsToBrowser'; use DBI; use strict; use warnings; print "Content-Type:text/html\n\n"; print "this is a test"; sub redirect { # code for redirect will go here } exit 1;

When I run this script, the script runs, but I receive the following error:

test.cgi: Subroutine redirect redefined at test.cgi line 13.

My guess is that there is already a subroutine called "redirect" in one of the Perl modules that I am already using.

What would be the best way to handle this warning?
1. Ignore the warning - I don't want to ignore the warning because then my error log will build up unnecessarily, and I could be ignoring a critical error message in the future.
2. Change the name of the subroutine to redirect123 - I feel like I would just be avoiding the problem rather than fixing it.
3. Don't use subroutines at all - I could do this, but I really want to learn how to use subroutines, so I don't really want to do this.

Comment on best way to handle Subroutine redefined warning
Download Code
Re: best way to handle Subroutine redefined warning
by Eliya (Vicar) on Feb 24, 2011 at 17:44 UTC

    You could wrap your routine in no warnings 'redefine' (which is lexically scoped):

    ... { no warnings 'redefine'; sub redirect { # code for redirect will go here } } # CGI's redirect() can then still be used fully qualified: # print CGI::redirect('http://somewhere.else/');

    Alternatively, don't import ":standard" functions from CGI module (which is what imports a redirect function (among many others) into your namespace), and use its OO interface instead.

Re: best way to handle Subroutine redefined warning
by ikegami (Pope) on Feb 24, 2011 at 17:47 UTC

    Change the name of the subroutine to redirect123 - I feel like I would just be avoiding the problem rather than fixing it.

    And safe driving doesn't fix car accidents? Not creating two functions with the same name is indeed the proper fix.

    It can be done by not giving them the same name, or it can be done by not creating the one you don't use.

    That you are importing functions you don't even know you are importing is the real problem as far as I'm concerned.

Re: best way to handle Subroutine redefined warning
by toolic (Chancellor) on Feb 24, 2011 at 17:54 UTC
    My guess is that there is already a subroutine called "redirect" in one of the Perl modules that I am already using.
    One way to find that out is to read the module docs. CGI makes mention of a redirect method. So you could grep through the source code, either at CGI, or at your unix command line:
    grep -w redirect `perldoc -l CGI`
    Sure enough, there is a sub redirect.

    Now, where does that warning message come from? A look at perldiag shows:

    Subroutine %s redefined (W redefine) You redefined a subroutine. To suppress this warning, say 1. { 2. no warnings 'redefine'; 3. eval "sub name { ... }"; 4. }

    Update: a closer look at CGI.pm shows that :standard exports :cgi which exports the redirect sub. An option is to only export what you need. You could change this line to not export redirect:

    use CGI qw/:standard/;
Re: best way to handle Subroutine redefined warning
by jeffa (Chancellor) on Feb 24, 2011 at 18:38 UTC

    How about 4. Don't roll your own redirect and use CGI.pm

    print redirect(-uri=>'http://somewhere.else/in/movie/land', -nph=>1, -status=>301);


    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: best way to handle Subroutine redefined warning
by ww (Bishop) on Feb 24, 2011 at 20:52 UTC

    Apologies for this mess which is rooted in my careless misreading of the terminal when I initially ran the script.

    Your script, as is (presumptively, minimized for posting See para 3), run from the CLI doesn't do what you say it does. In fact, it compiles and runs quite cheerfully, printing the 'sub redefined' message and 'Content-Type:text/html...this is a test' The sub is never called, so it can't produce the message you cite.

    Were you to include just before or after the comment in the sub, an instruction that actually does something (for example, print "inside sub redir \n";, the problem you assert would manifest itself.

    Please, test code before posting to make sure it comports well with your problem description. Yes, we advocate posting a minimal sample of your code, but NOTE that's modified by the qualifier "working." <=true but irrelevant

    We've been promised that our crystal balls will be fixed by Tuesday, but we don't know which Tuesday of which year. On the other hand, from the excellent analyses in the replies above, maybe it's just mine that's on the fritz.

    Update: I failed to see the "redefined" message between the CLI and the output produced by OP's lines 9 and 10. After reading chromatic's reply, I reran OP's script, and spotted the 'redefined' message. So I've now added acknowledgment of its appearance and have stricken my inaccurate/imprecise (eg, "wrong") statements above.
    But, since the code compiles and executes (from a W32 CLI perl -T 890026.pl, isn't the message a warning (as outlined above by toolic) rather than a compiler error?

      The sub is never called, so it can't produce the message you cite.

      The "subroutine redefined" warning is a compile-time warning. Whether that code is reachable is irrelevant.

Re: best way to handle Subroutine redefined warning
by Khen1950fx (Canon) on Feb 25, 2011 at 06:33 UTC
    Try SelfLoader. It's simple, fast, and it works.
    #!/usr/bin/perl -T use SelfLoader; use CGI qw/:standard/; use CGI::Carp 'fatalsToBrowser'; use DBI; use strict; use warnings; print "Content-Type:text/html\n\n"; print "this is a test\n"; __DATA__ sub redirect { # code for redirect will go here } exit 1;

      How does that solve the problem?

        If you don't want the redirect that's in CGI.pm, just don't import it. change
        use CGI qw/:standard/;
        to
        use CGI qw/:standard !redirect/;

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2014-10-25 23:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (149 votes), past polls