start page | rating of books | rating of authors | reviews | copyrights

Learning Perl

Learning PerlSearch this book
Previous: A.8 Chapter 9, Miscellaneous Control Structures Appendix A
Exercise Answers
Next: A.10 Chapter 11, Formats
 

A.9 Chapter 10, Filehandles and File Tests

  1. Here's one way to do it:

    print "What file? "; chomp($filename = <STDIN>); 
    
    open(THATFILE, "$filename") ||     die "cannot open $filename: $!"; while (<THATFILE>) {     print "$filename: $_"; # presume $_ ends in \n }

    The first two lines prompt for a filename, which is then opened with the filehandle THATFILE . The contents of the file are read using the filehandle and printed to STDOUT .

  2. Here's one way to do it:

    print "Input file name: "; chomp($infilename = <STDIN>); print "Output file name: "; chomp($outfilename = <STDIN>); print "Search string: "; chomp($search = <STDIN>); print "Replacement string: "; chomp($replace = <STDIN>); open(IN,$infilename) ||     die "cannot open $infilename for reading: $!"; ## optional test for overwrite... die "will not overwrite $outfilename" if -e $outfilename; open(OUT,">$outfilename") ||     die "cannot create $outfilename: $!"; while (<IN>) {    # read a line from file IN into $_     
    
    s/$search/$replace/g; # change the lines     print OUT $_; # print that line to file OUT } close(IN); close(OUT);

    This program is based on the file-copying program presented earlier in the chapter. New features here include prompting for the strings and the substitute command in the middle of the while loop, as well as the test for overwriting a file.

    Note that backreferences in the regular expression do work, but referencing memory in the replacement string does not.

  3. Here's one way to do it:

    while (
    
    <>) {     chomp; # eliminate the newline     print "$_ is readable\n" if -r;     print "$_ is writable\n" if -w;     print "$_ is executable\n" if -x;     print "$_ does not exist\n" unless -e; }

    This while loop reads a filename each time through. After discarding the newline, the series of statements tests the file for the various permissions.

  4. Here's one way to do it:

    while (<>) {     chomp;     $age = -M;     if ($oldest_age < $age) {         $oldest_name = $_;         $oldest_age = $age;     } } print "The oldest file is $oldest_name ",     "and is $oldest_age days old.\n";

    First, we loop on each filename being read in. The newline is discarded, and then the age in days gets computed with the -M operator. If the age for this file exceeds the oldest file we've seen so far, we remember the filename and its corresponding age. Initially, $oldest_age will be 0 , so we're counting on there being at least one file that is more than 0 days old.

    The final print statement generates the report when we're done.


Previous: A.8 Chapter 9, Miscellaneous Control Structures Learning Perl Next: A.10 Chapter 11, Formats
A.8 Chapter 9, Miscellaneous Control Structures Book Index A.10 Chapter 11, Formats