Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

error printing all text in text file

by bigup401 (Pilgrim)
on Jan 11, 2022 at 09:03 UTC ( [id://11140354]=perlquestion: print w/replies, xml ) Need Help??

bigup401 has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: error printing all text in text file
by soonix (Canon) on Jan 11, 2022 at 10:11 UTC
    while(chomp(my $line = <$fh>)) { $show = "$line\n"; }
    This more or less says "as long as you can read a line, do so and replace the contents of $show with the content of that line".
    1. What do you think, will be in $show after reading the second, third, etc. line?
    2. Look at perldoc perlop under "Additive Operators" and then under Assignment Operators
    HTH

      It's likely that snippet should be:

      while(chomp(my $line = <$fh>)) { $show .= "$line\n"; }

      And that is pretty much functionally equivalent to:

      { local $/ = undef; $show = <$fh>; }

      ...except that his code would append a newline at the end of the file if one wasn't there already. So $show =~ s/(?<!\n)\z/\n/ if that trailing newline is important. If he intended to keep the lines separated then he could slurp into an array.


      Dave

        i appreciate thanks

      There are multiple problems with this snippet.
      1. This code will usually abend with throw a warning of an undefined value passed to chomp!
      2. The last line of the file can be missed if its not terminated by \n.
      What while (chomp (my $line = <$fh>)){} means is execute the while code if chomp is successful in removing at least 1 character from $line.

      Assuming that the last line is terminated properly, after all data is read from the filehandle, $line will be undefined. chomp won't like that and will barf.

      Now if the last line of the file is not terminated, an even more insidious thing can happen. That last line will be ignored because chomp does not remove any characters from that line.

      So, don't use this construct.
      here is proof:

      use strict; use warnings; $|=1; my $data = <<END; asdf fjfjf 324 0 2345 2wefrwef END #chomp $data; #toggle on/off to see results open my $fh, "<", \$data or die "$!"; while (chomp (my $line = <$fh>)) { print $line; } __END__ asdf fjfjf324023452wefrwefUse of uninitialized value $line in chomp at + testchompWhile.pl line 15, <$fh> line 5. asdf fjfjf32402345
Re: error printing all text in text file
by hippo (Bishop) on Jan 11, 2022 at 10:09 UTC
    am not getting all lines

    Concatenate or use an array.

    i would like to have lines outputted in colors like blue and red

    Use CSS.


    🦛

A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (5)
As of 2024-03-28 23:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found