140 likes | 289 Views
Programming in Perl File and directory handling. Peter Verhás January 2002. File handling. open(handle,”filename”) close handle <handle> read a record from file print handle expressionlist read, write, seek, truncate, flock, binmode See all these in detail.
E N D
Programming in PerlFile and directory handling Peter Verhás January 2002.
File handling • open(handle,”filename”) • close handle • <handle> read a record from file • print handle expressionlist • read, write, seek, truncate, flock, binmode See all these in detail.
Opening and closing a file open(F,”f.txt”)to read open(F,”>f.txt”)to write a new file open(F,”>>f.txt”)to append open(F,”+<f.txt”)read/write open(F,”+>f.txt”)read/write but first truncate Return value is non-zero or undef in case of error. close F closes the file
binmode • There are no text files. • There are no binary files. • The handling of a file can be binary or text. • The two conventions: • Windows \r\n • UNIX \n • Using binmode is safe and portable.
Reading record(s) from file • $/ specifies the record separator, \n by default • <F> reads a line (bytes until the next record separator) • @a = <F> gives all the lines • $/=undef; $file=<F>; reads the whole file
Reading in a loop open(F,"test.pl"); while(<F>){ print $_; } close F; By default it reads into $_ Returns undef when end of file
Printing to a file • print HANDLE expression list • print STDERR ”error output” • print STDOUT ”just output” • print ”to STDOUT default” • print F ”a line into a file\n”
Getting rid of the new-line character open(F,"test.pl"); while(<F>){ chomp; print ”$_\n”; } close F; <F> reads the whole line including the new line at the end of the line. chomp chops it off safely.
truncate, seek, flock Shared lock, Exclusive lock Non-blocking lock Unlock $LOCK_SH = 1; $LOCK_EX = 2; $LOCK_NB = 4; $LOCK_UN = 8; open(F,"+<counter.txt") or open(F,">counter.txt"); flock F,$LOCK_EX; seek F,0,0;# seek to the start of the file $counter = <F>; $counter++;# change here to -- seek F,0,0;# seek to the start of the file again truncate F,0;# comment this out when decrementing print F $counter; flock F,$LOCK_UN; close F; print $counter; Try go up to 10 and change $counter-- with #truncate commented out. You will see: 10, 9, 89
Handling directories opendir(D,”dirname”); readdir(D); closedir D; • You can not open a directory for writing
Example: getting the list of the files opendir(D,'.'); @filesanddirs = readdir(D); closedir D; @files = (); for ( @filesanddirs ){ push @files, $_ if -f $_; } for(@files){ print "$_\n"; }
Example: getting all files recursively $StartDirectory = '.'; opendir(D,$StartDirectory); @files = readdir(D); closedir(D); @dirs = grep( (-d "$StartDirectory/$_") && /^[^.]/ ,@files); for( @files ){ $_ = "$StartDirectory/$_" } while( $#dirs > -1 ){ $cdir = pop @dirs; opendir(D,"$StartDirectory/$cdir"); @f = readdir(D); closedir(D); @d = grep( (-d "$StartDirectory/$cdir/$_") && /^[^.]/,@f); for( @d ){ push @dirs, "$cdir/$_"; } for( @f ){ push @files, "$StartDirectory/$cdir/$_"; } }