finddata has asked for the wisdom of the Perl Monks concerning the following question:

Here is the code where i tried to rename the contents of the file by mathcing with text file using perl?

Am facing with this part of code especially change file,rename file and write file subroutine

sub rename_dirs { my ( $top_dir, $name_map, $regex ) = @_; opendir (my $dh, $top_dir) or die "Can't open $top_dir: $!"; my $save_dir = getcwd(); chdir $top_dir; while (my $name = readdir $dh) { next if ($name eq '.') or ($name eq '..'); if ( ( -d $name ) && ( exists $name_map->{$name} ) ) { print $name; my $new_name = $name_map->{$name}; #print $new_name; rename_file_or_dir( $name, $new_name ); $name = $new_name; #print $name; } elsif ( -f $name ) { if (( my $base_name = $name) =~ s/\.config$// ) { if ( $name_map->{$base_name} ) { my $new_name = $name_map->{$base_name} . '.config' +; # print $new_name; rename_file_or_dir( $name, $new_name ); change_file( $new_name, $name_map, $regex ); } } } rename_dirs( $name, $name_map, $regex ) if -d $name; } chdir $save_dir; } sub change_file { my ( $fn, $map, $regex ) = @_; # print $fn; open ( my $fh, '<', $fn ) or die "Could not open file '$fn': $!"; my $str = do { local $/; <$fh> }; print $str; close $fh; my $num_replacements = $str =~ s/($regex)/$map->{$1}/ge; if ( $num_replacements ) { write_new_file( $fn, \$str ); } } sub write_new_file { 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': $!"; } sub read_map { my ( $fn ) = @_; my %name_map; open( my $fh, '<', $fn ) or die "Could not open file '$fn': $!"; while( my $line = <$fh> ) { chomp $line; my @fields = split /:/, $line; # print @fields; if ( @fields == 3 ) { $name_map{$fields[2]} = $fields[1]; } } close $fh; return \%name_map; }

Replies are listed 'Best First'.
Re: Unable to rename the contents of the file using perl?
by Corion (Patriarch) on Mar 14, 2017 at 08:03 UTC

    Which part of the code is the problematic part?

    Can you remove all the other parts from your program, to make the problematic part easier to see for everybody?

    See also SSCCE.

      Am facing with this part of code especially change file,rename file and write file subroutine
      sub rename_dirs { my ( $top_dir, $name_map, $regex ) = @_; opendir (my $dh, $top_dir) or die "Can't open $top_dir: $!"; my $save_dir = getcwd(); chdir $top_dir; while (my $name = readdir $dh) { next if ($name eq '.') or ($name eq '..'); if ( ( -d $name ) && ( exists $name_map->{$name} ) ) { print $name; my $new_name = $name_map->{$name}; #print $new_name; rename_file_or_dir( $name, $new_name ); $name = $new_name; #print $name; } elsif ( -f $name ) { if (( my $base_name = $name) =~ s/\.config$// ) { if ( $name_map->{$base_name} ) { my $new_name = $name_map->{$base_name} . '.config' +; # print $new_name; rename_file_or_dir( $name, $new_name ); change_file( $new_name, $name_map, $regex ); } } } rename_dirs( $name, $name_map, $regex ) if -d $name; } chdir $save_dir; } sub change_file { my ( $fn, $map, $regex ) = @_; # print $fn; open ( my $fh, '<', $fn ) or die "Could not open file '$fn': $!"; my $str = do { local $/; <$fh> }; print $str; close $fh; my $num_replacements = $str =~ s/($regex)/$map->{$1}/ge; if ( $num_replacements ) { write_new_file( $fn, \$str ); } } sub write_new_file { 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': $!"; } sub read_map { my ( $fn ) = @_; my %name_map; open( my $fh, '<', $fn ) or die "Could not open file '$fn': $!"; while( my $line = <$fh> ) { chomp $line; my @fields = split /:/, $line; # print @fields; if ( @fields == 3 ) { $name_map{$fields[2]} = $fields[1]; } } close $fh; return \%name_map; }

        Without knowing the input data and the output of your program it is very hard for us to diagnose your problem.

        You can help us help you better by showing us a short, self-contained program that allows us to reproduce the problem you are facing.

        Please write your program in a way that it includes all necessary input data. If the input data is longer than 10 lines, reduce the input data to the relevant 10 lines.