http://www.perlmonks.org?node_id=749322

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

I've written some code for parsing input files which is working happily on one machine, but failing on another.

Although not necessary I've added support for the input to be broken across multiple lines - via "\" character at the end of line. The following is a cut-down version of the code (and identical to recipe 8.1 from the perl cookbook):

#!/usr/bin/perl -w use strict; use warnings; while (defined(my $line = <DATA>) ) { chomp $line; # line 7 if ($line =~ s/\\$//) # line 8 { $line .= <DATA>; redo unless eof(DATA); } print "LINE: '$line'\n"; } __DATA__ 1 1 2 2 3 3 4 4 \ 4 4 \ 4 4 5 5

On one machine that works perfectly:

skx@gold:~$ ./bug.pl LINE: '1 1' LINE: '2 2' LINE: '3 3' LINE: '4 4 4 4 4 4' LINE: '5 5' LINE: '' skx@gold:~$ perl -v|grep perl, This is perl, v5.10.0 built for x86_64-linux-gnu-thread-multi

On another it gives errors:

steve@skx:~$ ./bug.pl LINE: '1 1' LINE: '2 2' LINE: '3 3' Use of uninitialized value in scalar chomp at ./bug.pl line 7, <DATA> +line 5. Use of uninitialized value in substitution (s///) at ./bug.pl line 8, +<DATA> line 5. Use of uninitialized value in concatenation (.) or string at ./bug.pl +line 13, <DATA> line 5. LINE: '' LINE: ' 4 4' LINE: '5 5' LINE: '' steve@skx:~$ perl -v | grep perl, This is perl, v5.8.8 built for i486-linux-gnu-thread-multi

I would expect that the while loop wouldn't run if there was an undefined line - so I'm unclear on why the chomp is giving the uninitialized variable warning.

For the moment I've just reworked my input into non-split lines, but I'm confused as to what I've done wrong!

Steve
--