Perl (not PERL) does not, usually, loop through all the lines of the file; to make it do so see perlrun.
I've a few questions about your post:
- Do you really want to replace the word in binary files? Changing "GIF" to "IMG" may make a binary file unusable. Use a file test, such as -T to see if the file is text or not (note that I've found -T and -B to be less than 100% reliable on Windows, so I would be careful on any platform).
- If you want to change every file in a directory, you will have to loop through the files, not just loop through the records in a file. My preferred way of doing this is glob to get the file names, and looping through the resulting list.
- You can change files in place; there is a Perl runtime option to do so. See perlrun. I believe the option is -i. I tend not to use this option; when I need to change a file in situ my preference is Tie::File.
- Just about every language with which I've dealt is case sensitive when dealing with strings; in Perl (and other languages) this can be turned off in regular expressions (regex); see perlretut
- Depending on the size of your files, you may want to localize $/ (see perlvar) so each file's contents are read as a single string, vs reading the file line by line, e.g., something like this:
use strict;
use warnings;
sub subsitute {
(my $filename, my $oldword, my $newword) = @_[0..2];
local $/;
if(open(my $fh, "<", $filename)){
my $contents = <$fh>;
$contents =~ s/\b$oldword\b/$newword/ig;
close($fh);
if(open($fh, ">", $filename){
print $fh $contents;
close($fh);
return 1; # for success
}
else{
warn "Could not open $filename for writing because $!; skippin
+g it\n";
return undef;
}
}
else {
warn "Could not open $filename because $!; it will be skipped\n";
return undef;
}
}
I'm explicitly opening the file for writing as I don't want to assume that the -i runtime option is specified. Also, I'm assuming that case is not important and that you want every instance of $oldword replaced.
You will have to do some more checks than just to see if the file is binary or not. Obviously, you should make sure that you've got correct rights to change the file. I would also advise that you copy the file, instead of changing it in place, until after you've thoroughly tested the code. You would, for example, not want to change source code in such a way as to introduce a syntax error or undefined external reference. And changing system("cp $oldfile $newfile"); to system("rm $oldfile $newfile"); could tend to be career inhibiting.
emc
Any New York City or Connecticut area jobs? I'm currently unemployed.
There are some enterprises in which a careful disorderliness is the true method.
—Herman Melville
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.