Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Use of uninitialized value $i in bitwise and (&) at <Netmask.pm>

by dotowwxo (Acolyte)
on Dec 18, 2017 at 08:45 UTC ( [id://1205779]=perlquestion: print w/replies, xml ) Need Help??

dotowwxo has asked for the wisdom of the Perl Monks concerning the following question:

Hello everyone, I am very new to Perl. Any help would be greatly appreciated because I am stucked here for quite a long time :(

Basically what I am trying to do is:

1. I have a list of IP address in testingIP.txt

2. I want to loop through the text file and find out which IP address is in the net mask that I have specified using a PERL module that I have installed -- Net::Netmask http://search.cpan.org/~muir/Net-Netmask-1.9015/Netmask.pod

Unfortunately, it only works when there is only 1 IP address in testingIP.txt, this is my output for my current code:

Use of uninitialized value $i in bitwise and (&) at /usr/local/share/p +erl5/site_perl/5.26/Net/Netmask.pm line 411, <$fh> line 1. IP address: 192.168.1.1 is not inside 192.168.1.0/27 Use of uninitialized value $i in bitwise and (&) at /usr/local/share/p +erl5/site_perl/5.26/Net/Netmask.pm line 411, <$fh> line 2. IP address: 192.168.1.2 is not inside 192.168.1.0/27 Use of uninitialized value $i in bitwise and (&) at /usr/local/share/p +erl5/site_perl/5.26/Net/Netmask.pm line 411, <$fh> line 3. IP address: 192.168.1.3 is not inside 192.168.1.0/27 IP address: 192.168.1.4 is inside 192.168.1.0/27

This is my code

#!/usr/bin/perl use strict; use warnings; use Net::Netmask; my $filename = 'testingIP.txt'; open(my $fh, '<:encoding(UTF-8)', $filename) or die "Could not open file '$filename' $!"; my $block = Net::Netmask->new("192.168.1.0/27"); while (my $row = <$fh>){ my @row = $row; for (my $i=0; $i < scalar(@row); $i++){ if ($block->match($row[$i])){ print "IP address: $row[$i] is inside $block\n"; } else{ print "IP address: $row is not inside $block\n"; } } }

Replies are listed 'Best First'.
Re: Use of uninitialized value $i in bitwise and (&) at <Netmask.pm>
by hippo (Bishop) on Dec 18, 2017 at 09:22 UTC

    I notice that you are not chomping $row. Perhaps it is those EOL characters which are causing the problem?

    Also, you only ever have one element in your @row array, so there is little point in the loop. If I were you, as a general point I would avoid having @row and $row in the same context - as it stands it is too easy for strict to miss a typo. HTH.

      Hi, I have tried chomping @rows and some different scenarios, however, it stills gives me the same result. Understood, thanks for your advice!
Re: Use of uninitialized value $i in bitwise and (&) at <Netmask.pm>
by Eily (Monsignor) on Dec 18, 2017 at 09:23 UTC

    Hello dotowwxo, welcome to perl and to the monastery.

    Data::Dump is a useful tool for debugging. Add use Data::Dump qw( pp ); at the top and then you add: print '@row is now: '.pp(@row)."\n"; inside your loop. You'll find that it only ever has one element, that's because @row = $row replaces the content by $row only. Also my declares (creates) a new array, so even if you didn't replace the content, is starts empty anyway.

    You can read all the lines in an array like this: my @rows = <$fh>; Then you can simply remove the while loop and use only the inner one.

    Edit: I renamed @row as @rows, because hippo is right

      Hi, thank you for your recommendation. Tried debugging and your recommendation by removing the outer loop, it gave me the same results. I still don't see the issue why that error is produced. I am suspecting it to a problem/bug of the netmask module. I will try using other modules instead. Thank you again :)
Re: Use of uninitialized value $i in bitwise and (&) at <Netmask.pm>
by haukex (Archbishop) on Dec 18, 2017 at 09:02 UTC

    Looking at the CPAN Testers Matrix and some specific test results for Net-Netmask is worrying to me: the operations the module is performing should be deterministic, but at least on Linux some tests are failing sometimes*, so I'm not really sure if this module is reliable. I'm not up-to-date on the best modules for this kind of task, so perhaps another monk will be able to recommend something.

    * Update: I would guess it has to do with the options Perl was built with, a quick sampling of some failing vs. passing configurations shows that the failing ones have, among other things, USE_LONG_DOUBLE set, as also reported in #99184. OTOH, even older versions, like the one you've linked to, show some test failures.

      I normally use Net::CIDR for such tasks. It has always been reliable for me.

        Thank you so much for the recommendation!! Will try using this module instead.
Re: Use of uninitialized value $i in bitwise and (&) at <Netmask.pm>
by soonix (Canon) on Dec 18, 2017 at 12:44 UTC
    In addition to the advice by others: Something is wrong with
    if ($block->match($row[$i])){ print "IP address: $row[$i] is inside $block\n"; } else{ print "IP address: $row is not inside $block\n"; }
    in the "if" branch, you print $row[$i], while in the "else" branch, you print $row - no wonder your output contains blatant lies as e.g. "IP address: 192.168.1.1 is not inside 192.168.1.0/27" …
    I suggest something like
    print "IP address: $row[$i] is "; if ($block->match($row[$i])){ print "inside"; } else{ print "outside"; } print " $block\n";
    Of course, this isn't ideal either, but less error prone.
      Hello, will note down the tip for sure! However, its not ideal as the same error still occurs to me :( Appreciate the effort dou :) Thank you :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1205779]
Approved by haukex
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (6)
As of 2024-03-28 22:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found