Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

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:~$ ./ 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:~$ ./ LINE: '1 1' LINE: '2 2' LINE: '3 3' Use of uninitialized value in scalar chomp at ./ line 7, <DATA> +line 5. Use of uninitialized value in substitution (s///) at ./ line 8, +<DATA> line 5. Use of uninitialized value in concatenation (.) or string at ./ +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!


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

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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?

    What's my password?
    Create A New User
    [marto]: good morning all, TCIF!
    [hippo]: Good morning, marto.
    [choroba]: Tux It works for me.
    choroba demonstrates.
    Tux looks
    [Discipulus]: good morning community! you have to wait still some days for my new Tk cufp! ;=)
    choroba's pm-cb-g can even do the genitive.
    [karlgoethebier]: good morning
    [choroba]: plus good morning
    [Tux]: I apologize: it works correct on the PM website, but it shows wrong in #cbstream and I don't see any difference in pm_cb_g

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (8)
    As of 2018-04-27 08:35 GMT
    Find Nodes?
      Voting Booth?