Programming in Perl File and directory handling by ekc11009

VIEWS: 0 PAGES: 13

									       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.
           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 $_;
  }
                     By default it reads into $_
close F;
                     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”;
  }                  <F> reads the whole line
                     including the new line at the
close F;             end of the line.

                     chomp chops it off safely.
                         truncate, seek, flock
                  Shared lock,
$LOCK_SH = 1;
                  Exclusive lock
$LOCK_EX = 2;
                  Non-blocking lock
$LOCK_NB = 4;
                  Unlock
$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;        Try go up to 10 and change $counter--
close F;                 with #truncate commented out. You will see:
print $counter;          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/$_";
    }
  }
Thank you for your kind attention.

								
To top