I came across some unexpected behaviour and I'd be very grateful for any insight as to why this happens. The odd behaviour is illustrated by the following minimal case:
use strict; use warnings; my $tainted = shift; my $test; $test = undef; $test += $tainted; print "Test 1: $test\n"; $test = undef; $test += $tainted; print "Test 2: $test\n";
This must be run with taint checking on in order to see the issue, so for this example, save as test.pl and run as follows with a numeric argument passed in, eg:
perl -T test.pl 1
Now, as I understand it, a '+=' op is allowed on an undefined value, so I'd expect this to work fine and to leave $test with a value of 1 in each case. However, the surprising thing is, this triggers a warning because $test is undefined, but ONLY the second time round after it has first been set with a tainted value and then undefined. So here's what happens:
%perl -T test.pl 1 Test 1: 1 Use of uninitialized value in addition (+) at test.pl line 12. Test 2: 1
I tested this on 2 different Perls, both with same result: It's easy to workaround (eg. don't undef the variable, or if it needs resetting, set it to zero) but I am curious as to why this happens? Anybody got a good explanation for this?

In reply to Inconsistent warnings over undefined values by oxone

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.