Hello, all:

A few weeks ago, I was working on a little code and came up with a situation I didn't expect to see. It's breaking my mental model of Perl, so I'd appreciate an explanation. The situation: I have a global variable that I'm using as a loop index in a for statement. Inside the for statement, the variable takes on the expected values. But calling a subroutine that happens to use that value leaves that variable uninitialized. I've looked around a bit (such as reading perldoc perlsyn, but can't find anything in there that explains this behavior.

I've distilled the code into a simple example:

$ cat unexpected.pl #!env perl use strict; use warnings; my $S4; sub X { print "<$S4>\n"; return $S4; } for $S4 (1 .. 2) { print "A: <$S4> <", X(), ">\n"; } $ perl unexpected.pl Use of uninitialized value $S4 in concatenation (.) or string at unexp +ected.pl line 8. <> Use of uninitialized value in print at unexpected.pl line 13. A: <1> <> Use of uninitialized value $S4 in concatenation (.) or string at unexp +ected.pl line 8. <> Use of uninitialized value in print at unexpected.pl line 13. A: <2> <>

If I change the "my $S4;" to "our $S4;" it works as I'd expect it to.

It's hard to imagine that it's a perl bug, as I'd expect someone to have noticed something like this, but it's so contrary to my mental model of Perl that it's nagging the back of my brain.

Just in case it matters:

$ perl --version This is perl 5, version 26, subversion 1 (v5.26.1) built for x86_64-cy +gwin-threads-multi (with 7 registered patches, see perl -V for more detail) Copyright 1987-2017, Larry Wall Perl may be copied only under the terms of either the Artistic License + or the GNU General Public License, which may be found in the Perl 5 source ki +t. Complete documentation for Perl, including FAQ lists, should be found +on this system using "man perl" or "perldoc perl". If you have access to + the Internet, point your browser at http://www.perl.org/, the Perl Home Pa +ge.

Thanks in advance,

...roboticus

When your only tool is a hammer, all problems look like your thumb.


In reply to Why is it uninitialized? by roboticus

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.