File Coverage

blib/lib/Unicode/Peek.pm
Criterion Covered Total %
statement 42 42 100.0
branch 4 6 66.6
condition n/a
subroutine 15 15 100.0
pod 0 4 0.0
total 61 67 91.0


line stmt bran cond sub pod time code
1             package Unicode::Peek;
2              
3             ## Validate the version of Perl
4              
5 14 50   14   888084 BEGIN { die 'Perl version 5.13.2 or greater is required' if ($] < 5.013002); }
6              
7 14     14   262 use strict;
  14         32  
  14         641  
8 14     14   88 use warnings;
  14         28  
  14         585  
9              
10             require Exporter;
11 14     14   82 use vars qw($VERSION @ISA @EXPORT_OK);
  14         28  
  14         1816  
12              
13             our @ISA = qw(Exporter);
14              
15             # Items to export into callers namespace by default. Note: do not export
16             # names by default without a very good reason. Use EXPORT_OK instead.
17             # Do not simply export all your public functions/methods/constants.
18              
19             # This allows declaration use Unicode::Peek ':all';
20             # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
21             # will save memory.
22             our %EXPORT_TAGS = ( 'all' => \@EXPORT_OK );
23              
24             our @EXPORT_OK = qw (
25             ascii2hexEncode
26             hex2ascciiDecode
27             hexDumperOutput
28             hexDumperInput
29             );
30              
31             ## Version of Unicode::Peek module
32              
33             our $VERSION = '0.08';
34             $VERSION = eval $VERSION;
35              
36             ## Load necessary modules
37 14     14   93 use utf8;
  14         31  
  14         100  
38 14     14   383 use Carp;
  14         22  
  14         882  
39 14     14   83 use feature 'say';
  14         27  
  14         1477  
40 14     14   5242 use Encode qw(decode encode);
  14         128416  
  14         8448  
41              
42             binmode( STDOUT, ':utf8' ); # debuggin purposes
43              
44             my @unicodes = ( 'UCS-2',
45             'UCS-2BE',
46             'UCS-2LE',
47             'UCS-4',
48             'UTF-7',
49             'utf8',
50             'utf-8-strict',
51             'UTF-8',
52             'UTF-16',
53             'UTF-16BE',
54             'UTF-16LE',
55             'UTF-32',
56             'UTF-32BE',
57             'UTF-32LE' );
58              
59             sub _checkSubroutineParameters {
60 140 50   140   389 croak "Please pass only two parameters '@_'"
61             if scalar @_ != 2;
62              
63             croak "Unknown encoding format '$_[0]'"
64 140 100       281 unless (grep { /$_[0]/ } @unicodes);
  1960         12084  
65              
66 84         225 return $_[0], $_[1];
67             }
68              
69             sub _ascii2hex {
70 28     28   209 return unpack("H*", $_[0]);
71             }
72              
73             sub _hex2ascii {
74 28     28   175 return pack("H*", $_[0]);
75             }
76              
77             sub hexDumperOutput {
78 28     28 0 6942 my ( $unicodeFormat , $data ) = _checkSubroutineParameters(@_);
79 14         62 my $hexString = ascii2hexEncode( $unicodeFormat , $data );
80             # trim leading and trailing white space
81             # split string every two characters
82             # join the splitted characters with white space
83 14         299 $hexString = join(' ', split(/(..)/, $hexString))
84             =~ s/^\s+|\s+$//r =~ y/ / /rs;
85             # insert new line character every 30 characters
86             # return join("\n", unpack('(A30)*', $hexString));
87 14         113 push my @aref, unpack('(A30)*', $hexString);
88 14         93 return \@aref;
89             }
90              
91             sub hexDumperInput {
92 28     28 0 11733 my ( $unicodeFormat , $arrayRef ) = _checkSubroutineParameters(@_);
93 14         133 my $hexString = join('', split(/ /, join('', @$arrayRef)));
94 14         62 return hex2ascciiDecode($unicodeFormat, $hexString);
95             }
96              
97             sub ascii2hexEncode {
98 42     42 0 15856 my ( $unicodeFormat , $data ) = _checkSubroutineParameters(@_);
99 28         127 my $octets = encode( $unicodeFormat , $data );
100 28         37326 return _ascii2hex( $octets );
101             }
102              
103             sub hex2ascciiDecode {
104 42     42 0 8933 my ( $unicodeFormat , $data ) = _checkSubroutineParameters(@_);
105 28         102 my $hex2ascciiString = _hex2ascii( $data );
106 28         126 return decode( $unicodeFormat , $hex2ascciiString );
107             }
108              
109             1;
110              
111             __END__