-
Notifications
You must be signed in to change notification settings - Fork 0
/
perl7.pl
executable file
·194 lines (156 loc) · 4.91 KB
/
perl7.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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/perl
#> -----------------------------------------------------------------------------
#> Copyright 2020 Andrew A. V. Murphy All Rights Reserved - github.com/aavmurphy
#> -----------------------------------------------------------------------------
# no feature qw(indirect);
# use open qw(:std :utf8);
use utf8;
use strict;
use warnings;
use feature qw(signatures);
no warnings qw(experimental::signatures);
#> -----------------------------------------------------------------------------
# --------------------------------------------------------------------------
# Simple script to make your .pl, .pm, .cgi scripts Perl 7 Ready
# See github.com/aavmurphy/perl7ready
# --------------------------------------------------------------------------
# Usage:
# 1. Copy perl7.yaml.example to perl7.yaml and edit it
#
# 2. perl7ready ./dir ./file ./another-dir
#
# 3. Try out on a few scripts first. If something goes wrong, backups are in /tmp,
# e.g. /dir/file => /tmp/dir/file
#
# 4. You might have to install YAML, sudo cpanm YAML
#
# --------------------------------------------------------------------------
package Perl7ready
{
use YAML;
use IO::All -utf8;
use File::Spec;
sub new ( $class )
{
my $self = bless {};
my $config = YAML::LoadFile( "perl7.config" );
$self->{comment_prefix} = $config->{COMMENT_PREFIX} || '';
$self->{preamble} = $config->{PREAMBLE} || '';
$self->{pragmas} = $config->{PRAGMAS} || '';
map { die "no param $_\n" if ! $self->{$_} } qw( comment_prefix preamble pragmas );
return $self;
}
sub read( $self, $file )
{
$self->{file} = File::Spec->rel2abs( $file ); # ../fred.pl => /full/path/fred.pl;
$self->{perl} = io( $self->{file} )->slurp;
$self->{backup} = $self->{perl};
#
warn sprintf( "%6d : %s\n", length $self->{perl}, $file );
}
sub remove_comment_prefix( $self )
{
# remove any lines starting with comment_prefix
die "no standard preamble (comment) prefix" if ! $self->{comment_prefix} || ! $self->{comment_prefix} =~ /^#/;
my $prefix = quotemeta $self->{comment_prefix};
$self->{perl} =~ s/^ \s* $prefix .*? \n//mxg;
}
sub insert_preamble( $self )
{
$self->_insert( $self->{preamble} );
}
sub insert_pragmas( $self )
{
my @pragmas = split /\n/, $self->{pragmas} ;
# remove old ones
foreach my $pragma ( @pragmas )
{
$pragma =~ s/^\s*\#\s*//; # remove comment '#', so catches '# use strict' and 'use strict'
$pragma =~ s/^\s*(use|no)\s*//; # remove the 'use' or 'no' from 'use fred', so catched 'use fred' and 'no fred'
$pragma =~ s/\s*;\s*//; # remove the ; at the end, so 'use strict', so catches 'use strict ;'
next if ! $pragma;
$pragma = quotemeta $pragma;
$self->{perl} =~ s/^ \s* \#? \s* (use|no) \s* $pragma \s* ; \s*? \n//xmg;
}
$self->_insert( $self->{pragmas} );
}
sub _insert( $self, $insert )
{
# insert
if ( $self->{perl} =~ /^\#!/ )
{
my ( $hash_bang, $code ) = split /\n/, $self->{perl}, 2;
$self->{perl} = "$hash_bang\n$insert$code";
}
else
{
$self->{perl} = "$insert$self->{perl}";
}
}
sub signatures( $self )
{
# FROM
# sub create_gpx
# {
# my ( $self ) = @_;
#
# TO
# sub create_gpx( $self )
# {
# if you do my $self = shift; my ( $a ) = @_ ; you will have to be cleverer here!
$self->{perl} =~ s#
sub \s* ( [\w\d\_]+ ) \s*
\{ \s*
my \s* \( \s* ( .*? ) \s* \) \s* = \s* \@_ \s* ; \s*
#sub $1( $2 )\n\t{\n\t#sgx;
}
sub indirect( $self )
{
# FROM
# my $osm = new Cablechip::GPX::Openstreetmap( $self->{gps} );
# TO
# my $osm = Cablechip::GPX::Openstreetmap->new( $self->{gps} );
# and new, not: $new %new @new *new :new _new
my @code = split /\n/, $self->{perl};
foreach my $i ( 0 .. $#code )
{
next if $code[ $i ] =~ m/#.*?new\s+/; # skip the word new inside a comment
$code[ $i ] =~ s# (?<![$@%_\*\:])new \s+ ( [\w\d\:\_]+ ) \s* #$1->new#xg;
}
$self->{perl} = join "\n", @code;
}
sub line_feed_eof( $self )
{
$self->{perl} =~ s/\r$//mg; # when saving as utf8, get lots of spurious ^M, on unix at least (from windows line feeds)
$self->{perl} =~ s/(?<!\n)$/\n/; # make sure \n at eof
}
sub backup_save( $self )
{
# backup
my $backup_file = "/tmp/$self->{file}";
$self->{backup} > io( $backup_file )->assert;
# save
$self->{perl} > io( $self->{file} );
}
}
# --------------------------------------------------------
my ( $dir ) = "@ARGV";
die "usage: $0 /list/of/dirs /and/another/dir\n" if ! $dir;
warn "dirs: $dir\n";
my @files = qx( find $dir -type f -regex '.*\\.\\(pm\\|pl\\|cgi\\)' );
@files = grep !/DAV/, @files;
@files = map { chomp $_; $_ } @files ;
warn "files: ". scalar( @files ) . "\n";
#
my $ready = Perl7ready->new();
foreach my $file ( sort @files )
{
$ready->read( $file );
$ready->signatures;
$ready->indirect;
$ready->remove_comment_prefix;
$ready->insert_pragmas;
$ready->insert_preamble;
$ready->line_feed_eof;
$ready->backup_save;
}