Hi

I noticed some "uninitialized value in subroutine exit at /usr/pkg/lib/perl5/vendor_perl/5.26.0/CGI.pm line 472" warnings in our web server logs.

This was with CGI.pm version 4.36 running on "perl 5, version 26, subversion 0 (v5.26.0) built for x86_64-linux-thread-multi"

This warning was being generated by the following code in CGI.pm

sub self_or_default { return @_ if defined($_[0]) && (!ref($_[0])) &&($_[0] eq 'CGI'); unless (defined($_[0]) && (ref($_[0]) eq 'CGI' || UNIVERSAL::isa($_[0],'CGI')) # sli +ghtly optimized for common case ) { $Q = $CGI::DefaultClass->new unless defined($Q); unshift(@_,$Q); } return wantarray ? @_ : $Q; # <---- Line 472 of CGI.pm v4.36 }

I managed to track this down to some calls our code was making that were essentially:

print $cgi->textfield(-name=>"end_date", -default=>substr($pa->get_end_date(),0,10));

The $pa->get_end_date() call is part of a locally developed system that essentially queries a MySQL database. In the cases where the warning was appearing, the returned date was undefined (it was NULL in the DB)

Now, in retrospect I realise that this code was buggy and it has now been rewritten to handle undef dates. But what I don't understand, and was hoping to have explained here, is why the warning was coming out from somewhere deep within CGI.pm, rather than from the call to substr with an undefined first parameter

If I rewrite the above code to do the substr before the call to $cgi-textfield() (without also protecting it from an undef return value) I get the expected "Use of uninitialized value in substr at ..." warning.

I'd guess that its something to do with the differing contexts in which the substr is evaluated in, but a more definitive explanation would be greatly appreciated

Thanks,

Duncan


In reply to "uninitialized value in subroutine exit" warning from CGI.pm by dwmcewan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.