http://www.perlmonks.org?node_id=1184635


in reply to Re: following if condition fails to print its variable using perl?
in thread following if condition fails to print its variable using perl?

my $name_map = read_map( $mapfile ); my ($regex) = map {qr /\b(?:$_)\b/ } join '|', map {quotemeta} keys %$ +name_map; my $top_dir = $output_dir; rename_dirs( $top_dir, $name_map, $regex ); 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 '..'); #print $name,"\n"; 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( $mapfile, $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> }; 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; }
i hope by above code you can understand the complete flow.am trying to match directories and files at same block that is rename_dirs.Here i am struck with print $new_name and above lines.Help me to get it.Thanks in advance
  • Comment on Re^2: following if condition fails to print its variable using perl?
  • Download Code

Replies are listed 'Best First'.
Re^3: following if condition fails to print its variable using perl?
by stevieb (Canon) on Mar 15, 2017 at 04:52 UTC

    That shows a lot of Perl code, but it doesn't explain what you're trying to do.

    We need to know what your *input* is, and what you want as *output*. What directories or files are you sending in? What directories or files do you want out?

    That's reasonably intermediate Perl code there with a lot of hack comments and other such things (definitely kudos for at a glance appearing strict, and 3-arg open), so you can't just expect to throw it up on Perlmonks and think we'll know what you're trying to do with it.

    Start over. Tell us your problem statement. "I have this problem, and I want this outcome.".

    Or: "I have this directory with these files: ..., and I want to do ... with each one of them."

      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
      this is the input file(i.e $mapfile)

        Did you want to change that file here

        change_file( $mapfile, $name_map, $regex );
        poj
Re^3: following if condition fails to print its variable using perl?
by huck (Parson) on Mar 15, 2017 at 06:15 UTC

    start here

    sub rename_dirs { my ( $top_dir, $name_map, $regex ) = @_; print "start rename dirs $top_dir, $name_map \n"; 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 '..'); print 'name :'.$name,"\n"; if ( ( -d $name ) && ( exists $name_map->{$name} ) ) { my $new_name = $name_map->{$name}; print 'newdir :'.$new_name."\n; rename_file_or_dir( $name, $new_name ); $name = $new_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 'newfile:'.$new_name."\n; # print $new_name; rename_file_or_dir( $name, $new_name ); change_file( $mapfile, $name_map, $regex ); } } } if ( -d $name) { print "down :'.$name."\n"; rename_dirs( $name, $name_map, $regex ); print "up :'.$name."\n"; } } chdir $save_dir; }
    See what you can determine from that

      Here,
      i had print as like print 'name :$name', ((exists $name_map->{$name} ) + ? "is in hash" : "is not in hash"), "\n"; above the if condition but it prints not in hash .without taking hash +how it can rename the contents
      Error: when i prints these statements. print 'name :$name', ((exists $name_map->{$name} ) ? "is in hash" : "is not in hash"), "\n";
      name :$nameis in hash name :$nameis not in hash name :$nameis in hash name :$nameis not in hash name :$nameis in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis in hash name :$nameis not in hash name :$nameis in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash name :$nameis not in hash

        print 'name :$name' uses single quotes and the $ is just text, try print 'name :'.$name or print "name :$name"