Data::...::YAML(3pm)

πŸ“¦
version 0.13 Β· 2 versions
  • 0.13 Β· debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking (latest)
  • 0.13 Β· ubuntu jammy
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

✨ Data::Phrasebook::Loader::YAML β€” the manual page, bestie edition ✨

okay so this is Data::...::YAML(3pm) from the User Contributed Perl Documentation section, and honestly? it's giving phrasebook but make it YAML πŸ’…


πŸ’– NAME πŸ’–

Data::Phrasebook::Loader::YAML β€” Absract your phrases with YAML.

(yes, "Absract". that's the actual typo in the original and I am not touching it. iconic behavior tbh 🌸)


πŸ¦‹ SYNOPSIS πŸ¦‹

use Data::Phrasebook;

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

$q->delimiters( qr{ \[% \s* (\w+) \s* %\] }x );
my $phrase = $q->fetch($keyword);

that's it. that's the whole vibe check. you make a phrasebook, you point it at a .yaml file, you set your delimiters, you fetch your keyword. periodt. ✨


🌸 DESCRIPTION 🌸

so this class loader implements phrasebook patterns using YAML. lowkey the whole point.

Phrases can live inside one or more dictionaries, and every phrase is reachable via a unique key. Phrases may contain placeholders β€” go read Data::Phrasebook for the full explanation of how to use those, bestie, I'm not re-explaining it here. Groups of phrases are kept in a dictionary, and in this implementation a single file is one complete dictionary.

here's an example YAML file, no cap:

---
foo: >
  Welcome to [% my %] world.
  It is a nice [%place %].

Within the phrase text you can use placeholders, and those get replaced with the appropriate values once the get() method is called. The default style of placeholders can be altered using the delimiters() method. slay. πŸ’–


🧚 INHERITANCE 🧚

Data::Phrasebook::Loader::YAML inherits from the base class Data::Phrasebook::Loader::Base. See that module for other available methods and documentation. it's the parent, it's the blueprint, it's the moment. πŸ‘‘


πŸ”₯ METHODS πŸ”₯

🌈 load

Given a file, load it. file must contain a YAML map.

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

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

It must take a file (be it a scalar, or something more complex) and return a handle. The dict is optional, should you wish to use the dictionary support. optional means optional, bestie β€” no pressure 🫢

🌈 get

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

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

If one or more named dictionaries have been previously selected, they will be searched in order, followed by the default dictionary. The first hit on key will be returned, otherwise undef is returned. first come first served, and if nobody shows up? undef. πŸ’€

🌈 dicts

Returns the list of dictionaries available.

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

This is the list of all dictionaries available in the source file. If multiple dictionaries are not being used, then an empty list will be returned. so like, an empty list. nothing. silence. πŸ™ˆ

🌈 keywords

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

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

If one or more named dictionaries have been previously selected, they will be farmed for keywords, followed by the default dictionary. "farmed" β€” the original said that, I'm keeping it, it's giving cottagecore 🌾

The dict argument is optional, and may be used to override the search to a single named dictionary, or a list of dictionaries if passed by reference, plus the default dictionary of course.

To find all available keywords in all available dictionaries, use the following:

$loader->keywords( [ $loader->dicts ] );

go off, that's the full sweep ✨

🌈 set_default

If a requested phrase is not found in the named dictionary an attempt is made to find it in the default dictionary. Data::Phrasebook loaders normally use the first dictionary in the phrasebook as the default, but as mentioned in "DICTIONARY SUPPORT" this does not make sense because the dictionaries in YAML phrasebooks are not ordered. unordered. chaos. no fixed first place. 😭

To override the automatically selected default dictionary use this method, and pass it a default_dictionary_name. This value is only reset at phrasebook load time, so you'll probably need to trigger a reload:

$q->loader->set_default( $default_dictionary_name );
$q->loader->load( $file );

To reset the loader's behaviour to automatic default dictionary selection, pass this method an undefined value, and then reload. undef it and reload, bestie, that's the reset button πŸŽ€


πŸ’œ DICTIONARY SUPPORT πŸ’œ

This loader supports the use of dictionaries, as well as multiple dictionaries with a search order. If you are unfamiliar with these features, see Data::Phrasebook for more information.

Source data format for a single unnamed dictionary is a YAML stream that has as its root node an anonymous hash, like so:

---
first_key: first_value
second_key: second_value

In this case, specifying one or more named dictionaries will have no effect. The single dictionary that comprises the YAML file will take the place of your default dictionary, so will always be searched. To override this behaviour use the set_default object method.

Multiple dictionaries must be specified using a two-level hash system, where the root node of your YAML file is an anonymous hash containing dictionary names, and the values of those hash keys are further anonymous hashes containing the dictionary contents. Here is an example:

---
dict_one:
   first_key: first_value
   second_key: second_value
dict_two:
   first_key: first_value
   second_key: second_value
⚠️ If you use any other structure for your YAML dictionary files, the result is uncertain, and this loader module is very likely to crash your program. do not do this. this is the forbidden structure. πŸš«πŸ’€

If a requested phrase is not found in the named dictionary an attempt is made to find it in the default dictionary. Data::Phrasebook loaders normally use the first dictionary in the phrasebook as the default, but this does not make sense because YAML phrasebook files contain an unordered hash of dictionaries.

This loader will therefore select the first dictionary from the list of lexically sorted dictionary names to be the default. To override this behaviour use the set_default object method. Alternatively, just include a dictionary that is guaranteed to be selected for the default (e.g. "0000default"); it need not contain any keys. sneaky little hack, and it works πŸ’…


🌈 SEE ALSO 🌈

Data::Phrasebook, Data::Phrasebook::Loader.


πŸ› BUGS, PATCHES & FIXES πŸ›

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. receipts help, bestie 🫢

Fixes are dependent upon their severity and my availability. Should a fix not be forthcoming, please feel free to (politely) remind me. politely. we said politely. πŸŽ€


πŸ‘‘ AUTHOR πŸ‘‘

Original author: Iain Campbell Truskett (16.07.1979 - 29.12.2003)
Maintainer: Barbie <barbie@cpan.org> since January 2004.
for Miss Barbell Productions <http://www.missbarbell.co.uk>.

Copyright (C) 2003 Iain Truskett.
Copyright (C) 2004-2014 Barbie for Miss Barbell Productions.

This distribution is free software; you can redistribute it and/or
modify it under the Artistic License 2.0.

and that's the whole page, bestie. this came from the Perl documentation, perl v5.34.0, dated 2022-06-12 β€” shoutout to the version and the date, they're part of the lore ✨ πŸ’–