nimdokk has asked for the wisdom of the Perl Monks concerning the following question:
#! /usr/local/bin/perl -w use strict; my $hero="Batman"; my $car="Pinto"; my $year=sprintf("%04d", (localtime)[5]+1900); BLOCK0:{ print "In the first block, \$hero is $hero and \$car is $car. +\n"; } BLOCK1:{ my $hero="Superman";my $car="Porsche";print "In next code blo +ck \$hero is $hero and \$car is $car.\n"; } BLOCK2:{ print "In next code block, \$hero is $hero and \$car is $car +.\n"; } BLOCK3:{ print "To start, \$year is $year\n";} BLOCK4:{ my $year=--$year; print "We change \$year to $year\n"; } BLOCK5:{ print "And now, \$year is $year\n"; } __DATA__ In the first block, $hero is Batman and $car is Pinto. In next code block $hero is Superman and $car is Porsche. In next code block, $hero is Batman and $car is Pinto. To start, $year is 2003 We change $year to 2002 And now, $year is 2002
According to everything I've seen, the last line should be: And now, $year is <b>2003</b>. Obviously, this is not the case and I am trying to figure out why this is so. I have also tried hardcoding a value into $year at the start and still get the same result. I am suspecting the decrement operator(?) is doing something to the value of $year in the main package, but what and how.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Scoping Problem
by VSarkiss (Monsignor) on Jun 25, 2003 at 18:55 UTC | |
by nimdokk (Vicar) on Jun 25, 2003 at 19:02 UTC | |
|
Re: Scoping Problem
by tcf22 (Priest) on Jun 25, 2003 at 19:04 UTC | |
|
Re: Scoping Problem
by antirice (Priest) on Jun 25, 2003 at 20:42 UTC |