roboticus has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Why is it uninitialized?
by Laurent_R (Canon) on Dec 20, 2017 at 18:50 UTC | |
by Eily (Monsignor) on Dec 20, 2017 at 21:17 UTC | |
by Laurent_R (Canon) on Dec 20, 2017 at 22:14 UTC | |
by Eily (Monsignor) on Dec 21, 2017 at 09:15 UTC | |
by poj (Abbot) on Dec 20, 2017 at 19:48 UTC | |
|
Re: Why is it uninitialized?
by hexcoder (Curate) on Dec 20, 2017 at 21:03 UTC | |
by dsheroh (Monsignor) on Dec 21, 2017 at 08:16 UTC | |
|
Re: Why is it uninitialized?
by Eily (Monsignor) on Dec 20, 2017 at 18:10 UTC | |
|
Re: Why is it uninitialized?
by pryrt (Abbot) on Dec 20, 2017 at 18:15 UTC | |
by dave_the_m (Monsignor) on Dec 20, 2017 at 18:53 UTC | |
by Laurent_R (Canon) on Dec 20, 2017 at 19:00 UTC | |
by pryrt (Abbot) on Dec 20, 2017 at 19:04 UTC | |
by Laurent_R (Canon) on Dec 20, 2017 at 19:49 UTC | |
|
Re: Why is it uninitialized?
by LanX (Saint) on Dec 21, 2017 at 00:30 UTC | |
|
Re: Why is it uninitialized?
by hdb (Monsignor) on Dec 21, 2017 at 09:03 UTC |