Perl Monk, Perl Meditation | |
PerlMonks |
Re: Newline's creep in, while using Tie::Fileby GrandFather (Saint) |
on Nov 16, 2020 at 20:52 UTC ( [id://11123715]=note: print w/replies, xml ) | Need Help?? |
Your immediate problem is that you read lines from a file with <IN> into the default variable and "write" them immediately into the tied object. However the tied object is essentially an array of lines. Because you haven't stripped the line end character from the input line you end up with two line ends on output. However, there is a pile of other stuff that needs attention in your sample code. Consider this reworked version:
First off, always use strictures (use strict; use warnings; - see The strictures, according to Seuss). Always use lexical file handles. In combination with strictures that will save some very embarrassing file messes. Always use three parameter open. Usually at this point we suggest or die, but you are doing that already - great! However show the actual file path you were trying to open and the failure message. That will save a couple of iterations of debugging. Avoid using the default variable. Using a manifest named variable helps with code readability and helps maintenance. It also avoids subtle problems with the default variable changing content when you don't expect it to. A more contentions, but only marginally important suggestion is to use the pre increment operator unless logic demands the post increment operator. As a general rule stuff toward the end of a line gets lost and increment is important so do it up front.
Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
In Section
Seekers of Perl Wisdom
|
|