in reply to Re: Data::Dumper is returning empty
in thread Data::Dumper is returning empty

Good explanation, but the conclusion is backwards. Line 10 and 28 are correct; it's line 5 that's wrong. There are multiple nics, so there should be multiple hashes. The real problem is that the function pretends there's only one. It should be returning a list of nics (list of hashes).

sub networkInfo { my @nics; my $platform = getPlatform(); if ($platform =~ /RedHat/) { my $ifconfig = qx|/sbin/ifconfig| or die("Can't get info from Linux ifconfig: $!\n"); for (split /(?<=\n)(?=\w)/, $ifconfig) { my %nic; ($nic{device}) = /^(eth\d)\s/ or next; if (/\binet addr:([\d.]+)\s.+?:([\d.]+)\s.+?:([\d.]+)/) { $nic{ip} = $1; $nic{bcast} = $2; $nic{mask} = $3; } if (/^\s+ inet6 addr:\s*(\S+)/m) { $nic{ip6} = $1; } push @nics, \%nic; } } elsif ($platform =~ /SunOS/) { my $ifconfig = qx|/sbin/ifconfig -a| or die("Can't get info from Solaris ifconfig: $!\n"); for (split /(?<=\n)(?=\w)/, $ifconfig) { my %nic; ($nic{device}) = /^(fjgi\d)\s/ or next; if (/\binet addr:([\d.]+)\s.+?:([\d.]+)\s.+?:([\d.]+)/) { $nic{ip} = $1; $nic{bcast} = $2; $nic{mask} = $3; } if (/^\s+ inet6 addr:\s*(\S+)/m) { $nic{ip6} = $1; } push @nics, \%nic; } } return @nics; } for my $nic (networkInfo()) { print(join( "\t", map $_//'[undef]', @{$nic}{qw( device ip ipv6 bcast mask )} ), "\n"); }

Update: Cleaned up opening paragraph.
Update: Second push discovered ability to turn invisible. Added anti-invisibility field to post so it's visible again.

Replies are listed 'Best First'.
Re^3: Data::Dumper is returning empty
by MikeDexter (Sexton) on Jan 19, 2010 at 19:16 UTC

    I am getting an error when I try to run this code. It says

    Search pattern not terminated at ./sec-test.pl line 166 (#1) (F) The lexer couldn't find the final delimiter of a // or m{} construct. Remember that bracketing delimiters count nesting level. Missing the leading $ from a variable $m may cause this error. Note that since Perl 5.9.0 a // can also be the defined-or construct, not just the empty search pattern. Therefore code written in Perl 5.9.0 or later that uses the // as the defined-or can be misparsed by pre-5.9.0 Perls as a non-terminated search pattern. Uncaught exception from user code: Search pattern not terminated at ./sec-test.pl line 166. at ./sec-test.pl line 166

    line 166 is the map $_//'[undef]', line

    Also how would I return to main so I could run the following code section, or code similar to what I have below?

    my %nicdata = networkInfo($platform); for (@nicdata) { print "Device: $nic{device} has the IP Address of $nic{ip}\n\tMask +: $nic{mask}\n\tBroadcast: $nic{bcast}\n"; print "Device: $nic{device} also has IPv6 address of $nic{ip6}\n"; }
      Sorry, I used a 5.10 construct. Change
      map $_ // '[undef]'
      to
      map defined($_) ? $_ : '[undef]'

      Update:

      Concerning the code you posted, you introduced numerous errors:

      • You're assigning a list of nicdata to a hash.
      • Your hash is named nicdata, but it does contains the data about multiple nics.
      • You use @nicdata without ever placing any information in it.
      • Your array is named nicdata, but it's suppose to contain the data about multiple nics.
      • You use %nic without ever placing any information in ti.
      • You pass $platform as an argument as though it can change.
      for my $nic (networkInfo()) { print "Device: $nic->{device} has IP Address $nic->{ip}\n" . "\tMask: $nic->{mask}\n" . "\tBroadcast: $nic->{bcast}\n"; print "Device: $nic->{device} also IPv6 address $nic->{ip6}\n" if defined($nic->{device}); }

        Using the following code gives a different error about scoping

        Global symbol "%nic" requires explicit package name

        for my $nic (networkInfo()) { print "Device: $nic{device} has IP Address $nic{ip}\n" . "\tMask: $nic{mask}\n" . "\tBroadcast: $nic{bcast}\n"; print "Device: $nic{device} also IPv6 address $nic{ip6}\n" if defined($nic->{device}); }

        Much closer. I had to add a comma to end of that line. Now when it runs I get use of uninitialized value in pattern match (m//) lline 55 and 127 error.

        Are you sure you would like to delete all *.bak files that exist in: /emc/cccadm/scripts/perl/etc-test yes/no yes No files were found >>> Oper Sys: linux >>> Platform: RedHat >>> Hostname: ccasec1.centera.lab.emc.com Use of uninitialized value in pattern match (m//) at ./sec-test.pl line 55 (#1) (W uninitialized) An undefined value was used as if it were already defined. It was interpreted as a "" or a 0, but maybe it was a mistake. To suppress this warning assign a defined value to your variables. To help you figure out what was undefined, perl tells you what operation you used the undefined value in. Note, however, that perl optimizes your program and the operation displayed in the warning may not necessarily appear literally in your program. For example, "that $foo" is usually optimized into "that " . $foo, and the warning will refer to the concatenation (.) operator, even though there is no . in your program. Use of uninitialized value in pattern match (m//) at ./sec-test.pl line 127 (#1)