1 / 13

Programming in Perl File and directory handling

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.

trang
Télécharger la présentation

Programming in Perl File and directory handling

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming in PerlFile and directory handling Peter Verhás January 2002.

  2. 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.

  3. 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

  4. 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.

  5. 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

  6. Reading in a loop open(F,"test.pl"); while(<F>){ print $_; } close F; By default it reads into $_ Returns undef when end of file

  7. 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”

  8. 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.

  9. 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

  10. Handling directories opendir(D,”dirname”); readdir(D); closedir D; • You can not open a directory for writing 

  11. 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"; }

  12. 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/$_"; } }

  13. Thank you for your kind attention.

More Related