Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Reading a text file collapsing line continuations

by kennethk (Monsignor)
on Mar 09, 2009 at 15:57 UTC ( #749327=note: print w/ replies, xml ) Need Help??


in reply to Reading a text file collapsing line continuations

I can repeat your issue with my Linux box (and get good behavior w/ AS 5.8.8 under Windows). Based upon what I see in my debugger, you are having a scoping issue. Essentially, every iteration of your while loop reinitializes $line as per your my statement. My guess is they modified the scoping behavior in 5.10.0, which fixes this particular issue. You can avoid the problem with:

#!/usr/bin/perl -w use strict; use warnings; my $line; while (defined($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

Update:Regarding your comment

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.
you should check out the documentation to see why the original developer used a redo in place of a next.


Comment on Re: Reading a text file collapsing line continuations
Select or Download Code
Re^2: Reading a text file collapsing line continuations
by skx (Parson) on Mar 09, 2009 at 17:58 UTC

    Thanks for the tip, and for the solution.

    The reason is obvious now you point it out, but I managed to fail to spot it myself so ++.

    Steve
    --
Re^2: Reading a text file collapsing line continuations
by CountZero (Bishop) on Mar 09, 2009 at 17:59 UTC
    I doubt if it is a scoping issue. The script works as expected on my Windows XP Pro ActiveState Perl v5.8.8 [MSWin32-x86-multi-thread]. The OP has a problem with the script under Linux Perl v5.8.8 but no problem under Linux Perl v5.10.0.

    If you are right, that means that the scoping rules would be different between Windows and Linux? Seems strange to me.

    But I see the same output (but not the warnings!) as the OP if I replace the redo with next and that is as expected.

    The difference between redo and next being that redo does not re-evaluate the condition, whereas next of course does. In other words, with redo you do not run the defined(my $line = <DATA>) again, so the accumulation of the "broken" lines work and hence it cannot be a scoping issue since you never leave, nor re-enter the scope.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      That my Linux box (v5.8.8 built for x86_64-linux-gnu-thread-multi under Ubuntu) replicates the issue and my Windows box (v5.8.9 built for MSWin32-x86-multi-thread, I forgot I upgraded) does not implies something differs between the implementations. Running the Linux variety through my debugger shows $line to be undef on line 7 on the continuation case, whereas it maintains continuity under AS. Since we are dealing with a variable which is scoped at the loop, I was reading it as the variable was getting a new instance upon redo even though the my was not being re-evaluated. This is supported by the following code, which prints an infinite series of 1's on my Windows box and prints one 1 and then an infinite series of "Use of uninitialized value in print at fluff.pl line 6, <DATA> line 1." on my Linux box:

      #!/usr/bin/perl -w use strict; use warnings; while (defined(my $line = <DATA>) ) { print $line; redo; } __DATA__ 1

      Update: Better example code (I think). Prints 210 under Windows and 2 under Linux:

      #!/usr/bin/perl use strict; use warnings; my $j = 2; while ( my $i = $j ) { print $i if defined $i; last unless $i--; redo; }

      Update 2: Filed a bug report, id [perl #63752]

        It is quite frightening that there are such fundamental differences between Linux and Windows implementations. I would rate that as a bug.

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        Received a reply on the bug report. The change in behavior between 5.8.8 and 5.10.0 represents "an intended change of behaviour (a bugfix)". The fact that the AS distribution behaves well reflects non-standard patching implemented by AS (Change 24412). Incident is closed, though it seems to me like this patch also needs inclusion in the next distro of 5.8.x.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://749327]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (11)
As of 2014-07-25 13:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (172 votes), past polls