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

Basics: I'm using mod_perl and getting a 502 error on one file

If I don't use strict, then it passes through fine, with nothing other than my own printed errors in the log

However, if I do use strict, the script fails in the most vital place (I'm positive all the variables are instantiated and have correct values)

require "preload/$fileHandle\_build2.pl"; print &$conBuild($data,$file,$num,$ty,1);

Basically I'm dynamically setting the function name after dynamically requiring a file.

This is the error:

Can't use string ("rss_function") as a subroutine ref while "strict refs" in use at widget_load.pl line 120.

It seems that strict doesn't like dynamically setting function names, its just mod_perl doesn't seem to like to run any of my scripts unless they're strict. Cheers!

Replies are listed 'Best First'.
Re: Mod_perl strict problem
by Errto (Vicar) on Apr 07, 2007 at 17:48 UTC

    It is true that strict doesn't like dynamically setting function names, also known as symbolic references. The usual answer is to create some kind of table to track references to your subroutines. In other words, rather than saying:

    $conBuild = "rss_function";
    or the equivalent you could say
    $conBuild = $funcTable{rss_function};
    where %funcTable is defined elsewhere in some manner like
    %funcTable = ( .... rss_function => \&actual_rss_function, ... );
    See perlref for more on coderefs, as these are called. If for some reason this won't work and you really need to use a string as the subroutine name, then you will need to turn off strict for that part of your code. The cleanest way to do this is to use a block around just the part where the symbolic reference is used:
    ...other code... { no strict 'refs'; print &$conBuild($data,$file,$num,$ty,1); } ...other code...
    This way strict will be turned off in only the one place you need it.

    By the way, while using strict is a good idea in general, I can't think of any reason why mod_perl would force you to use strict. Can you shed some more light on that?

      Interesting, when you said that mod_perl doesn't force strict, I commented out that part of the script and it prints in SSH with no errors, and no errors in the log, still sending me to this darn 502 page.
        A 502 is a proxy problem. Are you using a proxy in front of the server? What happens if you access the server directly? Maybe it's not sending the output you think it is.