Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
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 romping around the Monastery: (4)
As of 2021-03-02 11:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favorite kind of desktop background is:











    Results (42 votes). Check out past polls.

    Notices?