| 1 | package YAML::SyckWrapper; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | =encoding utf8 | ||||||||||||||
| 4 | |||||||||||||||
| 5 | =cut | ||||||||||||||
| 6 | |||||||||||||||
| 7 | |||||||||||||||
| 8 | # ABSTRACT: Loads YAML files in old and new fashion encoding ways. | ||||||||||||||
| 9 | |||||||||||||||
| 10 | 1 | our $VERSION = '0.001'; # VERSION | |||||||||||||
| 11 | |||||||||||||||
| 12 | 1 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | 1 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | |||||||||||||||
| 15 | 1 | ▶ | use Carp qw( croak ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 16 | 1 | ▶ | use YAML::Syck qw( Load LoadFile Dump ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 17 | 1 | ▶ | use Exporter qw( import ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 18 | 1 | ▶ | use File::Slurp qw( read_file ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 19 | |||||||||||||||
| 20 | |||||||||||||||
| 21 | 1 | our @EXPORT = (); | |||||||||||||
| 22 | 1 | our @EXPORT_OK = qw( | |||||||||||||
| 23 | &load_yaml_utf8 | ||||||||||||||
| 24 | &load_yaml_bytes | ||||||||||||||
| 25 | &load_yaml | ||||||||||||||
| 26 | &load_yaml_objects | ||||||||||||||
| 27 | &dump_yaml | ||||||||||||||
| 28 | &parse_yaml | ||||||||||||||
| 29 | &yaml_merge_hash_fix | ||||||||||||||
| 30 | ); | ||||||||||||||
| 31 | |||||||||||||||
| 32 | 1 | our $allow_blessed = 0; | |||||||||||||
| 33 | |||||||||||||||
| 34 | =head1 FUNCTIONS | ||||||||||||||
| 35 | |||||||||||||||
| 36 | None exported by default. | ||||||||||||||
| 37 | |||||||||||||||
| 38 | =over | ||||||||||||||
| 39 | |||||||||||||||
| 40 | =item B<load_yaml_utf8> | ||||||||||||||
| 41 | |||||||||||||||
| 42 | load_yaml_utf8( $file_name ); | ||||||||||||||
| 43 | |||||||||||||||
| 44 | Loads specified YAML file. | ||||||||||||||
| 45 | Source file should be in UTF-8. | ||||||||||||||
| 46 | Output is UTF-8 binary string. UTF-8 validation performed. | ||||||||||||||
| 47 | |||||||||||||||
| 48 | =cut | ||||||||||||||
| 49 | |||||||||||||||
| 50 | 5 | ▶ | sub _load_yaml_and_close { | ||||||||||||
|
Subroutine
_load_yaml_and_close: called CC=3 SCAR=11.5
| |||||||||||||||
| 51 | 5 | my ( $file_name, $fh ) = @_; | |||||||||||||
| 52 | |||||||||||||||
| 53 | 5 | local $YAML::Syck::LoadBlessed = $allow_blessed; # никаких bless по умолчанию | |||||||||||||
| 54 | 5 | my ( $res, $err ) = ( undef, undef ); | |||||||||||||
| 55 | eval { | ||||||||||||||
| 56 | 5 | $res = LoadFile( $fh ); | |||||||||||||
| 57 | 5 | 1; | |||||||||||||
| 58 | 5 | ▶ | } or do { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 59 | 0 | $err = $@; | |||||||||||||
| 60 | }; | ||||||||||||||
| 61 | 5 | close $fh; | |||||||||||||
| 62 | 5 | ▶ | if ( $err ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 63 | 0 | croak "Cannot load $file_name: $err"; | |||||||||||||
| 64 | } | ||||||||||||||
| 65 | 5 | return $res; | |||||||||||||
| 66 | } | ||||||||||||||
| 67 | |||||||||||||||
| 68 | 5 | ▶ | sub load_yaml_utf8 { | ||||||||||||
|
Subroutine
load_yaml_utf8: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 69 | 5 | my ( $file_name ) = @_; | |||||||||||||
| 70 | |||||||||||||||
| 71 | # This misleading hack taken from YAML::Syck documentation. We use encoding(UTF-8) layer here - obviously | ||||||||||||||
| 72 | # output should be Perl character string, but YAML::Syck::LoadFile will output binary UTF-8 (with | ||||||||||||||
| 73 | # YAML::Syck::ImplicitUnicode == false, which is default) | ||||||||||||||
| 74 | # Also YAML format defined as bytes (UTF-8 , UTF-16), so passing character filehandle to YAML::Syck::LoadFile is | ||||||||||||||
| 75 | # a hack too. | ||||||||||||||
| 76 | # encoding(UTF-8) does nothing here, except forcing perl to validate UTF-8 | ||||||||||||||
| 77 | 5 | ▶ | open ( my $fh, '<:encoding(UTF-8)', $file_name ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 78 | or die "Cannot open $file_name: $!"; | ||||||||||||||
| 79 | |||||||||||||||
| 80 | 3 | return _load_yaml_and_close( $file_name, $fh ); | |||||||||||||
| 81 | } | ||||||||||||||
| 82 | |||||||||||||||
| 83 | =item B<load_yaml> | ||||||||||||||
| 84 | |||||||||||||||
| 85 | load_yaml( $file_name ); | ||||||||||||||
| 86 | |||||||||||||||
| 87 | Loads specified YAML file. | ||||||||||||||
| 88 | Source file should be in UTF-8. | ||||||||||||||
| 89 | Output is UTF-8 character string. UTF-8 validation performed. | ||||||||||||||
| 90 | |||||||||||||||
| 91 | =cut | ||||||||||||||
| 92 | |||||||||||||||
| 93 | 3 | ▶ | sub load_yaml { | ||||||||||||
|
Subroutine
load_yaml: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 94 | 3 | my ( $file_name ) = @_; | |||||||||||||
| 95 | |||||||||||||||
| 96 | 3 | local $YAML::Syck::ImplicitUnicode = 1; | |||||||||||||
| 97 | 3 | return load_yaml_utf8( $file_name ); | |||||||||||||
| 98 | } | ||||||||||||||
| 99 | |||||||||||||||
| 100 | =item B<load_yaml_bytes> | ||||||||||||||
| 101 | |||||||||||||||
| 102 | load_yaml_bytes( $file_name ); | ||||||||||||||
| 103 | |||||||||||||||
| 104 | Loads specified as-is (no charset processing is involved). For old cp1251 yamls only. | ||||||||||||||
| 105 | Output is binary string, same as input data. | ||||||||||||||
| 106 | |||||||||||||||
| 107 | =cut | ||||||||||||||
| 108 | |||||||||||||||
| 109 | 4 | ▶ | sub load_yaml_bytes { | ||||||||||||
|
Subroutine
load_yaml_bytes: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 110 | 4 | my ( $file_name ) = @_; | |||||||||||||
| 111 | 4 | ▶ | open ( my $fh, '<:bytes', $file_name ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 112 | or die "Cannot open $file_name: $!"; | ||||||||||||||
| 113 | 2 | return _load_yaml_and_close( $file_name, $fh ); | |||||||||||||
| 114 | } | ||||||||||||||
| 115 | |||||||||||||||
| 116 | |||||||||||||||
| 117 | =item B<load_yaml_objects> | ||||||||||||||
| 118 | |||||||||||||||
| 119 | load_yaml_objects( $file_name ) | ||||||||||||||
| 120 | |||||||||||||||
| 121 | |||||||||||||||
| 122 | Loads specified file and outputs data in configured encoding. | ||||||||||||||
| 123 | Source file always in UTF-8. | ||||||||||||||
| 124 | Output format is text (unicode) | ||||||||||||||
| 125 | |||||||||||||||
| 126 | Any !perl tag will be blessed. | ||||||||||||||
| 127 | |||||||||||||||
| 128 | Syntax: | ||||||||||||||
| 129 | |||||||||||||||
| 130 | myobj: !!perl/Some::Class | ||||||||||||||
| 131 | prop: value | ||||||||||||||
| 132 | |||||||||||||||
| 133 | Don't use on insecure data! | ||||||||||||||
| 134 | |||||||||||||||
| 135 | =cut | ||||||||||||||
| 136 | |||||||||||||||
| 137 | 1 | ▶ | sub load_yaml_objects { | ||||||||||||
|
Subroutine
load_yaml_objects: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||
| 138 | 1 | my ( $file_name ) = @_; | |||||||||||||
| 139 | 1 | local $allow_blessed = 1; | |||||||||||||
| 140 | 1 | ▶ | local $SIG{__WARN__} = sub {}; | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.0
| |||||||||||||||
| 141 | 1 | my $data = load_yaml( $file_name ); | |||||||||||||
| 142 | 1 | ▶ | return unless defined $data; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 143 | 1 | return $data; | |||||||||||||
| 144 | } | ||||||||||||||
| 145 | |||||||||||||||
| 146 | =item B<dump_yaml> | ||||||||||||||
| 147 | |||||||||||||||
| 148 | dump_yaml( $data ); | ||||||||||||||
| 149 | |||||||||||||||
| 150 | Dumps data in YAML in Unicode. | ||||||||||||||
| 151 | Возвращает уникод. | ||||||||||||||
| 152 | |||||||||||||||
| 153 | =cut | ||||||||||||||
| 154 | |||||||||||||||
| 155 | 1 | ▶ | sub dump_yaml { | ||||||||||||
|
Subroutine
dump_yaml: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 156 | 1 | my ( $data ) = @_; | |||||||||||||
| 157 | 1 | local $YAML::Syck::ImplicitUnicode = 1; | |||||||||||||
| 158 | 1 | local $YAML::Syck::SortKeys = 1; | |||||||||||||
| 159 | 1 | return Dump( $data ); | |||||||||||||
| 160 | } | ||||||||||||||
| 161 | |||||||||||||||
| 162 | =item B<parse_yaml> | ||||||||||||||
| 163 | |||||||||||||||
| 164 | parse_yaml( $yaml_text ); | ||||||||||||||
| 165 | |||||||||||||||
| 166 | Parses YAML text into Unicode structure. | ||||||||||||||
| 167 | |||||||||||||||
| 168 | Принимает unicode или UTF-8. | ||||||||||||||
| 169 | |||||||||||||||
| 170 | =cut | ||||||||||||||
| 171 | |||||||||||||||
| 172 | 2 | ▶ | sub parse_yaml { | ||||||||||||
|
Subroutine
parse_yaml: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 173 | 2 | my ( $yaml_text ) = @_; | |||||||||||||
| 174 | 2 | local $YAML::Syck::LoadBlessed = $allow_blessed; # никаких bless по умолчанию | |||||||||||||
| 175 | 2 | local $YAML::Syck::ImplicitUnicode = 1; | |||||||||||||
| 176 | 2 | return Load( $yaml_text ); | |||||||||||||
| 177 | } | ||||||||||||||
| 178 | |||||||||||||||
| 179 | |||||||||||||||
| 180 | =item B<yaml_merge_hash_fix> | ||||||||||||||
| 181 | |||||||||||||||
| 182 | yaml_merge_hash_fix( $ref ); | ||||||||||||||
| 183 | |||||||||||||||
| 184 | YAML hash merge bugfix | ||||||||||||||
| 185 | http://www.perlmonks.org/?node_id=813443. | ||||||||||||||
| 186 | |||||||||||||||
| 187 | =cut | ||||||||||||||
| 188 | |||||||||||||||
| 189 | 8 | ▶ | sub yaml_merge_hash_fix { | ||||||||||||
|
Subroutine
yaml_merge_hash_fix: called CC=9 SCAR=24.6
Pod
documented
| |||||||||||||||
| 190 | 8 | my ($ref) = @_; | |||||||||||||
| 191 | |||||||||||||||
| 192 | 8 | my $type = ref $ref; | |||||||||||||
| 193 | 8 | ▶ | if ( $type eq 'HASH' ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 194 | 3 | ▶ | while ( exists $ref->{'<<'} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 195 | 1 | my $tmphref = $ref->{'<<'}; | |||||||||||||
| 196 | 1 | ▶ | if ($tmphref) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 197 | 1 | ▶ | if (ref $tmphref eq 'HASH') { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 198 | 1 | my %tmphash = %$tmphref; | |||||||||||||
| 199 | 1 | delete $ref->{'<<'}; | |||||||||||||
| 200 | 1 | %$ref = (%tmphash, %$ref); | |||||||||||||
| 201 | } | ||||||||||||||
| 202 | elsif (ref $tmphref eq 'ARRAY') { | ||||||||||||||
| 203 | 0 | delete $ref->{'<<'}; | |||||||||||||
| 204 | 0 | %$ref = ( (map %$_, reverse @$tmphref ), %$ref); | |||||||||||||
| 205 | } | ||||||||||||||
| 206 | else { | ||||||||||||||
| 207 | 0 | die "Merge key only support merging hashes or arrays"; | |||||||||||||
| 208 | } | ||||||||||||||
| 209 | } | ||||||||||||||
| 210 | } | ||||||||||||||
| 211 | 3 | yaml_merge_hash_fix($_) for ( values %$ref ); | |||||||||||||
| 212 | } | ||||||||||||||
| 213 | elsif ( $type eq 'ARRAY' ) { | ||||||||||||||
| 214 | 0 | yaml_merge_hash_fix($_) for (@$ref); | |||||||||||||
| 215 | } | ||||||||||||||
| 216 | 8 | return $ref; | |||||||||||||
| 217 | } | ||||||||||||||
| 218 | |||||||||||||||
| 219 | =back | ||||||||||||||
| 220 | |||||||||||||||
| 221 | =cut | ||||||||||||||
| 222 | |||||||||||||||
| 223 | 1 | 1; | |||||||||||||