Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

rename issues

by sunadmn (Curate)
on Nov 29, 2004 at 17:20 UTC ( #410971=perlquestion: print w/ replies, xml ) Need Help??
sunadmn has asked for the wisdom of the Perl Monks concerning the following question:

*****UPDATE*****

I found the issue in the rename and fixed it I forgot to change a scalar name please disregard.

*****UPDATE*****


Hello all I have a quick question about some code I wrote to rename files under a given dir and then open the file and replace a scalar with another. I have the files being created with the new name, but I also still have the old copy with the old name there, issue number 1. The other issue I have is that the output in the new file is not what I expected, so if this code is correct it should look for the text "example" and switch it with the scalar $mname globaly in the new file this is not happening in the new file but the old file it is.

Example of the sdiff:
sdiff example_user_menu.htm pnDiagram_user_main.htm
Old file output:
<!--[* $Id: pnDiagram_user_menu.htm,v 1.6 2004/01/06 15:17:20 markwes +t Exp $ *]--> <!--[pngetstatusmsg]--> <br /> <div class="pn-title"> <!--[* To get the results of a function for using in another function +, you *]--> <!--[* need to capture its output! AFAIK currently there is no other +way *]--> <!--[capture name="heading"]--><!--[pnml name="_EXAMPLE"]--><!--[/capt +ure]--> <!--[pnimg src="heading.gif" alt=$smarty.capture.heading]--> </div> <div class="pn-menuitem-title"> <a href="<!--[pnmodurl modname="pnDiagram" type="user" func="view"]- +->"><!--[pnml name="_EXAMPLEVIEW"]--></a> </div> <br />
New file output:
<!--[* $Id: example_user_menu.htm,v 1.6 2004/01/06 15:17:20 markwest +Exp $ *]--> <!--[pngetstatusmsg]--> <br /> <div class="pn-title"> <!--[* To get the results of a function for using in another function +, you *]--> <!--[* need to capture its output! AFAIK currently there is no other +way *]--> <!--[capture name="heading"]--><!--[pnml name="_EXAMPLE"]--><!--[/capt +ure]--> <!--[pnimg src="heading.gif" alt=$smarty.capture.heading]--> </div> <div class="pn-menuitem-title"> <a href="<!--[pnmodurl modname="Example" type="user" func="view"]--> +"><!--[pnml name="_EXAMPLEVIEW"]--></a> </div> <br />
Here is what I have for code:
#!/usr/bin/perl use strict; use warnings; use File::Find; # We need to gather some info from the user # here for module name print "Please enter your module name\n"; print "This would be the name of the directory of your module\n"; my $mname = <STDIN>; chomp($mname); # Now we need to know where to look to make # the changes to files on the system this # will be our base directory to search through print "Please enter your modules base directory\n"; print "Example would be /var/www/postnuke/modules/YOUR_MODULE/\n"; my $base = <STDIN>; chomp($base); my $old_name = 'example'; my $old_name2 = 'Example'; find(\&wanted, $base); sub wanted { if ((-f $File::Find::name) && ($File::Find::name !~ m/\.bak/)) { my $file = $File::Find::name; (my $new = $file) =~ s/$old_name/$mname/; rename($file, $new); open(IN, $new) or die($!); open(OUT, '>' . $file) or die($!); while (<IN>) { s/$old_name/$mname/g; s/$old_name2/$mname/g; print OUT $_; } close(OUT); close(IN); } }
SUNADMN
USE PERL

Comment on rename issues
Select or Download Code
Re: rename issues
by ikegami (Pope) on Nov 29, 2004 at 17:36 UTC

    Is the problem the EXAMPLE in the new output? You're doing case sensitive substitutions, and you never look for EXAMPLE in uppercase.

      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

        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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2014-09-20 22:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (163 votes), past polls