{"id":45,"date":"2003-11-24T10:44:45","date_gmt":"2003-11-24T14:44:45","guid":{"rendered":"http:\/\/blogs.law.harvard.edu\/rlucastemp\/2003\/11\/24\/fix-suppress-perl-uninitialized-va"},"modified":"2003-11-24T10:44:45","modified_gmt":"2003-11-24T14:44:45","slug":"fix-suppress-perl-uninitialized-value-warnings-strongish-medicine-f","status":"publish","type":"post","link":"https:\/\/archive.blogs.harvard.edu\/rlucastemp\/2003\/11\/24\/fix-suppress-perl-uninitialized-value-warnings-strongish-medicine-f\/","title":{"rendered":"FIX: Suppress Perl &#8220;uninitialized value&#8221; warnings (strongish medicine for clueful users only)"},"content":{"rendered":"<p><a name='a26'><\/a><\/p>\n<p><P>If you have written any Perl of moderate complexity, and <EM>especially<\/EM> if your Perl of moderate complexity has included CGI and Database interactions, (or any time you have to deal with <CODE>undef<\/CODE> or <CODE>NULL<\/CODE> vs. a blank string, and you might have either of the two), you have run across warnings like this (maybe to STDERR, or maybe in your \/etc\/httpd\/logs\/error_log):<\/P><br \/>\n<P><CODE>Use of uninitialized value in concatenation (.) or string at &#8230;<\/CODE><\/P><br \/>\n<P><CODE>Use of uninitialized value in numeric gt (&gt;) at &#8230;<\/CODE><\/P><br \/>\n<P>etc.&nbsp; How can you stop these error messages (warnings, really) from blocking up your logs and your STDERR?<\/P><br \/>\n<P>In fact, you&nbsp;should be somewhat&nbsp;concerned at your <CODE>uninitialized value<\/CODE> warnings.&nbsp; After all, they are there for a reason.&nbsp; A <EM>really<\/EM> good&nbsp;piece of code ought not to generate them, at least in theory.&nbsp; However, sometimes you want the benefit of <CODE>use strict<\/CODE> and <CODE>-w<\/CODE> warnings, and you have at once good reason not to want to know about uninitialized values.&nbsp;&nbsp;What might these be?<\/P><br \/>\n<UL><br \/>\n<LI>You are doing string interpolation into&nbsp;something where <CODE>undef<\/CODE> and <CODE>&#8220;&#8221;<\/CODE> are&nbsp;equivalent for your purposes (most web page work)<br \/>\n<LI>You are doing some conditionals&nbsp;or string comparisons based upon data that come in from one crufty source or another, like CGI, and you don&#8217;t want to make a separate case for <CODE>undef<\/CODE> and <CODE>&#8220;&#8221;<\/CODE>.<br \/>\n<LI>Relative quick-and-dirtiness where you want at least <CODE>use strict<\/CODE> in order to prevent egregious code but you don&#8217;t need to hear about the semantic difference between undefined and the emtpy string.<\/LI><\/UL><br \/>\n<P>In these cases,&nbsp;if you are using&nbsp;Perl 5.6+, you are in luck.&nbsp; You&nbsp;can <EM>carefully<\/EM>&nbsp;wrap&nbsp;the <EM>specific <\/EM>section of code that has a good reason for not caring about undef values in a block (curly braces) and write:<\/P><br \/>\n<CODE><PRE>&#123;<br \/>\n  no warnings &#8216;uninitialized&#8217;;<br \/>\n  if ( CGI::param(&#8216;name&#8217;) ) &#123;<br \/>\n    print &#8220;Hello, &#8221; . CGI::param(&#8216;name&#8217;);<br \/>\n  &#125;<br \/>\n  else &#123;<br \/>\n    print &#8220;Hi there.&#8221;;<br \/>\n  &#125;<br \/>\n&#125;<br \/>\n<\/PRE><\/CODE><\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you have written any Perl of moderate complexity, and especially if your Perl of moderate complexity has included CGI and Database interactions, (or any time you have to deal with undef or NULL vs. a blank string, and you might have either of the two), you have run across warnings like this (maybe to [&hellip;]<\/p>\n","protected":false},"author":1180,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1460],"tags":[],"class_list":["post-45","post","type-post","status-publish","format-standard","hentry","category-rlucasstories"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/archive.blogs.harvard.edu\/rlucastemp\/wp-json\/wp\/v2\/posts\/45","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/archive.blogs.harvard.edu\/rlucastemp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/archive.blogs.harvard.edu\/rlucastemp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/rlucastemp\/wp-json\/wp\/v2\/users\/1180"}],"replies":[{"embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/rlucastemp\/wp-json\/wp\/v2\/comments?post=45"}],"version-history":[{"count":0,"href":"https:\/\/archive.blogs.harvard.edu\/rlucastemp\/wp-json\/wp\/v2\/posts\/45\/revisions"}],"wp:attachment":[{"href":"https:\/\/archive.blogs.harvard.edu\/rlucastemp\/wp-json\/wp\/v2\/media?parent=45"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/rlucastemp\/wp-json\/wp\/v2\/categories?post=45"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/rlucastemp\/wp-json\/wp\/v2\/tags?post=45"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}