Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Corrector

by gok8000 (Scribe)
on Dec 02, 2008 at 15:47 UTC ( #727451=snippet: print w/replies, xml ) Need Help??
Description:

Hi all,

Sometimes I have to change words inside text files. When this happens, and when I know what to search for, I use this program.

#!c:/Perl/bin/Perl.exe
#
# corrector.pl
# changes words inside text files
# placed in the win directory C:\filestochange
# which is supposed to contain text files
#

my $dir_to_process = "C:\\filestochange";
opendir DH, $dir_to_process or die "Cannot open $dir_to_process: $!";

foreach $file (readdir DH) {
  unless ($file eq "." || $file eq ".." || $file eq "discarded" || $fi
+le eq "tmp") {
    print "in $dir_to_process $file is processed\n";
    open (INFILEHANDLE, "C:\\filestochange\\$file") or die "error open
+ing";
    open (OUTFILEHANDLE, ">C:\\filestochange\\tempfile8000") or die "e
+rror opening";
    while (<INFILEHANDLE>) {
      # chomp;
      s/this/that/; # substututes this with that
      s/one/two/; # substututes one with two
      print OUTFILEHANDLE;
    }
    close INFILEHANDLE;
    close OUTFILEHANDLE;
    rename "C:\\filestochange\\tempfile8000","C:\\filestochange\\$file
+";
  }
}

closedir DH;

print "\nDone (press enter key)\n";
$line = <STDIN>;
Replies are listed 'Best First'.
Re: Corrector
by hossman (Prior) on Dec 02, 2008 at 16:57 UTC

    May i humbly suggest an alternative...

    #!/usr/bin/perl -pi s/this/that/; # substitutes this with that s/one/two/; # substitutes one with two

    Run with: corrector.pl C:\filestochange

    It doesn't have some of the interesting side effects of the original script, but i consider that a good thing.

    Updated: there is no "i" in team, but there should have been one on the first line
      #!/usr/bin/perl -p s/this/that/; # substitutes this with that s/one/two/; # substitutes one with two

      It also doesn't work. Did you even try it?

        Nope, didn't give it any thought at all. should be fixed now.

Re: Corrector
by quinkan (Monk) on Dec 13, 2008 at 06:02 UTC
    Save yourself effort and use one of the "Slurp" modules, given that Perl 6 is to support even smarter things. These let you read all file content into a single variable. Less code to go wrong. Thus, one fragment might read:
    use File::Slurp: # get one file here, as $file my($bakfile, $file, $text); # maybe set up file renaming with something like this... ($bakfile = $file ) =~ s/\.([^.]+)$/\.bak/; # then, the main bit $text = read_file($file); $text =~ s/this/that/smg; rename($file, $bakfile); write_file($file, $text);
    Another possible version something like:
    use Perl6::Slurp; #<= only handles input at present use File::Slurp; # permits writing. # and then.... .... $text = slurp($file); $text =~ s/$this/$that/smg; ... with renaming here ? (see above). write_file($file, $text);
    And BTW do you really need that chomp and/or intend to remove newlines?

      Thank you for the suggestion.

      The commented chomp was left in the source for a possible future use, but at present newlines are not removed.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: snippet [id://727451]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2023-06-07 20:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How often do you go to conferences?






    Results (29 votes). Check out past polls.

    Notices?