in reply to Unable to rename the contents of the file using perl?

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.

  • Comment on Re: Unable to rename the contents of the file using perl?

Replies are listed 'Best First'.
Re^2: Unable to rename the contents of the file using perl?
by finddata (Sexton) on Mar 14, 2017 at 08:43 UTC
    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.

        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
        my problem is with renaming the contents of the files