in reply to Re: Unable to rename the contents of the file using perl?
in thread Unable to rename the contents of the 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^3: Unable to rename the contents of the file using perl?
by Corion (Patriarch) on Mar 14, 2017 at 09:00 UTC

    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.

      the output should be run as follows ./generate.pl -prjroot "/home/rpsa/DEMO/space" -outdir "/home/rpsa/html_output" -mapfile "/home/rpsa/DEMO/mapfile.txt So the input file that is mapfile as follows:
      PROJECT:DCMS_DEMO:prj1 BLOCK:de_top:blk1 BLOCK:new_block2:blk2 BLOCK:test:blk3 CHECKLIST:Block_DV:checklist1 CHECKLIST:Block_Physical_design_checklist:checklist2 CHECKLIST:CAD_checklist:checklist3 CHECKLIST:DFT:checklist4 CHECKLIST:Formality_DCT_Vs_ICC:checklist5 CHECKLIST:Formality_RTL_Vs_DCT:checklist6 CHECKLIST:FrontEnd_Design_Checklist:checklist7 CHECKLIST:Fullchip_Physical_design_checklist:checklist8 CHECKLIST:ICC_DPMHV_Checklist:checklist9 CHECKLIST:ICC_L2_Checklist:checklist10 CHECKLIST:ICC_Top_level_Checklist:checklist11 CHECKLIST:Overall_DV:checklist12 CHECKLIST:Power_analysis_checklist:checklist13 CHECKLIST:Synthesis_L2_checklist:checklist14 CHECKLIST:Timing_analysis_checklist:checklist15 CHECKLIST:checklist_tmp:checklist17 CHECKLIST:synthesis:checklist16 CHECKLIST:test:checklist18 CHECKLIST:test_copy_checklist:checklist19

        And where is your problem?

        Is your problem in reading the list file? Is your problem in renaming the files to the wrong names? Is your problem in not renaming the files at all?´

      my problem is with renaming the contents of the files

        If you run this SSCCE then you see where one problem is

        #!perl use strict; my $map = { one =>1, two =>2, three=>3, }; my ($regex) = map {qr /\b(?:$_)\b/ } join '|', map {quotemeta} keys %$ +map; change_file('test.txt',$map,$regex); 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; #<-- error here should be $$str close $fh; } < #test.txt __DATA__ one two three four five
        poj

        What do you mean by "renaming the contents of the files"?

        A file has a name and has content. The rename system call changes the name, not the content.

        If you want to change the content, you will need to rewrite the file using open and print.

        Please go back to step one and describe in short, simple English sentences what you have, and what you want, and what steps you think you need to take to get from the one place to the other.

        I think you're confusing rename with rewrite.