Data::ClearSilver::HDF - Convert from Perl Data Structure to ClearSilver HDF


Data-ClearSilver-HDF documentation Contained in the Data-ClearSilver-HDF distribution.

Index


Code Index:

NAME

Top

Data::ClearSilver::HDF - Convert from Perl Data Structure to ClearSilver HDF

VERSION

Top

version 0.04

SYNOPSIS

Top

  use Data::ClearSilver::HDF;

  my $data = {
    foo => {
      bar => 1,
      baz => [0 .. 5]
    },
    obj => bless { foo => "xxx", bar => "yyy" }
  };

  my $hdf = Data::ClearSilver::HDF->hdf($data);

  print $hdf->getValue("obj.foo", undef); # xxx

PROPERTIES

Top

$USE_SORT

Sorting each keys hieralcally. default false;

METHODS

Top

hdf($data)

The argument $data must be reference. In the data, all of value excluded ARRAY, HASH, blessed reference will be ignored.

Blessed reference will be unblessed by Data::Structure::Util's unbless functon.

hdf_dump($hdf)

Dump as string from ClearSilver::HDF object. This method will create temporary file.

hdf_scalar($hdf, $keys, $data)

Translate scalar data to hdf. Please don't call directory.

hdf_array($hdf, $keys, $data)

Translate array reference data to hdf. Please don't call directory.

hdf_hash($hdf, $keys, $data)

Translate hash reference data to hdf. Please don't call directory.

SEE ALSO

Top

http://www.clearsilver.net/

This module requires ClearSilver and ClearSilver's perl binding.

http://www.clearsilver.net/docs/perl/

ClearSilver perl binding documentation.

Data::Structure::Util
File::Slurp
File::Temp

AUTHOR

Top

Toru Yamaguchi, <zigorou@cpan.org>

BUGS

Top

Please report any bugs or feature requests to bug-data-clearsilver-hdf@rt.cpan.org, or through the web interface at http://rt.cpan.org. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

COPYRIGHT & LICENSE

Top


Data-ClearSilver-HDF documentation Contained in the Data-ClearSilver-HDF distribution.
package Data::ClearSilver::HDF;

use strict;
use warnings;

use ClearSilver;
use Data::Structure::Util qw(unbless has_circular_ref circular_off);
use File::Slurp qw(slurp);
use File::Temp;

our $VERSION = '0.04';

our $USE_SORT = 0;

sub hdf {
    my ( $class, $data ) = @_;

    unbless($data);
    circular_off($data) if ( has_circular_ref($data) );

    my $hdf       = ClearSilver::HDF->new;
    my $data_type = ref $data;

    unless ( $data_type && ( $data_type eq "ARRAY" || $data_type eq "HASH" ) ) {
        return $hdf;
    }
    else {
        my $method = "hdf_" . lc($data_type);
        $class->$method( $hdf, undef, $data );
        _hdf_walk($hdf) if ($USE_SORT);
        return $hdf;
    }
}

sub hdf_dump {
    my ( $class, $hdf ) = @_;

    my $fh = File::Temp->new;
    $hdf->writeFile( $fh->filename );

    return slurp( $fh->filename );
}

sub hdf_scalar {
    my ( $class, $hdf, $keys, $data ) = @_;

    $hdf->setValue( join( ".", @$keys ), $data );
}

sub hdf_array {
    my ( $class, $hdf, $keys, $data ) = @_;

    $keys ||= [];
    my $idx = 0;

    for my $value (@$data) {
        my @keys      = @$keys;
        my $value_ref = ref $value;

        push( @keys, $idx );

        unless ( defined $value && $value_ref ) {
            $class->hdf_scalar( $hdf, \@keys, $value );
        }
        elsif ( $value_ref eq "ARRAY" ) {
            $class->hdf_array( $hdf, \@keys, $value );
        }
        elsif ( $value_ref eq "HASH" ) {
            $class->hdf_hash( $hdf, \@keys, $value );
        }
        else {
            next;
        }

        $idx++;
    }
}

sub hdf_hash {
    my ( $class, $hdf, $keys, $data ) = @_;

    $keys ||= [];

    while ( my ( $key, $value ) = each %$data ) {
        my @keys      = @$keys;
        my $value_ref = ref $value;

        push( @keys, $key );

        unless ( defined $value && $value_ref ) {
            $class->hdf_scalar( $hdf, \@keys, $value );
        }
        elsif ( $value_ref eq "ARRAY" ) {
            $class->hdf_array( $hdf, \@keys, $value );
        }
        elsif ( $value_ref eq "HASH" ) {
            $class->hdf_hash( $hdf, \@keys, $value );
        }
        else {
            next;
        }
    }
}

### private method

sub _hdf_walk {
    my $hdf = shift;
    $hdf->sortObj("_hdf_sort");
    my $child = $hdf->objChild;
    _hdf_walk($child) if ($child);
    my $next = $hdf->objNext;
    _hdf_walk($next) if ($next);
}

sub _hdf_sort {
    my ( $a, $b ) = @_;

    return $a->objName cmp $b->objName;
}

1;    # End of Data::ClearSilver::HDF