Your variable names are very confusing. You're looking for $old_name in file $new?? In fact, I think you confused yourself. Use consistent, meaningful names.
Is this what you want?
sub fc {
# Pass by reference for efficiency.
local $_; *_ = \$_[1];
return uc(substr($_, 0, 1)) . lc(substr($_, 1));
}
sub match_case {
# Very simplistic.
# Pass by reference for efficiency.
our $template; *template = \$_[0];
our $s; *s = \$_[1];
return uc($s) if (uc($template) eq $template);
my $first_char = substr($template, 0, 1);
return fc($s) if (uc($first_char) eq $first_char);
return lc($s);
}
sub wanted {
if ((-f $File::Find::name) && ($File::Find::name !~ m/\.bak/)) {
my $old_file_name = $File::Find::name;
(my $new_file_name = $old_file_name) =~ s/$old_name/$mname/;
# # Make backup
# rename($new_file_name, $new_file_name.'.bak');
local *IN;
open(IN, '<' . $old_file_name)
or die("Unable to open old file: $!$/");
local *OUT;
open(OUT, '>' . $new_file_name)
or die("Unable to create new file: $!$/");
while (<IN>) {
s/($old_name)/match_case($1, $mname)/eig;
print OUT $_;
}
close(OUT);
close(IN);
unlink($old_file_name);
}
}
Update: Added match_case.
Update: Added unlink as per later discussions.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|