-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_missing_shebangs.pl
52 lines (41 loc) · 941 Bytes
/
add_missing_shebangs.pl
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
#!/usr/bin/perl
use 5.010;
use strict;
use warnings;
use File::Find qw(find);
my %lookup = (
sf => '#!/usr/bin/ruby',
pl => '#!/usr/bin/perl',
);
my $ext_re = qr/\.(pl|sf)\z/;
my @files;
find(
{
no_chdir => 1,
wanted => sub {
if (/$ext_re/) {
my $ext = $1;
open my $fh, '<', $_ or die "$_: $!";
chomp(my $line = <$fh>);
if ($line ne $lookup{$ext}) {
push @files, [$_, $lookup{$ext}];
}
close $fh;
}
}
},
glob('*')
);
foreach my $entry (@files) {
my ($file, $ext) = @$entry;
say "Modifying: $file\n";
open my $fh, '<', $file or die "$file: $!";
my $content = do {
local $/;
<$fh>;
};
close $fh;
open my $out_fh, '>', $file or die "$file: $!";
print $out_fh "$ext\n$content";
close $out_fh;
}