-
Notifications
You must be signed in to change notification settings - Fork 33
/
perl_code_analyzer.pl
executable file
·183 lines (151 loc) · 4.04 KB
/
perl_code_analyzer.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
#!/usr/bin/perl
# Author: Daniel "Trizen" Șuteu
# License: GPLv3
# Date: 04 January 2015
# Website: https://github.com/trizen
#
## Analyze your Perl code and see whether you are or not a true Perl hacker!
#
# More info about this script:
# https://trizenx.blogspot.com/2015/01/perl-code-analyzer.html
use utf8;
use 5.010;
use strict;
use warnings;
use IPC::Open2 qw(open2);
use Encode qw(encode_utf8 decode_utf8);
use Getopt::Long qw(GetOptions);
use Algorithm::Diff qw(LCS_length);
use Perl::Tokenizer qw(perl_tokens);
my $strict_level = 1;
my %ignored_types;
sub help {
my ($code) = @_;
print <<"HELP";
usage: $0 [options] [file] [...]
options:
--strict [level] : sets the strictness level (default: $strict_level)
Valid strict levels:
>= 1 : ignores strings, PODs, comments, spaces and semicolons
>= 2 : ignores round parentheses
>= 3 : ignores here-documents, (q|qq|qw|qx) quoted strings
>= 4 : ignores hex and binary literal numbers
If level=0, any stricture will be disabled.
HELP
exit($code // 0);
}
GetOptions('strict=i' => \$strict_level,
'help|h' => sub { help(0) },)
or die("Error in command line arguments\n");
@ARGV || help(2);
if ($strict_level >= 1) {
@ignored_types{
qw(
pod
data
comment
vertical_space
horizontal_space
other_space
semicolon
double_quoted_string
single_quoted_string
)
} = ();
}
if ($strict_level >= 2) {
@ignored_types{
qw(
parenthesis_open
parenthesis_close
)
} = ();
}
if ($strict_level >= 3) {
@ignored_types{
qw(
heredoc
heredoc_beg
q_string
qq_string
qw_string
qx_string
)
} = ();
}
if ($strict_level >= 4) {
@ignored_types{
qw(
hex_number
binary_number
)
} = ();
}
sub deparse {
my ($code) = @_;
local (*CHLD_IN, *CHLD_OUT);
my $pid = open2(\*CHLD_OUT, \*CHLD_IN, $^X, '-MO=Deparse', '-T');
print CHLD_IN encode_utf8($code);
close(CHLD_IN);
my $deparsed = do {
local $/;
decode_utf8(<CHLD_OUT>);
};
waitpid($pid, 0);
my $child_exit_status = $? >> 8;
if ($child_exit_status != 0) {
die "B::Deparse failed with code: $child_exit_status\n";
}
return $deparsed;
}
sub get_tokens {
my ($code) = @_;
my @tokens;
perl_tokens {
my ($token) = @_;
if (not exists $ignored_types{$token}) {
push @tokens, $token;
}
}
$code;
return @tokens;
}
foreach my $script (@ARGV) {
print STDERR "=> Analyzing: $script\n";
my $code = do {
open my $fh, '<:utf8', $script;
local $/;
<$fh>;
};
my $d_code = eval { deparse($code) };
$@ && do { warn $@; next };
my @types = get_tokens($code);
my @d_types = get_tokens($d_code);
if (@types == 0 or @d_types == 0) {
warn "This script seems to be empty! Skipping...\n";
next;
}
my $len = LCS_length(\@types, \@d_types) - abs(@types - @d_types);
my $score = (100 - ($len / @types * 100));
if ($score >= 60) {
printf("WOW!!! We have here a score of %.2f! This is obfuscation, isn't it?\n", $score);
}
elsif ($score >= 40) {
printf("Outstanding! This code seems to be written by a true legend! Score: %.2f\n", $score);
}
elsif ($score >= 20) {
printf("Amazing! This code is very unique! Score: %.2f\n", $score);
}
elsif ($score >= 15) {
printf("Excellent! This code is written by a true Perl hacker. Score: %.2f\n", $score);
}
elsif ($score >= 10) {
printf("Awesome! This code is written by a Perl expert. Score: %.2f\n", $score);
}
elsif ($score >= 5) {
printf("Just OK! We have a score of %.2f! This is production code, isn't it?\n", $score);
}
else {
printf("What is this? I guess it is some baby Perl code, isn't it? Score: %.2f\n", $score);
}
}