| 1 | # | ||
| 2 | # (c) Jan Gehring <jan.gehring@gmail.com> | ||
| 3 | # | ||
| 4 | |||
| 5 | package Rex::Helper::Encode; | ||
| 6 | |||
| 7 | 34 | ▶ | use v5.14.4; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 34 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 34 | our $VERSION = '1.16.1'; # VERSION | |
| 11 | |||
| 12 | 34 | require Exporter; | |
| 13 | 34 | ▶ | use base qw(Exporter); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | 34 | ▶ | use vars qw(@EXPORT); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 15 | 34 | @EXPORT = qw(func_to_json); | |
| 16 | |||
| 17 | 34 | my %escapes; | |
| 18 | 34 | for ( 0 .. 255 ) { | |
| 19 | 8704 | $escapes{ chr($_) } = sprintf( "%%%02X", $_ ); | |
| 20 | } | ||
| 21 | |||
| 22 | 1 | ▶ | sub url_encode { |
|
Subroutine
url_encode: called CC=1 SCAR=0.0
Pod
undocumented
| |||
| 23 | 1 | my ($txt) = @_; | |
| 24 | 1 | $txt =~ s/([^A-Za-z0-9_])/$escapes{$1}/g; | |
| 25 | 1 | return $txt; | |
| 26 | } | ||
| 27 | |||
| 28 | 0 | ▶ | sub url_decode { |
|
Subroutine
url_decode: not called CC=1 SCAR=6.9
Pod
undocumented
| |||
| 29 | 0 | my ($txt) = @_; | |
| 30 | 0 | $txt =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg; | |
| 31 | 0 | return $txt; | |
| 32 | } | ||
| 33 | |||
| 34 | 0 | ▶ | sub func_to_json { |
|
Subroutine
func_to_json: not called CC=1 SCAR=6.9
Pod
undocumented
| |||
| 35 | |||
| 36 | 0 | return q| | |
| 37 | sub to_json { | ||
| 38 | my ($ref) = @_; | ||
| 39 | |||
| 40 | my $s = ""; | ||
| 41 | |||
| 42 | if(ref $ref eq "ARRAY") { | ||
| 43 | $s .= "["; | ||
| 44 | for my $itm (@{ $ref }) { | ||
| 45 | if(substr($s, -1) ne "[") { | ||
| 46 | $s .= ","; | ||
| 47 | } | ||
| 48 | $s .= to_json($itm); | ||
| 49 | } | ||
| 50 | return $s . "]"; | ||
| 51 | } | ||
| 52 | elsif(ref $ref eq "HASH") { | ||
| 53 | $s .= "{"; | ||
| 54 | for my $key (keys %{ $ref }) { | ||
| 55 | if(substr($s, -1) ne "{") { | ||
| 56 | $s .= ","; | ||
| 57 | } | ||
| 58 | $s .= "\"$key\": " . to_json($ref->{$key}); | ||
| 59 | } | ||
| 60 | return $s . "}"; | ||
| 61 | } | ||
| 62 | else { | ||
| 63 | if($ref =~ /^0\d+/) { | ||
| 64 | return "\"$ref\""; | ||
| 65 | } | ||
| 66 | elsif($ref =~ /^\d+$/) { | ||
| 67 | return $ref; | ||
| 68 | } | ||
| 69 | else { | ||
| 70 | $ref =~ s/'/\\\'/g; | ||
| 71 | return "\"$ref\""; | ||
| 72 | } | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | |; | ||
| 77 | |||
| 78 | } | ||
| 79 | |||
| 80 | 34 | 1; | |