Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^2: rename issues

by sunadmn (Curate)
on Nov 29, 2004 at 17:39 UTC ( #410977=note: print w/ replies, xml ) Need Help??


in reply to Re: rename issues
in thread rename issues

yeah I fixed that forgot the i switch to my switch, but the issue I have now is still that I want to do the work in the new file not the old file and it's not working I know I am missing something stupid just not sure what I am missing.

SUNADMN
USE PERL


Comment on Re^2: rename issues
Re^3: rename issues
by ikegami (Pope) on Nov 29, 2004 at 17:43 UTC

    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.

      Yes this is exactly correct thank you much for fixing my problem.
      SUNADMN
      USE PERL
      Ok so the only thing I am missing here is that I do no want to make a backup of the old file all I want to do is rename the old to the new then open the new for writing and make the switches in the new file.
      SUNADMN
      USE PERL
        You don't want to keep the old file? Add unlink($old_file_name); at the end. You can't read from and write to the same file at the same time, so you have to do it at the end.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2014-08-31 03:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (294 votes), past polls