| 1 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 2 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | package YAML::PP::Schema::Binary; | ||||||||||
| 4 | |||||||||||
| 5 | 2 | our $VERSION = 'v0.41.1'; # TRIAL VERSION | |||||||||
| 6 | |||||||||||
| 7 | 2 | ▶ | use MIME::Base64 qw/ decode_base64 encode_base64 /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 2 | ▶ | use YAML::PP::Common qw/ YAML_ANY_SCALAR_STYLE /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 1 | ▶ | sub register { | ||||||||
|
Subroutine
register: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 11 | 1 | my ($self, %args) = @_; | |||||||||
| 12 | 1 | my $schema = $args{schema}; | |||||||||
| 13 | |||||||||||
| 14 | $schema->add_resolver( | ||||||||||
| 15 | tag => 'tag:yaml.org,2002:binary', | ||||||||||
| 16 | 15 | ▶ | match => [ all => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 15 | my ($constructor, $event) = @_; | |||||||||
| 18 | 15 | my $base64 = $event->{value}; | |||||||||
| 19 | 15 | my $binary = decode_base64($base64); | |||||||||
| 20 | 15 | return $binary; | |||||||||
| 21 | 1 | }], | |||||||||
| 22 | implicit => 0, | ||||||||||
| 23 | ); | ||||||||||
| 24 | |||||||||||
| 25 | $schema->add_representer( | ||||||||||
| 26 | regex => qr{.*}, | ||||||||||
| 27 | 20 | ▶ | code => sub { | ||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||
| 28 | 20 | my ($rep, $node) = @_; | |||||||||
| 29 | 20 | my $binary = $node->{value}; | |||||||||
| 30 | 20 | ▶ | unless ($binary =~ m/[\x{7F}-\x{10FFFF}]/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 31 | # ASCII | ||||||||||
| 32 | 2 | return; | |||||||||
| 33 | } | ||||||||||
| 34 | 18 | ▶ | if (utf8::is_utf8($binary)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | # utf8 | ||||||||||
| 36 | 4 | return; | |||||||||
| 37 | } | ||||||||||
| 38 | # everything else must be base64 encoded | ||||||||||
| 39 | 14 | my $base64 = encode_base64($binary); | |||||||||
| 40 | 14 | $node->{style} = YAML_ANY_SCALAR_STYLE; | |||||||||
| 41 | 14 | $node->{data} = $base64; | |||||||||
| 42 | 14 | $node->{tag} = "tag:yaml.org,2002:binary"; | |||||||||
| 43 | 14 | return 1; | |||||||||
| 44 | }, | ||||||||||
| 45 | 1 | ); | |||||||||
| 46 | } | ||||||||||
| 47 | |||||||||||
| 48 | 2 | 1; | |||||||||
| 49 | |||||||||||
| 50 | __END__ | ||||||||||