http://qs1969.pair.com?node_id=1125183

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

OK. I've been struggling with this long enough. I'm going to have to ask for help...

I'm working (manipulating) a list of IP addresses. I have been largely doing this manually, which is stupid. So now that I've finally got to the point that the list is large enough that I have to deal with it in some (at least somewhat) automated fashion. I'm looking to Perl to save the day.

The finished list is packed in CIDR form. I unpack the address block I need to modify, then repack, and re-insert the (modified) block back into the list.

I looked at several nodes here, including (tye)Re2: Net::CIDR::Lite ?? (Merge CIDR addresses). But wasn't sure how to apply it to my needs. So I eventually settled on Net::CIDR. I've managed unpacking the list, with an array. But what I'm trying to do, is feed the list to the script

# unpackcidr < packed-list > unpacked-list

Here's what I have so far. But it requires me to paste the list between the paren's
#!/usr/bin/perl use Net::CIDR; use Net::CIDR ':all'; print join("\n", Net::CIDR::cidr2octets( # here's where I've tried <STDIN> && $_ but to no avail )) . "\n";
Inserting a list of quoted IP addresses in CIDR notation works in there. But that's not what I'm hoping for. I know this is dead simple stuff. But I'm apparently in dimwit mode ATM. :P

Thanks for anything that might help.

¡λɐp ʇɑəɹ⅁ ɐ əʌɐɥ puɐ ʻꜱdləɥ ꜱᴉɥʇ ədoH

Replies are listed 'Best First'.
Re: Redirection problem / question
by Anonymous Monk on Apr 30, 2015 at 01:15 UTC
    Um, try
    my $input = do { binmode STDIN; local $/; <STDIN>; };

    Also,

    use Path::Tiny qw/ path /; use Data::Dump qw/ dd /; my $input = path( 'input' )->slurp_raw ; my $output = Net::CIDR::cidr2octets( $input ); path( 'output')->spew_raw( $output ); dd( $input, $output ); ## for debugging/SOPW :)
      #!/usr/bin/perl -- use strict; use warnings; use Net::CIDR qw/ cidr2octets /; while(<STDIN>){ s{\s+$}{}; ## trim print "$_\n" for cidr2octets( $_ ); } __END__ $ echo dead::beef::/46 | perl cidr2octets.pl dead:beef:0000 dead:beef:0001 dead:beef:0002 dead:beef:0003
        Firstly; allow me to apologize for my seeming incoherent replies. But as I noted in the OP, the script(s) I'm currently using to help automate the process, must be responded to, anywhere from 60 - 90 seconds. So it makes it extremely difficult carry out a thought process, let alone, type them in a box. It plays hell, when trying to coordinate a group of routines for a Perl script. Hence my plea for help.

        While I'm extremely grateful for the solution @anonymous, I must admit I'm also a bit disappointed. As I had hoped to solve it with your earlier hints. But not that disappointed! :)

        Thanks!

        Here's what I've used

        #!/usr/bin/perl -w use Modern::Perl; use Net::CIDR qw/ cidr2octets /; while(<STDIN>){ s{\s+$}{}; print "$_\n" for cidr2octets($_); } __END__
        Which allows me to
        $ unpack-cidr.pl < packed-cidr > unpacked-cidr
        Once again, I'm really grateful for your taking the time to respond with the illusive logic I was seeking. This is all part of a much larger process, and this missing piece of the puzzle was just what I needed to put it all together. I'll post it all here, when I've completed it. As I suspect others will also gain from it.

        Thanks again!

        ¡λɐp ʇɑəɹ⅁ ɐ əʌɐɥ puɐ ʻꜱdləɥ ꜱᴉɥʇ ədoH

      Well. I actually tried an example from another script I created to strip ANSI sequences from (type)script output
      binmode STDIN, ":raw:eol(LF)"; while (<STDIN>) { s/\033[^m]*m//g; print $_;
      But I found my problem seems to stem from how to implement it -- plug it into what I have working with Net::CIDR. As shown in my OP example.

      ¡λɐp ʇɑəɹ⅁ ɐ əʌɐɥ puɐ ʻꜱdləɥ ꜱᴉɥʇ ədoH

        Well. I actually tried an example from another script I created to strip ANSI sequences from (type)script output But I found my problem seems to stem from how to implement it -- plug it into what I have working with Net::CIDR. As shown in my OP example.

        What?

        You don't show sample input, and you say there is a problem with the program, <STDIN> doesn't work in some way

        Well, use dd() to dump the data and show the data here on perlmonks