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

I've run the following using -w, with only one minor complaint. Also run it using -d, and found that for some reason, it doesn't enter the while loop on line 14. Can anyone help me figure out why I'm not entering the while? The script doesn't die on the filehandle open, so I know that's not a problem. There is one detail that might be important--the file I'm opening with has only a single line and doesn't have a "\n" at the end. I would like to have a script that can deal with single and multi-lined files though.
#!/usr/bin/perl if (!$ARGV[0]) { die "Please include the name of a comma-delimited text file as + argument. "; } elsif ( ($ARGV[0] eq "help") or ($ARGV[0] eq "-h") or ($ARGV[0] eq " +--help") ) { die "Please include the name of a comma-delimited text file as + argument. "; } else { $FILE = $ARGV[0]; open FILE or die "Couldn't open file $FILE: $!\n"; #open(FILE,"$FILE") or die "Couldn't open file $FILE: $!\n"; while (<FILE>) { $file_data=<FILE>; } close FILE; @new_array = split /,/, $file_data; $i=1; open LOG, '>>./array_parser.log'; print LOG localtime() . "\n"; foreach $item (@new_array) { print LOG "Item i => $item\n"; $i++; } print LOG "-------- END --------\n\n"; close LOG; };

Replies are listed 'Best First'.
Re: Script doesn't enter while loop.
by Masem (Monsignor) on Aug 23, 2001 at 20:57 UTC
    while (<FILE>) { $file_data=<FILE>; }
    The <> operator reads a line from a file regardless if you set it to anything, so this script will try to read in two lines every time it iterates through the loop. Since you only have a single line, you're going to run into problems here. A better way to code this is :
    while( $file_data = <FILE> ) { }
    or
    my ( $file_data ) = <FILE>;

    -----------------------------------------------------
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
    It's not what you know, but knowing how to find it if you don't know that's important

      Or even

      my $file_data = <FILE>;
      if you don't like useless parentheses. :-) (Note that those parentheses are far from useless under other circumstances-- you just don't need them here).

      For the problem of "not entering the while loop", note that the debugger does some strange and curious things around loops sometimes. I tested your code with the following two-line input:

      foo,bar fooey
      and got the following sequence in the debugger (note that the empty command statements are all interpreted as "n"):
Re: Script doesn't enter while loop.
by VSarkiss (Monsignor) on Aug 23, 2001 at 21:03 UTC

    If your file doesn't have any \n delimiters and you want to read it all in one slurp, tell Perl not to stop when reading:

    undef $/; $file_data = <FILE>; # That pulled it all in close FILE;

    BTW, This statement: while (<FILE>) {is reading the (single) line from your file and assiging it to $_. The body of the while tries to read from the file a second time. Remember that the <> operator reads data in addition to checking for EOF.

    HTH

Re: Script doesn't enter while loop.
by Sifmole (Chaplain) on Aug 23, 2001 at 23:01 UTC
    Well the line: open FILE or die "Couldn't open file $FILE: $!\n"; seems a bit off. I did this in a script with warnings and strict turned on and got a few messages. Could that be causing a problem?