Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing a variable from a file in a loop
    primarykey
    data
    text
    <p>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: </p> <p>NETWORK NUMBER_OF_HOSTS<BR> 192.168.1.0 512</p> <p>I've found this bunch of code in the Net:IP module that seems perfect for my needs:</p> <pre><code>my $ip = new Net::IP ('192.168.1.0 + 512'); do { print $ip-&gt;ip(), "\n"; } while (++$ip); </code></pre> <p>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:<BR></p> <p>'192.168.150.0 + 512'</p> <p>And this is the resulting script:</p> <pre><code>#!/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 (&lt;MYFILE&gt;){ chomp $_; my $ip = new Net::IP ($_); do { print $ip-&gt;ip(), "\n"; } while (++$ip); } close(MYFILE); </code></pre> <p>When I try to execute it gives me the following error:</p> <blockquote> <p>Can't call method "ip" on an undefined value at ./iplist.pl line 14, line 1.</p> </blockquote> <p>If I print the variable inside the while loop with this <code>print $_,"\n";</code> it prints the variable correctly, so where am I wrong? Any help is appreciated.</p> <p>UPDATE1: od output for ips.txt</p> <pre><code>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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload