Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Printing a String (TOO stupid?)

by TGI (Parson)
on Jun 20, 2014 at 05:06 UTC ( #1090550=note: print w/replies, xml ) Need Help??


in reply to Printing a String (TOO stupid?)

Printing is a red-herring here. The real issue is how you open the file.

Instead of doing a plain-vanilla open, you want to turn on one of [Perl's amazing IO layers]. In this case :crlf.

open my $fh, '<:crlf', "test.txt" or die "Failed to open $!\n"; my $line; while ($line = <$fh>) { chomp($line); print "It was $line!!!\n"; }

The awesome thing about this method, is you automatically translate \r\n to \n, but if the file has plain old \n line endings, the just cruise on through to your code without a problem.

You may also notice that I used a 3-argument open with a lexical file handle. This is generally considered to be a good idea.


TGI says moo

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2021-05-14 19:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (150 votes). Check out past polls.

    Notices?