| RDF-YAML documentation | Contained in the RDF-YAML distribution. |
RDF::YAML - RDF YAML parser and dumper
This document describes version 0.11 of RDF::YAML, released October 22, 2003.
# Get triples from a RDF/YAML file
$rdf = RDF::YAML->new;
$triples = $rdf->parse_file("input.yml");
# Declare namespaces
my %namespaces = ( 'rss' => 'http://purl.org/rss/1.0/' );
$rdf->set_namespaces( \%namespaces );
# Translate RDF/XML to RDF/YAML
my @triples = RDF::Simple::Parser->new->parse_rdf($xml_string);
$rdf->set_triples(\@triples);
$rdf->dump_file("output.yml");
# Add new triples to a RDF/YAML string
$rdf->parse_string($yaml_string);
$rdf->add_triples(\@triples);
$yaml_string = $rdf->dump_string;
This module is a RDF/YAML parser/dumper; it can parse RDF/YAML files or strings, provide results as triples, and dump triples back to RDF/YAML format. It is only a thin wrapper around RDF::Simple::Parser::YAML and RDF::Simple::Serialiser::YAML.
Note that this is a proof-of-concept work; the RDF/YAML format used by this module is purely experimental, and may change without notice.
Constructor. Currently takes no parameters.
Parses a RDF/YAML file specified by $file. Returns an array reference
to parsed triples, in the standard [ $subject, $predicate, $object ]
format. This also replaces all triples and namespaces stored within
the object.
Similar to parse_file, but parses RDF/YAML data from string.
Writes all triples stored in the object into a file specified by $file,
in RDF/YAML format.
Similar to dump_file, but returns a RDF/YAML string instead.
Returns a reference to array of triples currently stored in this object.
Takes a reference to array of triples and stores it in the object, replacing previous ones. If invoked with no arguments, clears the stored triples.
Similar to set_triples, but adds to currently stored triples instead
of replacing them.
Returns a hash reference of namespaces currently in use.
Default namespaces are the same as listed in RDF::Simple::Serialiser.
Takes a hash reference to namespaces expressed as $qname => $uri
pairs, and adds them to subsequent RDF/YAML documents. This overrides
(but does not clear) the default ones.
Similar to set_ns, but adds to currently stored namespaces instead
of replacing them.
Autrijus Tang <autrijus@autrijus.org>
Copyright 2003 by Autrijus Tang <autrijus@autrijus.org>.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
| RDF-YAML documentation | Contained in the RDF-YAML distribution. |
# $File: //member/autrijus/RDF-YAML/lib/RDF/YAML.pm $ $Author: autrijus $ # $Revision: #2 $ $Change: 8524 $ $DateTime: 2003/10/22 05:20:04 $ package RDF::YAML; $RDF::YAML::VERSION = '0.11'; use strict;
sub new { my $class = shift; bless({ triples => [], parser => undef, serialiser => undef, }, $class); }
sub parse_file { my ($self, $file) = @_; local $/; local *FH; open FH, $file or die $!; $self->parse_string(<FH>); close FH; return $self->get_triples; } sub parse_string { my $self = shift; $self->set_triples( [ $self->_parser->new->parse_rdf(@_) ] ); $self->set_ns( $self->_parser->ns ); return $self->get_triples; }
sub dump_file { my ($self, $file) = @_; local *FH; open FH, "> $file" or die $!; print FH $self->dump_string; close FH; } sub dump_string { my $self = shift; require RDF::Simple::Serialiser::YAML; $self->_serialiser->serialise(@{$self->get_triples || []}); }
sub get_triples { $_[0]->{triples} } sub set_triples { $_[0]->{triples} = ($_[1] || []) } sub add_triples { push @{$_[0]->get_triples}, @{$_[1] || []} }
sub get_ns { my $self = shift; return { $self->_serialiser->ns->lookup }; } sub set_ns { my $self = shift; delete $self->_serialiser->ns->{_lookup}; return $self->add_ns(@_); } sub add_ns { my $self = shift; return { $self->_serialiser->addns(%{$_[0]||{}}) }; } # Internal methods sub _parser { my $self = shift; require RDF::Simple::Parser::YAML; $self->{parser} ||= RDF::Simple::Parser::YAML->new; } sub _serialiser { my $self = shift; require RDF::Simple::Serialiser::YAML; $self->{serialiser} ||= RDF::Simple::Serialiser::YAML->new; } 1;