Data::Phrasebook(3pm)

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

πŸ’– Data::Phrasebook πŸ’–

okay so bestie, welcome to Data::Phrasebook(3pm) β€” the Perl manual page that's about to change your whole relationship with queries. no cap. it's a User Contributed Perl Documentation page and it's giving iconic framework energy. let's go πŸ¦‹

✨ NAME ✨

Data::Phrasebook β€” Abstract your queries! πŸ’…

(yes that's the actual tagline, and yes it ate.)

✨ SYNOPSIS ✨

okay so here's the whole vibe in code form:

use Data::Phrasebook;

my $q = Data::Phrasebook->new(
    class  => 'Plain',
    loader => 'Text',
    file   => 'phrases.txt',
);

# 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'});

literally just new it, fetch it, done. slay. ✨

✨ DESCRIPTION ✨

so Data::Phrasebook is a whole collection of modules for accessing phrasebooks from various data sources. that's it, that's the pitch. lowkey elegant. πŸŽ€

🌸 PHRASEBOOKS 🌸

if you wanna actually understand what a phrasebook is, bestie, Rani Pinchuk (the author of Class::Phrasebook) wrote an article on Perl.com and it's worth the read:

<http://www.perl.com/pub/a/2002/10/22/phrasebook.html>

the common uses of phrasebooks are: handling error codes, accessing databases via SQL queries, and written language phrases. examples are the mime.types file and the hosts file β€” both of which use a simple phrasebook design. periodt.

now, unfortunately, Class::Phrasebook is a complete work and not a true class based framework. πŸ’€ and if you can't install XML libraries, you cannot use it. no cap.

this distribution is a collaboration between Iain Truskett and myself to create an extendable and class based framework for implementing phrasebooks. 🫢

πŸ‘‘ CLASSES πŸ‘‘

in creating a phrasebook object, a class type is required. this class defines the nature of the phrasebook or the behaviours associated with it. currently there are two classes: Plain and SQL.

  • the Plain class is the default class, and allows retrieval of phrases via the fetch() method. the fetch() simply returns the phrase that maps to the given keyword. simple. clean. iconic.
  • the SQL class allows specific database handling. phrases are retrieved via the query() method. the query() method internally retrieves the SQL phrase, then returns the statement handler object, which the user can then perform a prepare/execute/fetch/finish sequence on. for more details see Data::Phrasebook::SQL. πŸ”₯

✨ CONSTRUCTOR ✨

new

the arguments to new depend upon the exact class you're creating. iykyk.

the default class is "Plain" and only requires the Loader arguments. the "SQL" class requires a database handle as well as the Loader arguments.

the "class" argument defines the object class of the phrasebook and the behaviours that can be associated with it. using "Foobar" as a fake class, the class module is searched for in the following order:

  1. if you've subclassed "Data::Phrasebook", for example as "Dictionary", then Dictionary::Foobar is tried.
  2. if that failed, Data::Phrasebook::Foobar is tried.
  3. if that failed, Foobar is tried.
  4. if all the above failed, we croak. 😭

this should allow you some flexibility in what sort of classes you use while not having you type too much. bestie convenience. πŸ’…

for other parameters, see the specific class you wish to instantiate. the class argument is removed from the arguments list and the "new" method of the specified class is called with the remaining arguments.

🌈 DELIMITERS 🌈

delimiters allow for variable substitution in the phrase. the default style is ':variable', which would be passed as:

$q->delimiters( qr{ :(\w+) }x );

as an alternative, a Template Toolkit style would be passed as:

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

pick your fighter, both are valid, both are cute. πŸŽ€

🧚 DICTIONARIES 🧚

πŸ’œ Simple Dictionaries

Data::Phrasebook supports the use of dictionaries. see the specific Loader module to see how to implement the dictionary within your phrasebook. using Data::Phrasebook::Loader::Ini as an example, the dictionary might be laid out as:

[Stuff]
language=Perl
platform=Linux

[Nonsense]
platform=Windows

the phrasebook object is then created and used as:

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

my $language = $q->fetch('language'); # retrieves 'Perl'
my $platform = $q->fetch('platform'); # retrieves 'Windows'

the former is from the default (first) dictionary, and the second is from the named dictionary ('Nonsense'). if a phrase is not found in the named dictionary an attempt is made to find it in the default dictionary. otherwise undef will be returned.

once a dictionary or file is specified, changing either requires reloading. as this is done at the loader stage, we need to let it know what it needs to reload. ⚠️

this can be done with the either (or both) of the following:

$q->file('phrases2.ini');
$q->dict('Stuff');

a subsequent fetch() will then reload the file and dictionary, before retrieving the phrase required. however, a reload only takes place if both the file and the dictionary passed are not the ones currently loaded. (so no unnecessary reloading, we love efficiency. 🫢)

πŸ¦‹ Multiple Dictionaries

as of version 0.25, the ability to provide prescendence over multiple dictionaries for the same phrasebook. (yes that's how the original spells it, i'm keeping it, character for character, that's the rule bestie.) using Data::Phrasebook::Loader::Ini again as an example, the phrasebook might be laid out as:

[AndTheOther]
language=Perl
platform=Linux
network=LAN

[That]
platform=Solaris
network=WLAN

[This]
platform=Windows

the phrasebook object is then created and used as:

my $q = Data::Phrasebook->new(
  class  => 'Plain',
  loader => 'Ini',
  file   => 'phrases.ini',
  dict   => ['This','That','AndTheOther'],
);

my $language = $q->fetch('language'); # retrieves 'Perl'
my $platform = $q->fetch('platform'); # retrieves 'Windows'
my $network  = $q->fetch('nework');   # retrieves 'WLAN'

(yes, 'nework' β€” that's exactly how it's written up there, typo and all, and i am not touching it. verbatim or nothing. πŸ’€)

the first dictionary, if not specified and supported by the Loader module, is still used as the default dictionary.

the dictionaries can be specified, or reordered, using the object method:

$q->dict('That','AndTheOther','This');

a subsequent reload will occur with the next fetch call.

🌸 DEDICATION 🌸

much of the code for the original class framework is from Iain's original code. my code was much simpler and was tied to using just an INI data source. merging all the ideas and code together we came up with this distribution.

unfortunately Iain died in December 2003, so he never got to see or play with the final working version. i can only thank him for his thoughts and ideas in getting this distribution into a state worthy of release. πŸ₯Ί

  Iain Campbell Truskett (16.07.1979 - 29.12.2003)

✨ SEE ALSO ✨

Data::Phrasebook::Plain, Data::Phrasebook::SQL, Data::Phrasebook::SQL::Query, Data::Phrasebook::Debug, Data::Phrasebook::Generic, Data::Phrasebook::Loader, Data::Phrasebook::Loader::Text, Data::Phrasebook::Loader::Base.

go read them, they're all part of the family. 🫢

πŸ’– SUPPORT πŸ’–

please see the README file.

πŸ‘‘ 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-2013 Barbie for Miss Barbell Productions.

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

and that's the whole page bestie!! 🌈 this one shipped as perl v5.36.0, dated 2022-11-19, and it lives under the manual name Data::Phrasebook(3pm). take care of it, it's iconic. mwah πŸ’…πŸ’–βœ¨