in reply to Re^2: rename issues
in thread rename issues

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.

Replies are listed 'Best First'.
Re^4: rename issues
by sunadmn (Curate) on Nov 29, 2004 at 18:08 UTC
    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.
        This is going to sound stupid but hwere exactly should I place the unlink at??

        SUNADMN
        USE PERL
Re^4: rename issues
by sunadmn (Curate) on Nov 29, 2004 at 18:05 UTC
    Yes this is exactly correct thank you much for fixing my problem.
    SUNADMN
    USE PERL