blib/script/perl_types_refactor_cpp_namespaces.pl untested

filter: Statementstmt 0.0% 0 / 60 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 7 Podpod n/a totaltotal 0.0% 0 / 101 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 #!/usr/bin/env perl
2 # bin/dev/perl_types_refactor_cpp_namespaces.pl
3 # Refactor C++ type names from underscore style to namespace:: style and update typemap.perl
4  
5 use strict;
6 use warnings;
7 use File::Find;
8 use File::Spec;
9 use File::Basename;
10 use File::Copy qw(copy);
11  
12 # Step 1: load mapping from generated refactor map
13 require 'bin/dev/perl_types_refactor_names_map.pm';
14 my $mapref = $Perl::Types::RefactorNamesMap::refactor_names_map;
15 # Merge cpp and h sections into one map for C++ refactoring
16 my %map = (
17 %{ $mapref->{cpp} // {} },
18 %{ $mapref->{h} // {} },
19 );
20  
21 # Build typemap symbol mapping: old_name => new_name with '::' => '__'
22 my %typemap_map = map {
23 my $old = $_;
24 my $new = $map{$old};
25 $new =~ s/::/__/g;
26 ($old => $new);
27 } keys %map;
28  
29 # Step 2: collect files to process
30 my @files;
31 # C++ sources (.cpp, .h)
32 find(sub { push @files, $File::Find::name if -f && /\.(?:cpp|h)$/ }, 'lib/Perl/Structure');
33 # typemap file
34 push @files, 'lib/typemap.perl' if -f 'lib/typemap.perl';
35  
36 die "No files found to refactor\n" unless @files;
37  
38 for my $file (@files) {
39 # read entire file
40 open my $in, '<', $file or warn "Cannot open $file: $!" and next;
41 local $/;
42 my $text = <$in>;
43 close $in;
44 my $orig = $text;
45  
46 if ($file =~ /\.(?:cpp|h)$/) {
47 # apply C++ type name mapping
48 for my $old (sort { length($b) <=> length($a) } keys %map) {
49 my $new = $map{$old};
50 $text =~ s{(?<![A-Za-z0-9_:])\Q$old\E(?![A-Za-z0-9_:])}{$new}g;
51 }
52 }
53 elsif ($file eq 'lib/typemap.perl') {
54 # apply typemap symbol mapping
55 for my $old (sort { length($b) <=> length($a) } keys %typemap_map) {
56 my $new = $typemap_map{$old};
57 $text =~ s{(?<![A-Za-z0-9_])\Q$old\E(?![A-Za-z0-9_])}{$new}g;
58 }
59 }
60  
61 # write back if changed
62 if ($text ne $orig) {
63 # backup original
64 copy($file, "$file.bak") or warn "Failed to backup $file: $!";
65 open my $out, '>', $file or die "Cannot write $file: $!";
66 print $out $text;
67 close $out;
68 print "Refactored $file\n";
69 }
70 else {
71 print "No changes in $file\n";
72 }
73 }
74  
75 print "C++ namespace refactoring complete.\n";
76 exit 0;