| 1 | package Unicode::Peek; | ||||||||||
| 2 | |||||||||||
| 3 | ## Validate the version of Perl | ||||||||||
| 4 | |||||||||||
| 5 | 14 | ▶ | BEGIN { die 'Perl version 5.13.2 or greater is required' if ($] < 5.013002); } | ||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||
| 6 | |||||||||||
| 7 | 14 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 14 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 14 | require Exporter; | |||||||||
| 11 | 14 | ▶ | use vars qw($VERSION @ISA @EXPORT_OK); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | 14 | 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 | 14 | our %EXPORT_TAGS = ( 'all' => \@EXPORT_OK ); | |||||||||
| 23 | |||||||||||
| 24 | 14 | our @EXPORT_OK = qw ( | |||||||||
| 25 | ascii2hexEncode | ||||||||||
| 26 | hex2ascciiDecode | ||||||||||
| 27 | hexDumperOutput | ||||||||||
| 28 | hexDumperInput | ||||||||||
| 29 | ); | ||||||||||
| 30 | |||||||||||
| 31 | ## Version of Unicode::Peek module | ||||||||||
| 32 | |||||||||||
| 33 | 14 | our $VERSION = '0.08'; | |||||||||
| 34 | 14 | $VERSION = eval $VERSION; | |||||||||
| 35 | |||||||||||
| 36 | ## Load necessary modules | ||||||||||
| 37 | 14 | ▶ | use utf8; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 38 | 14 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 39 | 14 | ▶ | use feature 'say'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 40 | 14 | ▶ | use Encode qw(decode encode); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 41 | |||||||||||
| 42 | 14 | binmode( STDOUT, ':utf8' ); # debuggin purposes | |||||||||
| 43 | |||||||||||
| 44 | 14 | 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 | 140 | ▶ | sub _checkSubroutineParameters { | ||||||||
|
Subroutine
_checkSubroutineParameters: called CC=3 SCAR=11.0
| |||||||||||
| 60 | 140 | ▶ | croak "Please pass only two parameters '@_'" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | if scalar @_ != 2; | ||||||||||
| 62 | |||||||||||
| 63 | croak "Unknown encoding format '$_[0]'" | ||||||||||
| 64 | 140 | ▶ | unless (grep { /$_[0]/ } @unicodes); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | |||||||||||
| 66 | 84 | return $_[0], $_[1]; | |||||||||
| 67 | } | ||||||||||
| 68 | |||||||||||
| 69 | 28 | ▶ | sub _ascii2hex { | ||||||||
|
Subroutine
_ascii2hex: called CC=1 SCAR=0.0
| |||||||||||
| 70 | 28 | return unpack("H*", $_[0]); | |||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | 28 | ▶ | sub _hex2ascii { | ||||||||
|
Subroutine
_hex2ascii: called CC=1 SCAR=0.0
| |||||||||||
| 74 | 28 | return pack("H*", $_[0]); | |||||||||
| 75 | } | ||||||||||
| 76 | |||||||||||
| 77 | 28 | ▶ | sub hexDumperOutput { | ||||||||
|
Subroutine
hexDumperOutput: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 78 | 28 | my ( $unicodeFormat , $data ) = _checkSubroutineParameters(@_); | |||||||||
| 79 | 14 | 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 | $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 | push my @aref, unpack('(A30)*', $hexString); | |||||||||
| 88 | 14 | return \@aref; | |||||||||
| 89 | } | ||||||||||
| 90 | |||||||||||
| 91 | 28 | ▶ | sub hexDumperInput { | ||||||||
|
Subroutine
hexDumperInput: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 92 | 28 | my ( $unicodeFormat , $arrayRef ) = _checkSubroutineParameters(@_); | |||||||||
| 93 | 14 | my $hexString = join('', split(/ /, join('', @$arrayRef))); | |||||||||
| 94 | 14 | return hex2ascciiDecode($unicodeFormat, $hexString); | |||||||||
| 95 | } | ||||||||||
| 96 | |||||||||||
| 97 | 42 | ▶ | sub ascii2hexEncode { | ||||||||
|
Subroutine
ascii2hexEncode: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 98 | 42 | my ( $unicodeFormat , $data ) = _checkSubroutineParameters(@_); | |||||||||
| 99 | 28 | my $octets = encode( $unicodeFormat , $data ); | |||||||||
| 100 | 28 | return _ascii2hex( $octets ); | |||||||||
| 101 | } | ||||||||||
| 102 | |||||||||||
| 103 | 42 | ▶ | sub hex2ascciiDecode { | ||||||||
|
Subroutine
hex2ascciiDecode: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 104 | 42 | my ( $unicodeFormat , $data ) = _checkSubroutineParameters(@_); | |||||||||
| 105 | 28 | my $hex2ascciiString = _hex2ascii( $data ); | |||||||||
| 106 | 28 | return decode( $unicodeFormat , $hex2ascciiString ); | |||||||||
| 107 | } | ||||||||||
| 108 | |||||||||||
| 109 | 14 | 1; | |||||||||
| 110 | |||||||||||
| 111 | __END__ | ||||||||||