Perl File
Open
open CONFIG, 'config'; open CONFIG, '<config'; open OUTPUT, '>output'; open LOG, '>>logfile'; # appending open CONFIG, '<', 'file'; open OUTPUT, '>, $filename; open LOG, '>>', $logfilename;
Encoding
open CONFIG, '<:encoding(UTF-8)', 'config'; [/perl] <b>UTF16LE</b> [perl] open LOGFILE, '>:endcoding(UTF-16LE)', $file_name;
– CR-LF
open LOGFILE, ‘<:crlf', $file_name;
autodie
Error handling is reluctant.
autodie module does die handling by auto.
use autodie; open LOG, '>>', 'logfile'; # if failed, die is called automatically.
Read Complete
use utf8; use open ":utf8"; open(DATAFILE, "<", "io_test.pl") or die("Error:$!"); while(my $line = <DATAFILE>){ chomp($line); print "$line\n"; } close(DATAFILE);
Write Complete
open(OUTPUT, ">", "text.txt") or die("Error:$!");; print OUTPUT "Hello World\n"; close(OUTPUT);
Additional mode
open(OUTPUTFILE, "+>> ", $file) or die "Cannot open missing file";
Delete file
Use unlink
unlink("test.txt");
Check directory and create new
if (!-d $file_local){ mkdir $file_local; }
“$file_local” is directory name