File Coverage

blib/lib/Text/Control.pm
Criterion Covered Total %
statement 19 19 100.0
branch n/a
condition n/a
subroutine 5 5 100.0
pod 3 3 100.0
total 27 27 100.0


line stmt bran cond sub pod time code
1             package Text::Control;
2              
3 2     2   68951 use strict;
  2         5  
  2         59  
4 2     2   10 use warnings;
  2         3  
  2         564  
5              
6             our $VERSION = '0.4';
7              
8             my $CTRL_PATTERN = '[\x00-\x19\x7f-\xff]';
9              
10             =encoding utf-8
11              
12             =head1 NAME
13              
14             Text::Control - Transforms of control characters
15              
16             =head1 SYNOPSIS
17              
18             use Text::Control;
19              
20             Text::Control::to_dot("\x00\\Hi\x7fthere.\x80\xff"); # .\Hi.there...
21              
22             Text::Control::to_hex("\x00\\Hi\x7fthere.\x80\xff");
23             # \x00\\Hi\x7fthere.\x80\xff -- note the escaped backslash
24              
25             =head1 DESCRIPTION
26              
27             These are transforms that I find useful for debugging. Maybe you will, too?
28              
29             =head1 NONPRINTABLE BYTES
30              
31             This module considers byte numbers 32 - 126 to be “printable”; i.e., they
32             represent actual ASCII characters. Anything outside this range is thus
33             “nonprintable”.
34              
35             =head1 FUNCTIONS
36              
37             =head2 to_dot( OCTET_STRING )
38              
39             Transforms each nonprintable byte into a dot (C<.>, ASCII 46) and returns
40             the result.
41              
42             =cut
43              
44             sub to_dot {
45 1     1 1 514 my ($input) = @_;
46              
47 1         19 $input =~ s<$CTRL_PATTERN><.>g;
48              
49 1         5 return $input;
50             }
51              
52             =head2 to_hex( OCTET_STRING )
53              
54             Transforms each nonprintable byte into the corresponding \x.. sequence,
55             appropriate for feeding into
56             C. For example, a NUL byte comes out as C<\x00>.
57              
58             In order to make this encoding reversible, backslash characters (C<\>) are
59             double-escaped (i.e., C<\> becomes C<\\>).
60              
61             =cut
62              
63             sub to_hex {
64 2     2 1 5 my ($input) = @_;
65              
66 2         10 $input =~ s<\\><\\\\>g;
67 2         23 $input =~ s<($CTRL_PATTERN)><'\\x' . sprintf('%02x', ord $1)>ge;
  8         33  
68              
69 2         10 return $input;
70             }
71              
72             =head2 from_hex( FROM_TO_HEX )
73              
74             This transforms the result of C back into its original form.
75             I’m not sure this is actually useful :), but hey.
76              
77             =cut
78              
79             sub from_hex {
80 1     1 1 2 my ($input) = @_;
81              
82 1         5 $input =~ s<\\x([0-9a-f]{2})>ge;
  4         14  
83 1         4 $input =~ s<\\\\><\\>g;
84              
85 1         6 return $input;
86             }
87              
88             =head1 AUTHOR
89              
90             Felipe Gasper (FELIPE)
91              
92             =head1 REPOSITORY
93              
94             L
95              
96             =head1 COPYRIGHT
97              
98             This program is free software; you can redistribute
99             it and/or modify it under the same terms as Perl itself.
100              
101             The full text of the license can be found in the
102             LICENSE file included with this module.
103              
104             =cut
105              
106             1;