Trouble with variable scoping
- From: romandas@xxxxxxxxx (Roman Daszczyszak)
- Date: Thu, 31 Aug 2006 15:19:25 +0200
In my perl script, I have a "global" variable called
@excludedIPAddresses, declared at the top of the script using my:
----------------------------------------
use strict;
use warnings;
use Net::Ping;
use Net::Netmask;
use Net::NBName;
use DBM::Deep;
# User-configured variable declarations
my @subnets = qw# 192.168.0.0/24 192.168.3.0/24 #; # @subnets
should only contain CIDR-type subnet address blocks
my @excludedIPAddresses = qw# 192.168.0.142 192.168.3.118 #; #
@excludedIPAddresses can only handle specific IP addresses for now
----------------------------------------
Further down, I use it in a subroutine:
----------------------------------------
sub ExcludeIPAddress
{
# Argument(s): IP Address (string) and $subnet (object reference)
# Returned: Boolean value corresponding to whether the IP should be excluded
# Globals: Uses @excludedIPAddresses (in user-config section)
my $ipAddress = shift @_;
my $subnet = shift @_;
# The next line does not work; I don't know why.
local @excludedIPAddresses = @excludedIPAddresses;
my $skip = 0;
push(@excludedIPAddresses, ($subnet->broadcast(),$subnet->base()));
$skip = grep /$ipAddress/, @excludedIPAddresses;
# Commented out original working code
# foreach my $exclude (@excludedIPAddresses)
# {
# $skip = 1 if ($ipAddress eq $exclude);
# }
return($skip);
}
----------------------------------------
When I run this, I get an error "Can't localize lexical variable". I
understand that it's because the variable is declared using "my"; what
I don't understand is why, or what I should declare the variable as,
since if I leave out "my" I get an error using "use strict".
.
- Follow-Ups:
- RE: Trouble with variable scoping
- From: Charles K. Clarkson
- Re: Trouble with variable scoping
- From: Mumia W.
- Re: Trouble with variable scoping
- From: Paul Lalli
- RE: Trouble with variable scoping
- Prev by Date: Reading whole file with while(INPUT) but I need to access each line
- Next by Date: Re: [Bulk] RE: Totally lost - need a starting point
- Previous by thread: Reading whole file with while(INPUT) but I need to access each line
- Next by thread: Re: Trouble with variable scoping
- Index(es):