Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: File Reading and Closing confusion

by zentara (Archbishop)
on Aug 08, 2010 at 14:03 UTC ( [id://853654]=note: print w/replies, xml ) Need Help??


in reply to File Reading and Closing confusion

Actually, the topic is so broad and full of complexities, you need to ask a more specific question, showing some code.

As far as the code $text = <FILE> goes, look at the difference

#!/usr/bin/perl use warnings; use strict; open (FILE,"< $0 ") or die "Couldn't open: $!"; # check the difference between these two lines my $text = do {local $/; <FILE>}; #my $text = <FILE>; close FILE; print "$text\n";
In the abscence of a code example, these links may get you started. Basically, you can read a file line by line(default behavior), or sysread it in chunks. The diamond operator <> adds some magic. See diamond operator and "perldoc -f read"

Some filehandles you cannot seek on, like a socket filehandle. See read and sysread and run "perldoc -q file" and read all the sections. Remember, if you are on a unix style OS, you have file descriptors associated with each filehandle in /proc/$pid/fd. See How to close all open file descriptors after a fork? and close filehandles Remember, the file descriptors (numbers) in /proc/$pid/fd/ is where the real action takes place( at least on linux).... the filehandles are just convenience handles.

Alot of us Perl hackers are lazy, and don't close filehandles after use, just letting the system clean them up when the program exits. :-)

The one common gotcha encountered with the mix of unix and win32 systems, is that the 2 system have different line endings, so when the default line-by-line reading behavior is used to read a windows file on linux, proper line reading can get broken. Read "perldoc -f binmode".


I'm not really a human, but I play one on earth.
Old Perl Programmer Haiku

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2024-04-18 15:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found