In addition to file operations, the program also uses some additional pattern matching and substitution operations. With the second file, the program will use the translate function "tr" to translate input lines to all uppercase letters as well as the substitution operator "s" to relace characters and strings. Many of these operations work with "$_", the default scalar variable in Perl. In most perl programs, this variable is not written out in the code, but is just understood to be the operand.
#!/usr/bin/perl -w #Experimenting with files print "Enter filename:"; chomp($fname = <STDIN>); ## Open a file. If unsuccessful, print an error message and quit. open (FPTR,$fname) || die "Can't Open File: $fname\n"; # First Technique: "Slurping" # This approach reads the ENTIRE file into memory # Caution... This is not a good method for BIG files!!! @filestuff = <FPTR> #Read the file into an array print "The number of lines in this file is ",$#filestuff + 1,"\n"; print @filestuff;Enter filename:  temp.txt
close (FPTR); #Close the file ## Some other useful capabilities ## Testing file attributes: print "Enter another filename:"; chomp($fname = <STDIN>); if (-T $fname) # Check if it's a textfile, and how old { print "File $fname is textfile. "; print "It was modified ", int(-M $fname), " days ago.\n"; open (FPTR,$fname) || die "Sorry. Can't Open File: $fname\n"; } elsif (-B $fname) # Check if it's a binary file, and some other stuff { print "File $fname is executable.\n" if (-x $fname); print "This file is ", -s $fname, " bytes.\n"; die "Since it is Binary file, we will not try to \"upcase\" this file.\n"; } else {die "File $fname is neither text nor binary, so it may not exist. \n" ; }
Enter another filename: window1 File window1 is executable. This file is 27141 bytes. Since it is Binary file, we will not try to "upcase" this file. |
    |
Enter another filename: temp.txt File temp.txt is textfile. It was modified 2 days ago. |
## Open a file for writing. Note UNIX-like I/O redirection symbol, ">". open (OUTFILE, ">upcase.txt") || die "Can't oupen output file.\n"; ## Better approach for large files... Work with just current input line. while (<FPTR>) # While still input lines in the file... { print "1. ",$_; # The symbol "$_" is the default variable, the current # input from file. Note: "$_" is assumed if left out. tr/a-z/A-Z/; # Translate all lower case letters to uppercase letters # in the default variable. print "2. ", $_; s/A/@/g; # More substitutions: All "A" chars become "@" signs. print"3. ", $_; s/UP/Down/g; # All "UP" words are replaced by the string "Down" print "4. ", $_; $pattern = '\sF(.*)L'; # Meaning of Regular Expression: # \sF - starts with a1. This is my file,and capital F # .* - some stuff in between # L - Has a capital L in it # The parentheses "mark" the stuff in between print " Match value: ", $1, "\n" if (/$pattern/);; s/$1/*/g if $_ =~ $pattern; # Substitute "*" for the marked pattern, # but anywhere within the line. print "5. ", $_, "\n"; print OUTFILE $_; # Print default variable to OUTFILE. } close (FPTR); # Close the other two files close (OUTFILE);