2011年10月21日 星期五

Perl open file ,filehandle technique


while ($line = <CHECKBOOK>) {
  print $line;
}

Opening files

Opening a file in perl in straightforward:open FILE, "filename.txt" or die $!;The command above will associate the FILE filehandle with the file filename.txt. You can use the filehandle to read from the file. If the file doesn't exist - or you cannot read it for any other reason - then the script will die with the appropriate error message stored in the $! variable.
What if you wanted to modify the file instead of just reading from it? Then you'd have to specify the appropriate mode using the three-argument form of open.
open FILEHANDLE, MODE, EXPRThe available modes are the following:
modeoperandcreatetruncate
read<
write>
append>>

Each of the above modes can also be prefixed with the + character to allow for simultaneous reading and writing.
modeoperandcreatetruncate
read/write+<
read/write+>
read/append+>>

沒有留言:

張貼留言