Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming in Perl File and directory handling Peter Verhás January 2002.

Similar presentations


Presentation on theme: "Programming in Perl File and directory handling Peter Verhás January 2002."— Presentation transcript:

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

2 File handling open(handle,”filename”) close 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 reads a line (bytes until the next record separator) @a = gives all the lines $/=undef; $file= ; reads the whole file

6 Reading in a loop open(F,"test.pl"); while( ){ 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( ){ chomp; print ”$_\n”; } close F; reads the whole line including the new line at the end of the line. chomp chops it off safely.

9 truncate, seek, flock $LOCK_SH = 1; $LOCK_EX = 2; $LOCK_NB = 4; $LOCK_UN = 8; open(F,"+ counter.txt"); flock F,$LOCK_EX; seek F,0,0;# seek to the start of the file $counter = ; $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; Shared lock, Exclusive lock Non-blocking lock Unlock 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.


Download ppt "Programming in Perl File and directory handling Peter Verhás January 2002."

Similar presentations


Ads by Google