Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Newline's creep in, while using Tie::File

by GrandFather (Saint)
on Nov 16, 2020 at 20:52 UTC ( [id://11123715]=note: print w/replies, xml ) Need Help??


in reply to Newline's creep in, while using Tie::File

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:

use strict; use warnings; use Tie::File; my @records; tie @records, 'Tie::File', "pile_out.txt"; my $in_file = "fake_vals.fem"; my $outIndex = 10; open my $in, '<', "$in_file" or die "cannot open '$in_file' for readin +g: $!\n"; while (my $line = <$in>) { next if $line !~ /\bNODE\b/i; chomp $line; $records[$outIndex] = $line; ++$outIndex; }

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

Replies are listed 'Best First'.
Re^2: Newline's creep in, while using Tie::File
by Bod (Parson) on Nov 17, 2020 at 00:31 UTC

    First off, always use strictures

    If they should always be used...why are they not on by default?

      If they should always be used...why are they not on by default?

      For backwards compatibility. Making strictures on by default for newer versions of Perl would break most old or poorly written Perl code. If you use 5.012; strictures are on by default.

      Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

        I'm probably asking the wrong person...but...is it a universal belief that strictures should be on or a majority belief? Is there an argument for leaving them off?

        I have never used them although I would not argue that my code could be held up as best practice or even good practice - other than it does everything it was designed to do and performs non-trivial tasks. On that basis, it works as well as can be expected. I am aware of some of the problems they eliminate and code so these issues do not arise.

      Perl 7 (not sure of the equivalent 5.x version but I think it is 5.32.0) is starting off with strict and warnings 'on' by default. They are not on by default in 5.x because it would break a lot of existing code.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (2)
As of 2024-04-20 06:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found