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

I'm going insane, right? Trying to trim newlines out of an array read from a file. I'm a relative newb, but everything I've read on the internet tells me this should work. (I've taken out all the format validation for testing).

input.txt has this in it:

111.111.111.111 222.222.222.222 333.333.333.333
Here is my snippet:
my $File = input.txt; open(FILE, "$File"); @IPLIST = <FILE>; close(FILE); foreach my $test (@IPLIST) { print "Is $test an IP?\n"; }
Output looks like this:
Is 111.111.111.111 an IP? Is 222.222.222.222 an IP? Is 222.222.222.222 an IP?
Expected behavior, right? So then I add chomp(@IPLIST); before my loop, and the output becomes this:
Is an IP? Is an IP? Is an IP?
Same behavior if I do it on the read:  chomp( @IPLIST = <FILE> ); or in the loop for each element. Isn't chomp supposed to do the opposite of this??? What am I missing?

Replies are listed 'Best First'.
Re: chomp removes everything *except* the newline?
by Anonymous Monk on Jun 26, 2010 at 17:33 UTC
    When your code is made runnable (see below), it works fine. Be sure to use strictures (the strict, warnings, and maybe diagnostics pragmas) to catch syntax errors and such.

    #!perl use strict; use warnings; my $File = "input.txt"; # this string wasn't quoted, so you must have +not shown us the code you're using open(FILE, "<", $File) or die qq(failed to open file "$File": $!); # y +ou didn't check whether open failed chomp(my @IPLIST = <FILE>); # this works as expected close(FILE); foreach my $test (@IPLIST) { print "Is $test an IP?\n"; }
    Output:
    Is 111.111.111.111 an IP?
    Is 222.222.222.222 an IP?
    Is 333.333.333.333 an IP?

      Thanks for your help. Sorry about the inaccuracy but I hand-typed the code in my post and missed the quotes.
      Anyhoo, it is working now that I changed open(FILE, "$File") to open(FILE, "<", "$File").

      I have no idea how reading the file without that "<" could have affected chomp later o_O

        On a side note, there's no need to put double quotes around $File.

        While this makes sense in shell programming, for when the string has spaces in it etc., in Perl it's plain superfluous.

        Sorry about the inaccuracy but I hand-typed the code

        Copy and paste is your friend. If you don't want to post the code you ran, at least run the code you post!

        it is working now that I changed open(FILE, "$File") to open(FILE, "<", "$File")

        That's a start, but one of the mantras around here is: "Always use strictures (use strict; use warnings; - see The strictures, according to Seuss), always use the three parameter version of open, always use lexical file handles and always check the result of I/O". Taking all that on board your open should have looked more like:

        open my $inFile, '<', $file or die "Failed to open $file: $!";

        Although that doesn't at all explain the chomp oddity you saw. More likely reasons for that behaviour are that you were reading a file generated with different line endings than are native on your OS or something had messed with $/ (see perlvar $/).

        True laziness is hard work
Re: chomp removes everything *except* the newline?
by Anonymous Monk on Jun 26, 2010 at 17:29 UTC
Re: chomp removes everything *except* the newline?
by ikegami (Patriarch) on Jun 27, 2010 at 07:37 UTC

    So then I add chomp(@IPLIST); before my loop, and the output becomes this:

    Then the file probably contained

    111.111.111.111<something><NL> 222.222.222.222<something><NL>

    where <something> is interpreted by the terminal as some sort of command sequence.