blib/lib/App/unichar.pm

filter: Statementstmt 45.2% 33 / 73 Branchbran 4.5% 1 / 22 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 64.2% 9 / 14 Podpod 0.0% 0 / 3 totaltotal 37.3% 43 / 115 CC 14 SCAR 38.5
CC
14
Cov
35%
run
110.0
output
30.0
__ANON__
2.0
CRAP 46.9
↑ Summary
1 #!perl
2  
3 package App::unichar;
4  
5 1 ▶use 5.026;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use open qw(:std :utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use experimental qw(signatures);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 our $VERSION = '0.014';
12  
13 =encoding utf8
14  
15 =pod
16  
17 =head1 NAME
18  
19 App::unichar - get info about a character
20  
21 =head1 SYNOPSIS
22  
23 Call it as a program with a name, character, or hex code number:
24  
25 % perl lib/App/unichar.pm 'CHECK MARK'
26 Processing CHECK MARK
27 match type name
28 code point U+2713
29 decimal 10003
30 name CHECK MARK
31 character ✓
32  
33 % perl lib/App/unichar.pm ✓
34 Processing ✓
35 match type grapheme
36 code point U+2713
37 decimal 10003
38 name CHECK MARK
39 character ✓
40  
41 % perl lib/App/unichar.pm 0x2713
42 Processing 0x2713
43 match type code point
44 code point U+2713
45 decimal 10003
46 name CHECK MARK
47 character ✓
48  
49 =head1 DESCRIPTION
50  
51 I use this as a little command-line program to quickly convert between
52 values of characters.
53  
54 =head1 AUTHOR
55  
56 brian d foy, C<briandfoy@pobox.com>.
57  
58 =head1 SOURCE AVAILABILITY
59  
60 This module is in Github:
61  
62 https://github.com/briandfoy/app-unichar
63  
64 =head1 COPYRIGHT & LICENSE
65  
66 Copyright 2011-2024 brian d foy
67  
68 This module is licensed under the Artistic License 2.0.
69  
70 =cut
71  
72 1 ▶use Encode qw(decode);
Subroutine BEGIN: called CC=1 SCAR=0.0
73 1 ▶use I18N::Langinfo qw(langinfo CODESET);
Subroutine BEGIN: called CC=1 SCAR=0.0
74  
75 1 ▶use charnames ();
Subroutine BEGIN: called CC=1 SCAR=0.0
76 1 ▶use List::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
77  
78 1 binmode STDOUT, ':utf8';
79  
80 1 my %r = (
81 u => qr/(?:U\+?(?<hex>[0-9A-F]+))/i,
82 h => qr/(?:0x(?<hex>[0-9A-F]+))/i,
83 d => qr/(?:(?<int>[0-9]+))/,
84 );
85  
86 my %transformation = (
87 0 ▶ 'hex' => sub { hex $_[0] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
88 0 ▶ 'int' => sub { $_[0] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
89 1 );
90  
91 1 my $codeset = langinfo(CODESET);
92 0 @ARGV = map { decode $codeset, $_ } @ARGV;
93  
94 1 ▶run( @ARGV ) unless caller;
Branch: 1 branch
BranchTrueFalseTotal
unless caller 0 1 1
95  
96 0 ▶sub run (@args) {
Subroutine run: not called CC=10 SCAR=47.0
Pod undocumented
97 0 foreach ( @args ) {
98 0 say "Processing $_";
99  
100 0 ▶ if( / \A (?: $r{u} | $r{h} | $r{d} ) \z /x ) {
Branch: 4 branches
BranchTrueFalseTotal
if (/ \A (?: $r{'u'} | $r{'h'} | $r{'d'} ) \z /ux) { } 0 0 0
elsif (/ \A ([A-Z\s]{2,}) \z /iux) { } 0 0 0
elsif (/ \A (\X) \z /ux) { } 0 0 0
elsif (/ \A r: ([A-Z\s]{2,}) \z /iux) { } 0 0 0
101 0 my( $key ) = keys %+;
102 0 my $code = $transformation{$key}( $+{$key} );
103 0 output( $code, 'code point' );
104 }
105 elsif( / \A ([A-Z\s]{2,}) \z /ix ) {
106 0 my $code = eval { charnames::vianame( uc($1) ) };
107 0 ▶ unless( defined $code ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $code) 0 0 0
108 0 say "\tCouldn't match <$1> to a code name";
109 0 next;
110 }
111 0 output( $code, 'name' );
112 }
113 elsif( / \A (\X) \z /x ) {
114 0 output( ord( $1 ), 'grapheme' );
115 }
116 elsif( / \A r: ([A-Z\s]{2,}) \z /ix ) { # new regex mode
117 0 state $names = name_list();
118 0 say "In elsif";
119 0 my $pattern = s/\Ar://r;
120 0 $pattern = eval{ qr/$pattern/i };
121 0 ▶ if( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
122 0 warn "Invalid pattern --> $pattern ---> $@\n";
123 0 exit(4);
124 }
125  
126 0 foreach my $name ( keys $names->%* ) {
127 0 say "Tring $name";
128 0 ▶ next unless $name =~ m/$pattern/;
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /$pattern/u 0 0 0
129 0 output( $names->{$name}, 'pattern' );
130 }
131 }
132 else {
133 0 say "\tInvalid character, codepoint, or pattern --> $_\n";
134 0 next;
135 }
136 }
137 }
138  
139 0 ▶sub name_list () {
Subroutine name_list: not called CC=1 SCAR=6.9
Pod undocumented
140 0 state $names = { map { charnames::viacode($_), $_ } 0 .. 0x3FFFF };
141 0 return $names;
142 }
143  
144 0 ▶sub output ( $code, $match ) {
Subroutine output: not called CC=5 SCAR=34.0
Pod undocumented
145 0 my $hex = sprintf 'U+%04X', $code;
146 0 my $char = chr( $code );
147 0 ▶ $char = '<unprintable>' if $char !~ /\p{Print}/;
Branch: 1 branch
BranchTrueFalseTotal
unless $char =~ /\p{Print}/u 0 0 0
148 0 ▶ $char = '<whitespace>' if $char =~ /\p{Space}/;
Branch: 1 branch
BranchTrueFalseTotal
if $char =~ /\p{Space}/u 0 0 0
149 0 ▶ $char = '<control>' if $char =~ /\p{Control}/;
Branch: 1 branch
BranchTrueFalseTotal
if $char =~ /\p{Control}/u 0 0 0
150  
151 0 ▶ my $name = charnames::viacode( $code ) // '<no name found>';
Condition0%0/2
charnames::viacode($code) // "<no name found>"
l!l
00
MC/DC0%0/1
charnames::viacode($code) // "<no name found>"
charnames::viacode($code)
Truth table0%0/2
A
A= charnames::viacode($code)
Aresult
11
00
152  
153 0 print <<~"HERE";
154 match type $match
155 code point $hex
156 decimal $code
157 name $name
158 character $char
159  
160 HERE
161  
162 }
↑ Summary