| 1 | package App::PerlShell::Plugin::Macros; | ||||||||||
| 2 | |||||||||||
| 3 | ######################################################## | ||||||||||
| 4 | # AUTHOR = Michael Vincent | ||||||||||
| 5 | # www.VinsWorld.com | ||||||||||
| 6 | ######################################################## | ||||||||||
| 7 | |||||||||||
| 8 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 1 | ▶ | use Pod::Usage; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 1 | ▶ | use Pod::Find qw( pod_where ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | 1 | ▶ | use Exporter; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | 1 | our @EXPORT = qw( | |||||||||
| 16 | Macros | ||||||||||
| 17 | D2B | ||||||||||
| 18 | D2H | ||||||||||
| 19 | H2B | ||||||||||
| 20 | H2D | ||||||||||
| 21 | H2S | ||||||||||
| 22 | S2H | ||||||||||
| 23 | ); | ||||||||||
| 24 | |||||||||||
| 25 | 1 | our @ISA = qw ( Exporter ); | |||||||||
| 26 | |||||||||||
| 27 | 0 | ▶ | sub Macros { | ||||||||
|
Subroutine
Macros: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 28 | 0 | pod2usage( | |||||||||
| 29 | -verbose => 2, | ||||||||||
| 30 | -exitval => "NOEXIT", | ||||||||||
| 31 | -input => pod_where( {-inc => 1}, __PACKAGE__ ) | ||||||||||
| 32 | ); | ||||||||||
| 33 | } | ||||||||||
| 34 | |||||||||||
| 35 | ######################################################## | ||||||||||
| 36 | |||||||||||
| 37 | 0 | ▶ | sub D2B { | ||||||||
|
Subroutine
D2B: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||
| 38 | 0 | my ( $dec, $pad ) = @_; | |||||||||
| 39 | |||||||||||
| 40 | 0 | ▶ | if ( not defined $dec ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | 0 | _help( "MACROS/D2B - convert decimal number to binary" ); | |||||||||
| 42 | 0 | return; | |||||||||
| 43 | } | ||||||||||
| 44 | |||||||||||
| 45 | 0 | my $ret; | |||||||||
| 46 | 0 | ▶ | if ( $dec =~ /^\d+$/ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | 0 | $ret = sprintf "%b", $dec; | |||||||||
| 48 | 0 | ▶ | if ( defined $pad ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 49 | 0 | ▶ | if ( $pad =~ /^\d+$/ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 50 | 0 | $pad = "0" x ( $pad - length($ret) ); | |||||||||
| 51 | 0 | $ret = $pad . $ret; | |||||||||
| 52 | } else { | ||||||||||
| 53 | 0 | warn "Ignoring not a number pad `$pad'\n"; | |||||||||
| 54 | } | ||||||||||
| 55 | } | ||||||||||
| 56 | 0 | ▶ | if ( !defined wantarray ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 57 | 0 | print "$ret\n"; | |||||||||
| 58 | } | ||||||||||
| 59 | 0 | return $ret; | |||||||||
| 60 | } else { | ||||||||||
| 61 | 0 | warn "Not a decimal number `$dec'\n"; | |||||||||
| 62 | } | ||||||||||
| 63 | } | ||||||||||
| 64 | |||||||||||
| 65 | 0 | ▶ | sub D2H { | ||||||||
|
Subroutine
D2H: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||
| 66 | 0 | my ($dec) = @_; | |||||||||
| 67 | |||||||||||
| 68 | 0 | ▶ | if ( not defined $dec ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | 0 | _help( "MACROS/D2H - convert decimal number to hex" ); | |||||||||
| 70 | 0 | return; | |||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | 0 | my $ret; | |||||||||
| 74 | 0 | ▶ | if ( $dec =~ /^\d+$/ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 75 | 0 | $ret = sprintf "%x", $dec; | |||||||||
| 76 | 0 | ▶ | if ( !defined wantarray ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | 0 | print "$ret\n"; | |||||||||
| 78 | } | ||||||||||
| 79 | 0 | return $ret; | |||||||||
| 80 | } else { | ||||||||||
| 81 | 0 | warn "Not a decimal number `$dec'\n"; | |||||||||
| 82 | } | ||||||||||
| 83 | } | ||||||||||
| 84 | |||||||||||
| 85 | 0 | ▶ | sub H2B { | ||||||||
|
Subroutine
H2B: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||
| 86 | 0 | my ( $hex, $pad ) = @_; | |||||||||
| 87 | |||||||||||
| 88 | 0 | ▶ | if ( not defined $hex ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | 0 | _help( "MACROS/H2B - convert hex number to binary" ); | |||||||||
| 90 | 0 | return; | |||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | 0 | my $ret; | |||||||||
| 94 | 0 | ▶ | if ( $hex =~ /^(?:0x)?[0-9a-fA-F]+$/ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 95 | 0 | $ret = sprintf "%b", $hex; | |||||||||
| 96 | 0 | ▶ | if ( defined $pad ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 97 | 0 | ▶ | if ( $pad =~ /^\d+$/ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 98 | 0 | $pad = "0" x ( $pad - length($ret) ); | |||||||||
| 99 | 0 | $ret = $pad . $ret; | |||||||||
| 100 | } else { | ||||||||||
| 101 | 0 | warn "Ignoring not a number pad `$pad'\n"; | |||||||||
| 102 | } | ||||||||||
| 103 | } | ||||||||||
| 104 | 0 | ▶ | if ( !defined wantarray ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 105 | 0 | print "$ret\n"; | |||||||||
| 106 | } | ||||||||||
| 107 | 0 | return $ret; | |||||||||
| 108 | } else { | ||||||||||
| 109 | 0 | warn "Not a hex number `$hex'\n"; | |||||||||
| 110 | } | ||||||||||
| 111 | } | ||||||||||
| 112 | |||||||||||
| 113 | 0 | ▶ | sub H2D { | ||||||||
|
Subroutine
H2D: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||
| 114 | 0 | my ($hex) = @_; | |||||||||
| 115 | |||||||||||
| 116 | 0 | ▶ | if ( not defined $hex ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 117 | 0 | _help( "MACROS/H2D - convert hex number to decimal" ); | |||||||||
| 118 | 0 | return; | |||||||||
| 119 | } | ||||||||||
| 120 | |||||||||||
| 121 | 0 | my $ret; | |||||||||
| 122 | |||||||||||
| 123 | # passed as number | ||||||||||
| 124 | 0 | ▶ | if ( $hex =~ /^\d+$/ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 125 | 0 | ▶ | if ( !defined wantarray ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 126 | 0 | print "$hex\n"; | |||||||||
| 127 | } | ||||||||||
| 128 | 0 | return $hex; | |||||||||
| 129 | } | ||||||||||
| 130 | |||||||||||
| 131 | # passed as string | ||||||||||
| 132 | 0 | ▶ | if ( $hex =~ /^(?:0x)?[0-9a-fA-F]+$/ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 133 | 0 | $ret = hex($hex); | |||||||||
| 134 | 0 | ▶ | if ( !defined wantarray ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 135 | 0 | print "$ret\n"; | |||||||||
| 136 | } | ||||||||||
| 137 | 0 | return $ret; | |||||||||
| 138 | } else { | ||||||||||
| 139 | 0 | warn "Not a hex number `$hex'\n"; | |||||||||
| 140 | } | ||||||||||
| 141 | } | ||||||||||
| 142 | |||||||||||
| 143 | 0 | ▶ | sub H2S { | ||||||||
|
Subroutine
H2S: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 144 | 0 | my ($pack) = @_; | |||||||||
| 145 | |||||||||||
| 146 | 0 | ▶ | if ( not defined $pack ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 147 | 0 | _help( "MACROS/H2S - convert hex to string" ); | |||||||||
| 148 | 0 | return; | |||||||||
| 149 | } | ||||||||||
| 150 | |||||||||||
| 151 | 0 | my $ret; | |||||||||
| 152 | 0 | $ret = pack "H*", $pack; | |||||||||
| 153 | 0 | ▶ | if ( !defined wantarray ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 154 | 0 | print "$ret\n"; | |||||||||
| 155 | } | ||||||||||
| 156 | 0 | return $ret; | |||||||||
| 157 | } | ||||||||||
| 158 | |||||||||||
| 159 | 0 | ▶ | sub S2H { | ||||||||
|
Subroutine
S2H: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||
| 160 | 0 | my ($str) = @_; | |||||||||
| 161 | |||||||||||
| 162 | 0 | ▶ | if ( not defined $str ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 163 | 0 | _help( "MACROS/S2H - convert string to hex" ); | |||||||||
| 164 | 0 | return; | |||||||||
| 165 | } | ||||||||||
| 166 | |||||||||||
| 167 | 0 | my $ret; | |||||||||
| 168 | 0 | for ( split //, $str ) { | |||||||||
| 169 | 0 | $ret .= sprintf "%0.2x", ord $_; | |||||||||
| 170 | } | ||||||||||
| 171 | 0 | ▶ | if ( !defined wantarray ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 172 | 0 | print "$ret\n"; | |||||||||
| 173 | } | ||||||||||
| 174 | 0 | return $ret; | |||||||||
| 175 | } | ||||||||||
| 176 | |||||||||||
| 177 | 0 | ▶ | sub _help { | ||||||||
|
Subroutine
_help: not called CC=1 SCAR=6.9
| |||||||||||
| 178 | 0 | my ($section) = @_; | |||||||||
| 179 | |||||||||||
| 180 | 0 | pod2usage( | |||||||||
| 181 | -verbose => 99, | ||||||||||
| 182 | -exitval => "NOEXIT", | ||||||||||
| 183 | -sections => $section, | ||||||||||
| 184 | -input => pod_where( {-inc => 1}, __PACKAGE__ ) | ||||||||||
| 185 | ); | ||||||||||
| 186 | } | ||||||||||
| 187 | |||||||||||
| 188 | 1 | 1; | |||||||||
| 189 | |||||||||||
| 190 | __END__ | ||||||||||