Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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.


In reply to Re^3: rename issues by ikegami
in thread rename issues by sunadmn

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2024-04-18 11:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found