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


In reply to Another scoping issue: How do I send 2 variables to my sub and use both. by MikeDexter

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.