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

Hi all, i've got some warnings that i'm having difficulty tracking down. They are being generated from CGI scripts, into the error log, and look like:

Use of uninitialized value in string ne at (eval 4) line 9.

Its clear what problem the warning is telling me about, but i cant seem to find where (eval 4) line 9 is in my script. I'm guessing that it means at line 9 of a fourth embedded eval block, but i dont have any idea where to specifically find that code.

So im really looking for a good way to find this error.

Thanks

Replies are listed 'Best First'.
Re: warnings within eval{}
by liz (Monsignor) on Sep 11, 2003 at 16:47 UTC
    You could install a WARN handler. An example:
    use Carp (); $SIG{__WARN__} = \&Carp::cluck; eval { use warnings; undef =~ m#1#; }

    which gives the following output:

    Use of uninitialized value in pattern match (m//) at foo line 8.
            eval {...} called at foo line 6
    
    which gives you a little better information.

    Hope this helps.

    Liz

      I was testing that out and I realized something. It only gives you the exact line number of the error if you are using eval BLOCK, it gives much different (but still very useful) warnings if you are using eval EXPR
      use Carp (); $SIG{__WARN__} = \&Carp::cluck; eval { use warnings; undef =~ m#1#; }; $string = q{ use warnings; undef =~ m#1#; }; eval $string; __OUTPUT__ Use of uninitialized value in pattern match (m//) at temp.pl line 5. eval {...} called at temp.pl line 3 Use of uninitialized value in pattern match (m//) at (eval 1) line 3. eval ' use warnings; undef =~ m#1#; ;' called at temp.pl line 11
      That is a very handy thing to remember.

      --

      flounder

Re: warnings within eval{}
by BrowserUk (Patriarch) on Sep 11, 2003 at 17:22 UTC

    There is a nice way to work out where the errors and warnings in eval code are coming from.

    If you take a look in perlsyn#Plain-old-comments-(Not!), you'll see a feature that allows you to reset Perls idea of the current file and current line.

    If you can use this to embed such a comment into each piece of evalable code, you can have perl tell you both the piece of code and the line number within it that at which the warning or error was encountered.

    A crude demo...

    P:\test>type test.pl8 #! perl -slw use strict; my $code = q[ #line 4000 "evalcode.pl" print "hello"; print undef; ]; eval $code; P:\test>test hello Use of uninitialized value in print at evalcode.pl line 4001.

    By carefully choosing the names you give to your eval blocks (perhaps including the source file name/line number) you should be able to easily translate between the resultant error message and your code.

    It would be really nice if it was possible to retroactively add this to eval'd code in CORE and CPAN modules. It sure would save a lot of searching when the messages arise from outside of your own code. Perhaps some sort of recommendation for a standardised filename/eval block/line number to module/source line mapping could be devised?


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
    If I understand your problem, I can solve it! Of course, the same can be said for you.

Re: warnings within eval{}
by dragonchild (Archbishop) on Sep 11, 2003 at 16:37 UTC
    Find all the conditionals that use ne and check the variables they're comparing. 10-1 you're setting a variable to the result of a param() call that has a param name mispelled.

    ------
    We are the carpenters and bricklayers of the Information Age.

    The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

      That is what i'm doing, unfortunately this site is about 20000 lines of perl, but i am beginning to isloate the problem.

      I was hoping that there may be some way to get a more detailed warning, through some dash option - i'm pretty novice with eval.

Re: warnings within eval{}
by diotalevi (Canon) on Sep 11, 2003 at 18:43 UTC

    Upgrade to 5.8.1, I recall this message is nicer to work with there.

Re: warnings within eval{}
by shemp (Deacon) on Sep 11, 2003 at 17:40 UTC
    WOW, awesome suggestions all around!

    I have tracked down the problem, my homecooked CGI descendent plays with param names, and in upgrading, i screwed up in a couple places.

    Thanks much!