MikeDexter has asked for the wisdom of the Perl Monks concerning the following question:
I have a sub that can fetch the network information of a host. The sub looks like this:
for my $nic (networkInfo()) { if (defined $nic->{ip}) { print ">>> IP4 Device: $nic->{device} is using: $nic->{ip +} address\n" . "\tMask: $nic->{mask}\n" . "\tBroadcast: $nic->{bcast}\n"; } if (defined $nic->{ip6}) { print ">>> IP6 Device: $nic->{device} is using: $nic->{ip6} a +ddress\n"; } }
The print lines are working, note how the print uses a $nic->{} variable. Okay, so now I want to send 2 variables into a new sub. These two variables are
$platform and $nic->{device}I have a new sub call that tries to do this but when I get into the subroutine code itself, I don't know how to access the 2nd variable $nic->{device}
Here is the code for my call and the sub so you can see what I am trying to do....
CALL
my @files = getPlatformFiles($platform, $nic->{device}); foreach my $file (@files) { print "$file\n"; }
SUB
sub getPlatformFiles { my $plat_in = shift; my @list; if ($plat_in =~ /RedHat/) { @list = ("/etc-test/resolv.conf", "/etc-test/sysconfig/network-scripts/ifcfg-$nic->{de +vice}"); } return @list; }
When I run the code I get an message telling me the following: (ignore the line numbers please)
Global symbol "$nic" requires explicit package name at ./new.pl line 1 +85. Execution of ./new.pl aborted due to compilation errors (#1) (F) You've said "use strict vars", which indicates that all variab +les must either be lexically scoped (using "my"), declared beforehand +using "our", or explicitly qualified to say which package the global var +iable is in (using "::"). Uncaught exception from user code: Global symbol "$nic" requires explicit package name at ./new.p +l line 185. Execution of ./new.pl aborted due to compilation errors. at ./new.pl line 189
Line 185 is the line in the sub itself with the $nic->{device} variable
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Another scoping issue: How do I send 2 variables to my sub and use both.
by kennethk (Abbot) on Feb 05, 2010 at 16:34 UTC | |
by MikeDexter (Sexton) on Feb 05, 2010 at 17:40 UTC | |
by kennethk (Abbot) on Feb 05, 2010 at 17:56 UTC | |
Re: Another scoping issue: How do I send 2 variables to my sub and use both.
by starX (Chaplain) on Feb 05, 2010 at 16:29 UTC | |
Re: Another scoping issue: How do I send 2 variables to my sub and use both.
by hexcoder (Curate) on Feb 06, 2010 at 13:05 UTC |