1

I bumped my head on this piece of code for two days without any success: What I need to do is obtain a list of IP addresses starting from a long list of NETWORKS in this format:

NETWORK NUMBER_OF_HOSTS
192.168.1.0 512

I've found this bunch of code in the Net:IP module that seems perfect for my needs:

my $ip = new Net::IP ('192.168.1.0 + 512');
        do {
             print $ip->ip(), "\n";
        } while (++$ip);

With the NETWORK and HOST value declared explicitly the snippet works flawlessly, when I use it inside a while loop passing the data through variables, the problems start. After many tries I've modified the list as follows:

'192.168.150.0 + 512'

And this is the resulting script:

#!/usr/bin/perl

use strict;
use warnings;
use Net::IP;

open(MYFILE, "ips.txt") or die "$0: Can't open input file $input: $!\n";

while (<MYFILE>){

        chomp $_;
        my $ip = new Net::IP ($_);
        do {
             print $ip->ip(), "\n";
        } while (++$ip);
}

close(MYFILE);

When I try to execute it gives me the following error:

Can't call method "ip" on an undefined value at ./iplist.pl line 14, line 1.

If I print the variable inside the while loop with this print $_,"\n"; it prints the variable correctly, so where am I wrong? Any help is appreciated.

UPDATE1: od output for ips.txt

0000000   '   4   1   .   5   7   .   1   1   6   .   0       +       1
0000020   0   2   4   '  \n   '   4   1   .   5   7   .   1   9   2   .
0000040   0       +       1   6   3   8   4   '  \n   '   4   1   .   6
0000060   6   .   1   9   2   .   0       +       1   6   3   8   4   '
0000100  \n   '   4   1   .   7   4   .   8   0   .   0       +       4
0000120   0   9   6   '  \n   '   4   1   .   7   5   .   4   8   .   0
0000140       +       4   0   9   6   '  \n   '   4   1   .   7   6   .
0000160   2   4   .   0       +       2   0   4   8   '  \n   '   4   1
0000200   .   7   7   .   6   4   .   0       +       2   0   4   8   '
0000220  \n   '   4   1   .   7   8   .   4   0   .   0       +       1
0000240   0   2   4   '  \n   
2
  • 1
    Nits aside, the program is fine. The error must be in the input file. What's the output of od -c ips.txt? Commented Mar 26, 2013 at 11:31
  • @ikegami I've added the first lines of the od output Commented Mar 26, 2013 at 11:43

1 Answer 1

3

The string literal (i.e. Perl code)

'192.168.1.0 + 512'

produces the string

192.168.1.0 + 512

As for readline (<$fh>), it produces a line of the file. Ignoring the newline you chomp away, it returns the string

'192.168.1.0 + 512'

This differs from the earlier string in that it has extra quotes. Remove the quotes from the file.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.