File Coverage

blib/lib/YAML/PP/Schema/Tie/IxHash.pm
Criterion Covered Total %
statement 12 22 54.5
branch 0 2 0.0
condition n/a
subroutine 4 6 66.6
pod 1 1 100.0
total 17 31 54.8


line stmt bran cond sub pod time code
1 1     1   146431 use strict;
  1         2  
  1         32  
2 1     1   4 use warnings;
  1         1  
  1         96  
3             package YAML::PP::Schema::Tie::IxHash;
4              
5             our $VERSION = 'v0.39.0'; # VERSION
6              
7 1     1   8 use base 'YAML::PP::Schema';
  1         1  
  1         491  
8              
9 1     1   6 use Scalar::Util qw/ blessed reftype /;
  1         1  
  1         140  
10             my $ixhash = eval { require Tie::IxHash };
11              
12             sub register {
13 0     0 1   my ($self, %args) = @_;
14 0           my $schema = $args{schema};
15 0 0         unless ($ixhash) {
16 0           die "You need to install Tie::IxHash in order to use this module";
17             }
18              
19             $schema->add_representer(
20             tied_equals => 'Tie::IxHash',
21             code => sub {
22 0     0     my ($rep, $node) = @_;
23 0           $node->{items} = [ %{ $node->{data} } ];
  0            
24 0           return 1;
25             },
26 0           );
27 0           return;
28             }
29              
30             1;
31              
32             __END__
33              
34             =pod
35              
36             =encoding utf-8
37              
38             =head1 NAME
39              
40             YAML::PP::Schema::Tie::IxHash - (Deprecated) Schema for serializing ordered hashes
41              
42             =head1 SYNOPSIS
43              
44             use YAML::PP;
45             use Tie::IxHash;
46             my $yp = YAML::PP->new( schema => [qw/ + Tie::IxHash /] );
47              
48             tie(my %ordered, 'Tie::IxHash');
49             %ordered = (
50             U => 2,
51             B => 52,
52             );
53              
54             my $yaml = $yp->dump_string(\%ordered);
55              
56              
57             # Output:
58             ---
59             U: 2
60             B: 52
61              
62             =head1 DESCRIPTION
63              
64             This is deprecated. See the new option C<preserve> in L<YAML::PP>.
65              
66             This schema allows you to dump ordered hashes which are tied to
67             L<Tie::IxHash>.
68              
69             This code is pretty new and experimental.
70              
71             It is not yet implemented for loading yet, so for now you have to tie
72             the hashes yourself.
73              
74             Examples:
75              
76             =cut
77              
78             ### BEGIN EXAMPLE
79              
80             =pod
81              
82             =over 4
83              
84             =item order
85              
86             # Code
87             tie(my %order, 'Tie::IxHash');
88             %order = (
89             U => 2,
90             B => 52,
91             c => 64,
92             19 => 84,
93             Disco => 2000,
94             Year => 2525,
95             days_on_earth => 20_000,
96             );
97             \%order;
98              
99              
100             # YAML
101             ---
102             U: 2
103             B: 52
104             c: 64
105             19: 84
106             Disco: 2000
107             Year: 2525
108             days_on_earth: 20000
109              
110              
111             =item order_blessed
112              
113             # Code
114             tie(my %order, 'Tie::IxHash');
115             %order = (
116             U => 2,
117             B => 52,
118             c => 64,
119             19 => 84,
120             Disco => 2000,
121             Year => 2525,
122             days_on_earth => 20_000,
123             );
124             bless \%order, 'Order';
125              
126              
127             # YAML
128             --- !perl/hash:Order
129             U: 2
130             B: 52
131             c: 64
132             19: 84
133             Disco: 2000
134             Year: 2525
135             days_on_earth: 20000
136              
137              
138              
139              
140             =back
141              
142             =cut
143              
144             ### END EXAMPLE
145              
146             =head1 METHODS
147              
148             =over
149              
150             =item register
151              
152             Called by YAML::PP::Schema
153              
154             =back
155              
156             =cut