Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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
--

In reply to Reading a text file collapsing line continuations by skx

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others wandering the Monastery: (10)
    As of 2014-10-23 11:08 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      For retirement, I am banking on:










      Results (125 votes), past polls