in reply to Need help with small IF statement

You should reference existing threads for interested monks: Data::Dumper is returning empty ([id://818253]) for example.

For your desired result, you should use the structure as written above. The idea is that the networkInfo() subroutine returns a list, which you iterate over. For every entry in that list, you want to perform your tests. Examining your output, I note blank entries in your output for $nic->{ip}, $nic->{mask} and $nic->{bcast}. This means the bug is where you assign those values in the networkInfo() subroutine.

Replies are listed 'Best First'.
Re^2: Need help with small IF statement
by MikeDexter (Sexton) on Jan 20, 2010 at 16:13 UTC

    I will reference in the future. Good point. Sometimes too much information just muddies the water and I try to ask clear questions as much as possible. If I throw too much at someone they may run and hide.

    As for the code eth0 has no IPv4 information in it, but does have IPv6, so that is why the data returns empty. Here is the output of ifconfig

    eth0 Link encap:Ethernet HWaddr 00:19:B9:EB:80:91 inet6 addr: fe80::219:b9ff:xxxx:8091/64 Scope:Link UP BROADCAST MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:0 (0.0 b) TX bytes:0 (0.0 b) Interrupt:169 Memory:f4000000-f4011100 eth1 Link encap:Ethernet HWaddr xx:19:xx:xx:80:xx inet addr:xx.xx.xx.xx Bcast:xx.xx.xx.xx Mask:xx.xx.xx.xx inet6 addr: 3xxx:80c0:22c:225:219:b9ff:xxxx:80xx/64 Scope:Gl +obal inet6 addr: fexx::219:b9ff:feeb:80xx/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:4173511549 errors:0 dropped:0 overruns:0 frame:0 TX packets:4181891776 errors:0 dropped:0 overruns:0 carrier: +0 collisions:0 txqueuelen:1000 RX bytes:696505594365 (648.6 GiB) TX bytes:630337964502 (58 +7.0 GiB) Interrupt:169 Memory:f8000000-f8011100 lo Link encap:Local Loopback inet addr:xx.xx.xx.xx Mask:xx.xx.xx.xx inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:4698160 errors:0 dropped:0 overruns:0 frame:0 TX packets:4698160 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:611857168 (583.5 MiB) TX bytes:611857168 (583.5 Mi +B)

    I need this thing to not care about who has what. Not be dependant on IPv4 being satisifed. If IPv4 info is there print it. if IPv6 info is there print that too. Frustrated trying to figure that out.

    yesterday I mistakenly said What is garanteed is v4 data. What is not garanteed is which NIC the v4 data will be on and if there will be any v6 data and now I am realizing some nic devices could have IPv4 only, IPv6 only, or both.
    Can you or someone help me get that piece fixed?

      If there are multiple conditions (IPv4 may or may not be there, IPv6 may or may not be there), then there should be multiple conditionals. So rather than testing if the key device is defined, you should test if ip and if ip6 are defined, like:

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

      Note I also added defined tests. This has no real impact here (unless these values can be meaningfully defined to empty strings or the value 0), but it is probably a good habit to get into since later it will avoid unwanted autovivification side effects for complex structures as well as weird bugs when those values are legal.