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