
Stormdev
New User
May 8, 2009, 2:21 PM
Post #1 of 4
(1366 views)
|
|
Why does my array appear to be out of scope?
|
Can't Post
|
|
I have a piece of code that simply stumps me... This works:
sub htmlTimeZonePopdown { my $default = shift; my $html = ""; my @TimeZoneRegions = ( "Eastern Time Zone", "Central Time Zone", "Mountain Time Zone", "Pacific Time Zone", "Alaska Time Zone", "Hawaii Time Zone", ); $default = 0 if ($default eq ""); foreach my $offset (0..scalar(@TimeZoneRegions)-1) { my $selected = ($offset == $default) ? "selected" : ""; $html .= qq~ <option value="$offset" $selected>$TimeZoneRegions[$offset]</option>\n ~; } return $html; } But when I move the array outside of the subroutine, it stops working. In fact the array does not appear to exist at all to the subroutine. This does not work:
my @TimeZoneRegions = ( "Eastern Time Zone", "Central Time Zone", "Mountain Time Zone", "Pacific Time Zone", "Alaska Time Zone", "Hawaii Time Zone", ); sub htmlTimeZonePopdown { my $default = shift; my $html = ""; $default = 0 if ($default eq ""); foreach my $offset (0..scalar(@TimeZoneRegions)-1) { my $selected = ($offset == $default) ? "selected" : ""; $html .= qq~ <option value="$offset" $selected>$TimeZoneRegions[$offset]</option>\n ~; } return $html; } This has really got me puzzled. I'm running with -w and use strict in case that matters. Any insights? I'm running a Perl v5.8.8. Thanks in advance! |