Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^4: How to rename the contents in perl?

by finddata (Sexton)
on Mar 20, 2017 at 09:11 UTC ( #1185238=note: print w/replies, xml ) Need Help??


in reply to Re^3: How to rename the contents in perl?
in thread How to rewrite the contents of the file using perl?

Following part is not working for me.
sub if_config_file_rename_and_modify_it { my ( $name, $name_map, $regex ) = @_; if (( my $base_name = $name) =~ s/\.config$// ) { if ( $name_map->{$base_name} ) { my $new_name = $name_map->{$base_name} . '.config'; rename_file_or_dir( $name, $new_name ); change_file( $new_name, $name_map, $regex ); } } } sub change_file { my ( $fn, $map, $regex ) = @_; print "enter\n"; open ( my $fh, '<', $fn ) or die "Could not open file '$fn': $!"; my $str = do { local $/; <$fh> }; close $fh; my $num_replacements = $str =~ s/($regex)/$map->{$1}/ge; if ( $num_replacements ) { write_new_file( $fn, \$str ); } } sub write_new_file { print "enter1\n"; my ( $fn, $str ) = @_; open ( my $fh, '>', $fn ) or die "Could not open file '$fn': $!"; print $fh $$str; close $fh; } sub rename_file_or_dir { my ( $name, $new_name ) = @_; File::Copy::move( $name, $new_name ) or die "Could not rename '$name' as '$new_name': $!"; #print $name; }

Replies are listed 'Best First'.
Re^5: How to rename the contents in perl?
by Corion (Pope) on Mar 20, 2017 at 09:18 UTC

    Let me repeat myself:

    Please rewrite your program so that it only contains the parts that don't work but still is a complete program.

    We can only help you if we can reproduce your problem, but if you don't give us a complete program, we cannot reproduce your problem.

    We can only help you if you give us a short program that reproduces only your problem. This allows us to focus on the problematic part of your program.

Re^5: How to rename the contents in perl?
by huck (Parson) on Mar 20, 2017 at 09:20 UTC

    For something you claim is not working there is a distinct lack of any debugging code.

    why do you feel it is not working, does it die?

    as has been suggested before you should add debugging prints before and after the tests , and as you enter and leave each subroutine.

      I had used print statements to test those statements are not printed.Thats why i finalyzed my program is not working

        If your subroutines do not get entered at all, the problem is not with the subroutines but with the code that calls them.

        Please debug it some more and find out where your program does not do what you think it should.

        Add a print statement at the start of each loop to print out the current value of the loop variable.

        Add a print statement in every if block and also every else block to see what path your code takes. If there is no else block, add it.

        Add a print statement on every line to find out where your program does not enter your subroutine.

        This will help you to find out where your program does not do what you think it should do.

Re^5: How to rename the contents in perl?
by Anonymous Monk on Mar 20, 2017 at 15:39 UTC
    It looks like it might work, but you didn't tell us how you're calling it. $regex needs to be something like:
    $regex = join '|', map { '\\b' . quotemeta($_) . '\\b' } keys %$map;
    Of course, the substitution might change things in the config file that it shouldn't. No way for us to know.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (4)
As of 2020-01-25 06:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?