Commit | Line | Data |
---|---|---|
2986a63f JH |
1 | |
2 | ||
3 | print "\nModifying the '.t' files...\n\n"; | |
4 | ||
5 | use File::Basename; | |
6 | use File::Copy; | |
7 | ||
8 | ## Change the below line to the directory you want to process | |
9 | $DirName = "/perl/scripts/t"; | |
10 | ||
11 | $FilesTotal = 0; | |
12 | $FilesRead = 0; | |
13 | $FilesModified = 0; | |
14 | ||
15 | opendir(DIR, $DirName) or die "Unable to open the directory, $DirName for reading.\n"; | |
16 | @Dirs = readdir(DIR); | |
17 | ||
18 | foreach $DirItem(@Dirs) | |
19 | { | |
20 | $DirItem = $DirName."/".$DirItem; | |
21 | push @DirNames, $DirItem; # All items under $DirName directory is copied into an array. | |
22 | } | |
23 | ||
24 | foreach $FileName(@DirNames) | |
25 | { | |
26 | if(-d $FileName) | |
27 | { # If an item is a directory, then open it further. | |
28 | ||
29 | opendir(SUBDIR, $FileName) or die "Unable to open the directory, $FileName for reading.\n"; | |
30 | @SubDirs = readdir(SUBDIR); | |
31 | close(SUBDIR); | |
32 | ||
33 | foreach $SubFileName(@SubDirs) | |
34 | { | |
35 | if(-f $SubFileName) | |
36 | { | |
37 | &Process_File($SubFileName); # If file, process it. | |
38 | } | |
39 | else | |
40 | { | |
41 | $SubFileName = $FileName."/".$SubFileName; | |
42 | push @DirNames, $SubFileName; # If sub-directory, push it into the array. | |
43 | } | |
44 | } | |
45 | } | |
46 | else | |
47 | { | |
48 | if(-f $FileName) | |
49 | { | |
50 | &Process_File($FileName); # If file, process it. | |
51 | } | |
52 | } | |
53 | } | |
54 | ||
55 | close(DIR); | |
56 | ||
57 | print "\n\n\nTotal number of files present = $FilesTotal\n"; | |
58 | print "Total number of '.t' files read = $FilesRead\n"; | |
59 | print "Total number of '.t' files modified = $FilesModified\n\n"; | |
60 | ||
61 | ||
62 | ||
63 | ||
64 | # Process the file. | |
65 | sub Process_File | |
66 | { | |
67 | local($FileToProcess) = @_; # File name. | |
68 | local($Modified) = 0; | |
69 | ||
70 | ||
71 | if(!(-w $FileToProcess)) { | |
72 | # If the file is a read-only file, then change its mode to read-write. | |
73 | chmod(0777, $FileToProcess); | |
74 | } | |
75 | ||
76 | ||
77 | $base = basename($FileToProcess); # Get the base name | |
78 | $dir = dirname($FileToProcess); # Get the directory name | |
79 | ($base, $dir, $ext) = fileparse($FileToProcess, '\..*'); # Get the extension of the file passed. | |
80 | ||
81 | ## If the value of $FileToProcess is '/perl/scripts/t/pragma/warnings.t', then | |
82 | ## $dir = '/perl/scripts/t/pragma/' | |
83 | ## $base = 'warnings' | |
84 | ## $ext = '.t' | |
85 | ||
86 | ||
87 | # Do the processing only if the file has '.t' extension. | |
88 | if($ext eq '.t') { | |
89 | ||
90 | open(FH, "+< $FileToProcess") or die "Unable to open the file, $FileToProcess for reading and writing.\n"; | |
91 | @ARRAY = <FH>; # Get the contents of the file into an array. | |
92 | ||
93 | flock(FH, LOCK_EX); # Lock the file for safety purposes. | |
94 | foreach $Line(@ARRAY) # Get each line of the file. | |
95 | { | |
96 | if($Line =~ m/\@INC = /) | |
97 | { # If the line contains the string (@INC = ), then replace it | |
98 | ||
99 | # Replace "@INC = " with "unshift @INC, " | |
100 | $Line =~ s/\@INC = /unshift \@INC, /; | |
101 | ||
102 | $Modified = 1; | |
103 | } | |
104 | ||
105 | if($Line =~ m/push \@INC, /) | |
106 | { # If the line contains the string (push @INC, ), then replace it | |
107 | ||
108 | # Replace "push @INC, " with "unshift @INC, " | |
109 | $Line =~ s/push \@INC, /unshift \@INC, /; | |
110 | ||
111 | $Modified = 1; | |
112 | } | |
113 | } | |
114 | ||
115 | seek(FH, 0, 0); # Seek to the beginning. | |
116 | print FH @ARRAY; # Write the changed array into the file. | |
117 | flock(FH, LOCK_UN); # unlock the file. | |
118 | close FH; # close the file. | |
119 | ||
120 | $FilesRead++; # One more file read. | |
121 | ||
122 | if($Modified) { | |
123 | print "Modified the file, $FileToProcess\n"; | |
124 | $Modified = 0; | |
125 | ||
126 | $FilesModified++; # One more file modified. | |
127 | } | |
128 | } | |
129 | ||
130 | $FilesTotal++; # One more file present. | |
131 | } | |
132 |