| 1 | package Data::ClearSilver::HDF; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use ClearSilver; | ||
| 7 | use Data::Structure::Util qw(unbless has_circular_ref circular_off); | ||
| 8 | use File::Slurp qw(slurp); | ||
| 9 | use File::Temp; | ||
| 10 | |||
| 11 | =head1 NAME | ||
| 12 | |||
| 13 | Data::ClearSilver::HDF - Convert from Perl Data Structure to ClearSilver HDF | ||
| 14 | |||
| 15 | =head1 VERSION | ||
| 16 | |||
| 17 | version 0.04 | ||
| 18 | |||
| 19 | =cut | ||
| 20 | |||
| 21 | our $VERSION = '0.04'; | ||
| 22 | |||
| 23 | =head1 SYNOPSIS | ||
| 24 | |||
| 25 | use Data::ClearSilver::HDF; | ||
| 26 | |||
| 27 | my $data = { | ||
| 28 | foo => { | ||
| 29 | bar => 1, | ||
| 30 | baz => [0 .. 5] | ||
| 31 | }, | ||
| 32 | obj => bless { foo => "xxx", bar => "yyy" } | ||
| 33 | }; | ||
| 34 | |||
| 35 | my $hdf = Data::ClearSilver::HDF->hdf($data); | ||
| 36 | |||
| 37 | print $hdf->getValue("obj.foo", undef); # xxx | ||
| 38 | |||
| 39 | =head1 PROPERTIES | ||
| 40 | |||
| 41 | =head2 $USE_SORT | ||
| 42 | |||
| 43 | Sorting each keys hieralcally. default false; | ||
| 44 | |||
| 45 | =cut | ||
| 46 | |||
| 47 | our $USE_SORT = 0; | ||
| 48 | |||
| 49 | =head1 METHODS | ||
| 50 | |||
| 51 | =head2 hdf($data) | ||
| 52 | |||
| 53 | The argument $data must be reference. | ||
| 54 | In the data, all of value excluded ARRAY, HASH, blessed reference will be ignored. | ||
| 55 | |||
| 56 | Blessed reference will be unblessed by L<Data::Structure::Util>'s unbless functon. | ||
| 57 | |||
| 58 | =cut | ||
| 59 | |||
| 60 | sub hdf { | ||
| 61 | my ( $class, $data ) = @_; | ||
| 62 | |||
| 63 | unbless($data); | ||
| 64 | circular_off($data) if ( has_circular_ref($data) ); | ||
| 65 | |||
| 66 | my $hdf = ClearSilver::HDF->new; | ||
| 67 | my $data_type = ref $data; | ||
| 68 | |||
| 69 | unless ( $data_type && ( $data_type eq "ARRAY" || $data_type eq "HASH" ) ) { | ||
| 70 | return $hdf; | ||
| 71 | } | ||
| 72 | else { | ||
| 73 | my $method = "hdf_" . lc($data_type); | ||
| 74 | $class->$method( $hdf, undef, $data ); | ||
| 75 | _hdf_walk($hdf) if ($USE_SORT); | ||
| 76 | return $hdf; | ||
| 77 | } | ||
| 78 | } | ||
| 79 | |||
| 80 | =head2 hdf_dump($hdf) | ||
| 81 | |||
| 82 | Dump as string from ClearSilver::HDF object. | ||
| 83 | This method will create temporary file. | ||
| 84 | |||
| 85 | =cut | ||
| 86 | |||
| 87 | sub hdf_dump { | ||
| 88 | my ( $class, $hdf ) = @_; | ||
| 89 | |||
| 90 | my $fh = File::Temp->new; | ||
| 91 | $hdf->writeFile( $fh->filename ); | ||
| 92 | |||
| 93 | return slurp( $fh->filename ); | ||
| 94 | } | ||
| 95 | |||
| 96 | =head2 hdf_scalar($hdf, $keys, $data) | ||
| 97 | |||
| 98 | Translate scalar data to hdf. | ||
| 99 | Please don't call directory. | ||
| 100 | |||
| 101 | =cut | ||
| 102 | |||
| 103 | sub hdf_scalar { | ||
| 104 | my ( $class, $hdf, $keys, $data ) = @_; | ||
| 105 | |||
| 106 | $hdf->setValue( join( ".", @$keys ), $data ); | ||
| 107 | } | ||
| 108 | |||
| 109 | =head2 hdf_array($hdf, $keys, $data) | ||
| 110 | |||
| 111 | Translate array reference data to hdf. | ||
| 112 | Please don't call directory. | ||
| 113 | |||
| 114 | =cut | ||
| 115 | |||
| 116 | sub hdf_array { | ||
| 117 | my ( $class, $hdf, $keys, $data ) = @_; | ||
| 118 | |||
| 119 | $keys ||= []; | ||
| 120 | my $idx = 0; | ||
| 121 | |||
| 122 | for my $value (@$data) { | ||
| 123 | my @keys = @$keys; | ||
| 124 | my $value_ref = ref $value; | ||
| 125 | |||
| 126 | push( @keys, $idx ); | ||
| 127 | |||
| 128 | unless ( defined $value && $value_ref ) { | ||
| 129 | $class->hdf_scalar( $hdf, \@keys, $value ); | ||
| 130 | } | ||
| 131 | elsif ( $value_ref eq "ARRAY" ) { | ||
| 132 | $class->hdf_array( $hdf, \@keys, $value ); | ||
| 133 | } | ||
| 134 | elsif ( $value_ref eq "HASH" ) { | ||
| 135 | $class->hdf_hash( $hdf, \@keys, $value ); | ||
| 136 | } | ||
| 137 | else { | ||
| 138 | next; | ||
| 139 | } | ||
| 140 | |||
| 141 | $idx++; | ||
| 142 | } | ||
| 143 | } | ||
| 144 | |||
| 145 | =head2 hdf_hash($hdf, $keys, $data) | ||
| 146 | |||
| 147 | Translate hash reference data to hdf. | ||
| 148 | Please don't call directory. | ||
| 149 | |||
| 150 | =cut | ||
| 151 | |||
| 152 | sub hdf_hash { | ||
| 153 | my ( $class, $hdf, $keys, $data ) = @_; | ||
| 154 | |||
| 155 | $keys ||= []; | ||
| 156 | |||
| 157 | while ( my ( $key, $value ) = each %$data ) { | ||
| 158 | my @keys = @$keys; | ||
| 159 | my $value_ref = ref $value; | ||
| 160 | |||
| 161 | push( @keys, $key ); | ||
| 162 | |||
| 163 | unless ( defined $value && $value_ref ) { | ||
| 164 | $class->hdf_scalar( $hdf, \@keys, $value ); | ||
| 165 | } | ||
| 166 | elsif ( $value_ref eq "ARRAY" ) { | ||
| 167 | $class->hdf_array( $hdf, \@keys, $value ); | ||
| 168 | } | ||
| 169 | elsif ( $value_ref eq "HASH" ) { | ||
| 170 | $class->hdf_hash( $hdf, \@keys, $value ); | ||
| 171 | } | ||
| 172 | else { | ||
| 173 | next; | ||
| 174 | } | ||
| 175 | } | ||
| 176 | } | ||
| 177 | |||
| 178 | ### private method | ||
| 179 | |||
| 180 | sub _hdf_walk { | ||
| 181 | my $hdf = shift; | ||
| 182 | $hdf->sortObj("_hdf_sort"); | ||
| 183 | my $child = $hdf->objChild; | ||
| 184 | _hdf_walk($child) if ($child); | ||
| 185 | my $next = $hdf->objNext; | ||
| 186 | _hdf_walk($next) if ($next); | ||
| 187 | } | ||
| 188 | |||
| 189 | sub _hdf_sort { | ||
| 190 | my ( $a, $b ) = @_; | ||
| 191 | |||
| 192 | return $a->objName cmp $b->objName; | ||
| 193 | } | ||
| 194 | |||
| 195 | =head1 SEE ALSO | ||
| 196 | |||
| 197 | =over 4 | ||
| 198 | |||
| 199 | =item http://www.clearsilver.net/ | ||
| 200 | |||
| 201 | This module requires ClearSilver and ClearSilver's perl binding. | ||
| 202 | |||
| 203 | =item http://www.clearsilver.net/docs/perl/ | ||
| 204 | |||
| 205 | ClearSilver perl binding documentation. | ||
| 206 | |||
| 207 | =item L<Data::Structure::Util> | ||
| 208 | |||
| 209 | =item L<File::Slurp> | ||
| 210 | |||
| 211 | =item L<File::Temp> | ||
| 212 | |||
| 213 | =back | ||
| 214 | |||
| 215 | =head1 AUTHOR | ||
| 216 | |||
| 217 | Toru Yamaguchi, C<< <zigorou@cpan.org> >> | ||
| 218 | |||
| 219 | =head1 BUGS | ||
| 220 | |||
| 221 | Please report any bugs or feature requests to | ||
| 222 | C<bug-data-clearsilver-hdf@rt.cpan.org>, or through the web interface at | ||
| 223 | L<http://rt.cpan.org>. I will be notified, and then you'll automatically be | ||
| 224 | notified of progress on your bug as I make changes. | ||
| 225 | |||
| 226 | =head1 COPYRIGHT & LICENSE | ||
| 227 | |||
| 228 | Copyright 2007 Toru Yamaguchi, All Rights Reserved. | ||
| 229 | |||
| 230 | This program is free software; you can redistribute it and/or modify it | ||
| 231 | under the same terms as Perl itself. | ||
| 232 | |||
| 233 | =cut | ||
| 234 | |||
| 235 | 1; # End of Data::ClearSilver::HDF |