Data::Properties::YAML - YAML-ized properties for your application


Data-Properties-YAML documentation Contained in the Data-Properties-YAML distribution.

Index


Code Index:

NAME

Top

Data::Properties::YAML - YAML-ized properties for your application

SYNOPSIS

Top

  use Data::Properties::YAML;

  my $yaml = Data::Properties::YAML->new(
    properties_file => '/etc/properties.yaml'
  );

  # OR:
  my $yaml = Data::Properties::YAML->new(
    yaml_data => <<'YAML',
  ---
  password_resend:
    general:
      is_not_found: Invalid email address
    contact_email:
      is_missing: Required
      is_invalid: Invalid email address
      is_not_found: Email is not valid - please try again.
  YAML
    );

  # Access your properties:
  print "Error: " . $yaml->general->is_not_found;

  # Access another property:
  print "Another error: " . $yaml->contact_email->is_missing;

  # Dies "Node root.general has no property named 'isnt_found'"
  $yaml->general->isnt_found; 

DESCRIPTION

Top

YAML is a simple way to store many strings. Why not use it in place of the typical "properties" file as used by java.util.properties?

Why not give ourselves a nice Perl-ish interface?

Well, here we go. Use Data::Properties::YAML and you have just that.

METHODS

Top

new( properties_file => '/path/to/file.yaml' )

Returns a new Data::Properties::YAML object based on the structure of your YAML.

new( yaml_data => $yaml )

Returns a new Data::Properties::YAML object based on the structure of your YAML.

SEE ALSO

Top

YAML

BUGS

Top

It's possible that some bugs have found their way into this release.

Use RT http://rt.cpan.org/NoAuth/Bugs.html?Dist=Data-Properties-YAML to submit bug reports.

HOMEPAGE

Top

Please visit the Apache2::ASP homepage at http://www.devstack.com/ to see examples of Data::Properties::YAML in action.

AUTHOR

Top

John Drago mailto:jdrago_999@yahoo.com

COPYRIGHT AND LICENSE

Top


Data-Properties-YAML documentation Contained in the Data-Properties-YAML distribution.

package Data::Properties::YAML;

use strict;
use warnings 'all';
use YAML 'Load';

our $VERSION = 0.03;


#====================================================================
sub new
{
  my ($class, %args) = @_;
  
  if( $args{properties_file} )
  {
    my $yaml_file = $args{properties_file};
    open my $ifh, '<', $yaml_file
      or die "Cannot open '$yaml_file': $!";
    my $data = '';
    while( my $line = <$ifh> )
    {
      $line =~ s/\t/  /
        while $line =~ m/\t/;
      $data .= $line;
    }# end while()
    $args{data} = Load( $data );
    $args{__name} = 'root';
  }
  elsif( $args{yaml_data} )
  {
    $args{data} = Load( $args{yaml_data} );
    $args{__name} = 'root';
  }# end if()
  
  my $s = bless \%args, $class;
  if( ref($s->{data}) )
  {
    foreach my $key ( keys(%{ $s->{data} }) )
    {
      next unless ref($s->{data}->{$key});
      $s->{$key} = ref($s)->new(
        __name => "$s->{__name}.$key",
        data => $s->{data}->{$key},
      );
    }# end foreach()
  }# end if()
  
  # Finally:
  return $s;
}# end new()


#====================================================================
sub AUTOLOAD
{
  my $s = shift;
  our $AUTOLOAD;
  
  my ($name) = $AUTOLOAD =~ m/::([^:]+)$/;
  die "Node $s->{__name} has no property named '$name'"
    unless exists( $s->{$name} ) || exists( $s->{data}->{$name} );
  
  return $s->{$name} || $s->{data}->{$name};
}# end AUTOLOAD()

sub DESTROY { }

1;# return true:

__END__