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

Oh Gurus! I am maintaining a large Perl CGI codebase dating back to '97. Recently a script that had been working for years failed with:

Undefined subroutine &main::DB2QUERY called at E:\wwwroot\cgi-bin\chargeR4.cgi line 260

&DB2QUERY is a sub that lives in a library called db2access.pli.
db2access.pli is required at the top of chargeR4.cgi like so:

require "$REQUIRE_DIR/db2access.pli";

I printed out $REQUIRE_DIR and it is the correct path.
I also printed out _PACKAGE_ and it is in fact "main".

I am asking for any debugging advise. I suspect that there is a namespace problem. Is there a way I can dump all the package names? Could DB2QUERY be in another namespace than "main"?

Btw, I am running Active State Perl 5.8.

Thanks - Dorian

Replies are listed 'Best First'.
Re: Namespace debugging advice
by MidLifeXis (Monsignor) on Jan 27, 2016 at 18:10 UTC

    Things that "working for years" don't just stop working unless something else changes (well, typically - see sunspots). Find out what else changed, bisect your changes to see what caused it to fail, etc.

    --MidLifeXis

      That's true. I have not touched the script or library in question but there are many dependencies in this code. I must have inadvertently changed another module or library that changed the namespace.
Re: Namespace debugging advice
by stevieb (Canon) on Jan 27, 2016 at 19:18 UTC

    If require isn't failing on loading the file, then I'd suspect that the sub in question has been removed from the required file, or renamed. Have you checked to see whether someone's played with that file?

    The following code snip will print out all subs currently within your namespace. Run it after the require and look for the sub in question, and see if other subs from that file have loaded properly.

    use warnings; use strict; require "file"; my @list = keys %main::; for (@list){ my $sub = "main::$_"; print "$_\n" if defined &$sub; }

    ps. If you haven't already, I'd *highly* recommend that you get all of your files checked into a version control system, so you can track this kind of thing.

      Thanks for the suggestions. Yes, I did check that the sub in question exists in the library. And I plan to set up a git repo asap.
        Paste my code above into a separate script file (replacing "file" with your actual include), run it, and see what you get. This will eliminate your other dependencies from pissing over the test. If the sub still doesn't show up, we'll have to see the code in the include file, at minimum. Essentially, you want to "halve" the problem until you figure out what you broke.