CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Advanced:
Why does my array appear to be out of scope?

 



Stormdev
New User

May 8, 2009, 2:21 PM

Post #1 of 4 (2077 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:


Code
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:



Code
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!

Code



      
    


FishMonger
Veteran / Moderator

May 8, 2009, 2:57 PM

Post #2 of 4 (2074 views)
Re: [Stormdev] Why does my array appear to be out of scope? [In reply to] Can't Post

I'm unable to duplicate your problem, but I see another issue. Do you see it?


Code
[root@fc4dev ~]# cat stormdev.pl 
#!/usr/bin/perl

use strict;
use warnings;

my @TimeZoneRegions = (
"Eastern Time Zone",
"Central Time Zone",
"Mountain Time Zone",
"Pacific Time Zone",
"Alaska Time Zone",
"Hawaii Time Zone",
);

my $HTML = htmlTimeZonePopdown();

print $HTML;

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;
}


Quote
[root@fc4dev ~]# ./stormdev.pl
Use of uninitialized value in string eq at ./stormdev.pl line 23.
<option value="0" selected>Eastern Time Zone</option>
<option value="1" >Central Time Zone</option>
<option value="2" >Mountain Time Zone</option>
<option value="3" >Pacific Time Zone</option>
<option value="4" >Alaska Time Zone</option>
<option value="5" >Hawaii Time Zone</option>


[edit adjusted a little error on my part]


(This post was edited by FishMonger on May 8, 2009, 3:02 PM)


Stormdev
New User

May 8, 2009, 5:07 PM

Post #3 of 4 (2065 views)
Re: [FishMonger] Why does my array appear to be out of scope? [In reply to] Can't Post

 
Funny thing, when I test it in isolation, it works fine either way, but when included with the other functions in my script, it doesn't. Literally, I can edit that larger file and simply move the array out of the sub, placing it immediately before the sub and it stops working....

I don't immediately see the other issue. I notice that you called the function without an argument and it generated the warning. Other than that, I would normally use "our" instead of "my" when declaring the array when placed outside of the sub. I also have an unneeded comma on the last array element, but I read that was perfectly accepted Perl practice.

Am I missing something?

Thanks for checking it out, by the way!


FishMonger
Veteran / Moderator

May 8, 2009, 5:34 PM

Post #4 of 4 (2062 views)
Re: [Stormdev] Why does my array appear to be out of scope? [In reply to] Can't Post

You want to declare vars with "our" only when you need to create global vars, which is not very often. 99.8% of the time you should use "my" to create lexical vars even when they're outside of subs.

You'll need to post your code so I can see why you're having this issue.


(This post was edited by FishMonger on May 8, 2009, 5:35 PM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives