Data::Phrasebook::Loader::Ini - Absract your phrases with ini files.


Data-Phrasebook-Loader-Ini documentation Contained in the Data-Phrasebook-Loader-Ini distribution.

Index


Code Index:

NAME

Top

Data::Phrasebook::Loader::Ini - Absract your phrases with ini files.

SYNOPSIS

Top

    use Data::Phrasebook;

    my $q = Data::Phrasebook->new(
        class  => 'Fnerk',
        loader => 'Ini',
        file   => 'phrases.ini',
    );

    # simple keyword to phrase mapping
    my $phrase = $q->fetch($keyword);

    # keyword to phrase mapping with parameters
    $q->delimiters( qr{ \[% \s* (\w+) \s* %\] }x );
    my $phrase = $q->fetch($keyword,{this => 'that'});

ABSTRACT

Top

This module provides a loader class for phrasebook implementations using INI files.

DESCRIPTION

Top

This module provides a base class for phrasebook implementations.

Phrases can be contained within one or more dictionaries, with each phrase accessible via a unique key. Phrases may contain placeholders, please see Data::Phrasebook for an explanation of how to use these. Groups of phrases are kept in a dictionary. In this implementation a dictionary is considered to be equivilent to a section in an ini file.

An example ini file:

  [BASE]
  foo=\
    Welcome to :my world. \
    It is a nice :place.

Within the phrase text placeholders can be used, which are then replaced with the appropriate values once the get() method is called. The default style of placeholders can be altered using the delimiters() method.

INHERITANCE

Top

Data::Phrasebook::Loader::Ini inherits from the base class Data::Phrasebook::Loader::Base. See that module for other available methods and documentation.

METHODS

Top

load

Given a file, load it. file must contain a INI style layout.

   $loader->load( $file, $dict );

This method is used internally by Data::Phrasebook::Generic's data method, to initialise the data store.

It must take a file (be it a scalar, or something more complex) and return a handle.

get

Returns the phrase stored in the phrasebook, for a given keyword.

   my $value = $loader->get( $key );

dicts

Returns the list of dictionaries available.

   my @dicts = $loader->dicts();

keywords

Returns the list of keywords available. List is lexically sorted.

   my @dicts = $loader->keywords();

CONTINUATION LINES

Top

As this module uses Config::IniFiles, it allows for the use of continuation lines as follows:

  [Section]
  Parameter=this parameter \
    spreads across \
    a few lines

SEE ALSO

Top

Data::Phrasebook, Config::IniFiles.

BUGS, PATCHES & FIXES

Top

There are no known bugs at the time of this release. However, if you spot a bug or are experiencing difficulties, that is not explained within the POD documentation, please send an email to barbie@cpan.org or submit a bug to the RT system (http://rt.cpan.org/). However, it would help greatly if you are able to pinpoint problems or even supply a patch.

Fixes are dependant upon their severity and my availablity. Should a fix not be forthcoming, please feel free to (politely) remind me.

DSLIP

Top

  b - Beta testing
  d - Developer
  p - Perl-only
  O - Object oriented
  p - Standard-Perl: user may choose between GPL and Artistic

AUTHOR

Top

  Barbie, <barbie@cpan.org>
  for Miss Barbell Productions <http://www.missbarbell.co.uk>.

COPYRIGHT AND LICENSE

Top


Data-Phrasebook-Loader-Ini documentation Contained in the Data-Phrasebook-Loader-Ini distribution.
package Data::Phrasebook::Loader::Ini;
use strict;
use warnings FATAL => 'all';
use Carp qw( croak );
use base qw( Data::Phrasebook::Loader::Base Data::Phrasebook::Debug );
use Config::IniFiles;

our $VERSION = '0.10';

sub load
{
    my ($class, $file, $dict) = @_;
    croak "No file given as argument!" unless defined $file;
	croak "Cannot read configuration file [$file]\n"
		unless(-r $file);

	my $cfg = Config::IniFiles->new(
					-file => $file,
					-allowcontinue => 1,	# allows continuation lines
				);
	croak "Cannot access configuration file [$file]".
			" - [@Config::IniFiles::errors]\n"	unless($cfg);
	$class->{cfg} = $cfg;

	# what sections are we using?
	($class->{default}) = $cfg->Sections;
	$class->{dict} = $class->{default};
	$class->{dict} = $dict
  		if($dict && $class->{cfg}->SectionExists( $dict ));
};

sub get {
	my ($class, $key) = @_;

  	my $data = $class->{cfg}->val( $class->{dict}, $key );
  	$data = $class->{cfg}->val( $class->{default}, $key )	unless($data);
	return	unless($data);

	$data =~ s!^\s+!!s;
	$data =~ s!\s+$!!s;
	$data =~ s!\s+! !sg;

	return $data;
}

sub dicts {
	my $class = shift;
    $class->{cfg}->Sections
}

sub keywords {
	my $class = shift;
	my $dict  = shift;

    return sort $class->{cfg}->Parameters($dict) if($dict);

    my @keywords = $class->{cfg}->Parameters($class->{dict});
    push @keywords, $class->{cfg}->Parameters($class->{default})    
        unless($class->{dict} eq $class->{default});

    my %keywords = map {$_=>1} @keywords;
    return sort keys %keywords;
}

1;

__END__