-
Notifications
You must be signed in to change notification settings - Fork 33
/
reformat_literal_perl_strings.pl
executable file
·59 lines (42 loc) · 1.69 KB
/
reformat_literal_perl_strings.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
#!/usr/bin/perl
# Daniel "Trizen" Șuteu
# Date: 12 November 2017
# https://github.com/trizen
# Reformat the literal quoted strings in a Perl source code, using Perl::Tokenizer and Data::Dump.
# Example:
# 'foo姓bar' -> "foo\x{59D3}bar"
# '\'foo\'' -> "'foo'"
# The literal quoted strings (quoted as: q{...}, qq{...}, '...' or "...") will be reformatted as "...".
# Strings which (potentially) include variable interpolations, are ignored.
# The input source code must be UTF-8 encoded.
use utf8;
use 5.018;
use warnings;
use open IO => ':encoding(UTF-8)', ':std';
use Data::Dump qw(pp);
use Perl::Tokenizer qw(perl_tokens);
# usage: perl script.pl < source.pl
my $code = join('', <>);
perl_tokens {
my ($name, $i, $j) = @_;
if ( $name eq 'single_quoted_string'
or $name eq 'double_quoted_string'
or $name eq 'qq_string'
or $name eq 'q_string') {
my $str = substr($code, $i, $j - $i);
my $eval_code = join(
';',
'my $str = qq{' . quotemeta($str) . '}', # quoted string
'die if $str =~ tr/@$//', # skip strings with interpolation
'$str = eval $str', # evaluate string
'die if $@', # check the status of eval()
'$str', # string content
);
my $raw_str = eval($eval_code);
if (defined($raw_str) and !$@) {
print scalar pp($raw_str);
return;
}
}
print substr($code, $i, $j - $i);
} $code;