Catman...PerlAPI(3pm)

πŸ“¦
version 1.2025 Β· 3 versions
, in ubuntu resolute, ubuntu stonking

Catmandu::PerlAPI πŸ’–

okay bestie, this is the Catman...PerlAPI(3pm) page from the User Contributed Perl Documentation, and it's lowkey a whole tour of the Catmandu Perl API. grab a snack, we're doing this. ✨


🌸 NAME 🌸

Catmandu::PerlAPI β€” a short overview of the current Catmandu Perl API. periodt.


πŸ¦‹ SYNOPSIS πŸ¦‹

    use Catmandu;

    # If you have Catmandu::OAI and Catmandu::MongoDB installed
    my $importer = Catmandu->importer('OAI',url => 'https://biblio.ugent.be/oai')
    my $store    = Catmandu->store('MongoDB',database_name => 'test');

    # Import all the OAI records into MongoDB
    $store->add_many($importer);

    # Export all the MongoDB records to YAML and apply some fixes
    # myfixes.txt:
    #   upcase(title.*)
    #   remove_field(_metadata)
    #   join_field(creator,'; ')
    #   join_field(subject,'-- ')
    my $fixer    = Catmandu->fixer('myfixes.txt');
    my $exporter = Catmandu->exporter('YAML');

    $exporter->add_many( $fixer->fix($store) );
    $exporter->commit;

look at that. import, store, fix, export. it's giving pipeline. πŸ’…


πŸŽ€ DESCRIPTION πŸŽ€

so this doc is a short overview of the Perl API of the 1.X version of Catmandu.

⚠️ real talk: the Perl API is provided as-is and might change in the future. if you want the stable, no-surprises experience, the docs point you to the command line interface of the tool. so like, for production vibes? CLI. for experimenting? go off. πŸ’–

✨ USE ✨

to pull Catmandu into a Perl script, you load it with a use command:

    use Catmandu;

by default, no methods are imported into the Perl context. nothing. zero. nada. if you want all of them, or just some, you pass them as a list to the use command:

    use Catmandu -all;
    use Catmandu qw(config store exporter);

Catmandu can also load configuration options for exports, importers, fixers via configuration files (peep the CONFIG section below πŸ‘€). if you add the --load option (optionally with a path) to the use command, those config files get loaded right at the start of your script:

    use Catmandu -load;
    use Catmandu -load => ['/my/config/directory'];

    # or use all the options
    use Catmandu -all, -load => [qw(/config/path' '/another/config/path)];
🫢 yes that qw(...) in the last one really does look like that in the source. we don't touch it. we respect it.

πŸ‘‘ CLASS METHODS πŸ‘‘

πŸ’œ log

returns the current Log::Any logger.

    use Catmandu;
    use Log::Any::Adapter;
    use Log::Log4perl;

    Log::Any::Adapter->set('Log4perl'); # requires Log::Any::Adapter::Log4perl
    Log::Log4perl::init('./log4perl.conf');

    my $logger = Catmandu->log;
    $logger->info("Starting main program");

with log4perl.conf like:

    # Send a copy of all logging messages to STDERR
    log4perl.rootLogger=DEBUG,STDERR

    # Logging specific for your main program
    log4perl.category.myprog=INFO,STDERR

    # Logging specific for on part of Catmandu
    log4perl.category.Catmandu::Fix=DEBUG,STDERR

    # Where to send the STDERR output
    log4perl.appender.STDERR=Log::Log4perl::Appender::Screen
    log4perl.appender.STDERR.stderr=1
    log4perl.appender.STDERR.utf8=1

    log4perl.appender.STDERR.layout=PatternLayout
    log4perl.appender.STDERR.layout.ConversionPattern=%d [%P] - %p %l time=%r : %m%n

loggers but make it aesthetic 🌈


πŸ’œ default_load_path(['/default/path'])

when you call it with no argument, it returns the default location where Catmandu goes looking for configuration and lib. if you hand it a path, it sets the default location to that.

the default load path is the script directory, or its parent if the script directory is bin.


πŸ’œ load

loads all the configuration options in the catmandu.yml configuration file. the CONFIG section below has extended examples of configuration options. πŸ’–


πŸ’œ load('/path', '/another/path')

loads all the configuration options stored at alternative paths.

a load path of :up will search upwards from your program for configuration. cute.

see CONFIG below for extended examples of configuration options.


πŸ’œ roots

returns an ARRAYREF of paths where configuration was found.

πŸ“Œ heads up: this list is empty before load.

πŸ’œ root

returns the first</s> path where configuration was found.

πŸ“Œ heads up again: this is undef before load.

πŸ’œ config

returns the current configuration as a HASHREF.


πŸ’œ config($config)

sets a new configuration and reloads the environment. slay. ✨


πŸ’œ default_store

returns the name of the default store.


πŸ’œ store([NAME])

returns an instance of Catmandu::Store. NAME is the name of a Catmandu::Store, or the name of a store configured in a catmandu.yml configuration file. when no NAME is given, the 'default' store in the configuration file gets used.

e.g. if the configuration file catmandu.yml contains:

     store:
      default:
       package: ElasticSearch
       options:
         index_name: blog
      test:
       package: Mock

then in your program:

    # This will use ElasticSearch
    my $store = Catmandu->store('ElasticSearch', index_name => 'blog');

    # or because we have a 'default' set in the configuration file

    my $store = Catmandu->store('default');

    # or because 'default' will be used when no name was provided

    my $store = Catmandu->store;

    # This will use Mock
    my $store = Catmandu->store('test');

configuration settings can be overwritten by the store command:

  my $store2 = Catmandu->store('default', index_name => 'test2');

overriding? iconic behavior honestly. πŸ’…


πŸ’œ default_fixer

returns the name of the default fixer.


πŸ’œ fixer(NAME) / fixer(FIX,FIX) / fixer([FIX])

returns an instance of Catmandu::Fix. NAME can be the name of a fixer section in a catmandu.yml file. or, one or more Catmandu::Fix-es can be provided inline.

e.g. if the configuration file catmandu.yml contains:

     fixer:
      default:
        - do_this()
        - do_that()

then in your program all these lines below will create the same fixer:

    my $fixer = Catmandu->fixer('do_this()', 'do_that()');
    my $fixer = Catmandu->fixer(['do_this()', 'do_that()']);
    my $fixer = Catmandu->fixer('default');
    my $fixer = Catmandu->fixer(); # The default name is 'default'

FIX-es can also be written to a Fix script. e.g. if myfixes.txt contains:

 do_this()
 do_that()

then the above code will even be equivalent to:

   my $fixer = Catmandu->fixer('myfixes.txt');

four ways to say the same thing. we love a flexible queen. πŸŽ€


πŸ’œ default_importer

returns the name of the default importer.


πŸ’œ default_importer_package

returns the name of the default importer package, if no package name is given in the config or as a param.


πŸ’œ importer(NAME)

returns an instance of Catmandu::Importer. NAME is the name of a Catmandu::Importer, or the name of an importer configured in a catmandu.yml configuration file. when no NAME is given, the 'default' importer in the configuration file gets used.

e.g. if the configuration file catmandu.yml contains:

   importer:
     default:
       package: OAI
       options:
         url: http://www.instute.org/oai/

then in your program all these lines will be equivalent:

   my $importer = Catmandu->importer('OAI', url => 'http://www.instute.org/oai/');
   my $importer = Catmandu->importer('default');
   my $importer = Catmandu->importer(); # The default name is 'default'

configuration settings can be overwritten by the importer command:

   my $importer2 = Catmandu->importer('default', url => 'http://other.institute.org');

πŸ’œ default_exporter

returns the name of the default exporter.


πŸ’œ default_exporter_package

returns the name of the default exporter package, if no package name is given in the config or as a param.


πŸ’œ exporter([NAME])

returns an instance of Catmandu::Exporter with name NAME (or the default when no name is given). NAME can be in a configuration file β€” see 'importer' above. πŸ’–


πŸ’œ validator([NAME])

returns an instance of Catmandu::Validator with name NAME (or the default when no name is given). NAME can be in a configuration file β€” see 'importer' above.


πŸ’œ export($data,[NAME])

export data using a default or named exporter, or an exporter instance.

    Catmandu->export({ foo=>'bar'});

    my $importer = Catmandu::Importer::Mock->new;
    Catmandu->export($importer, 'YAML', file => '/my/file');
    Catmandu->export($importer, 'my_exporter');
    Catmandu->export($importer, 'my_exporter', exporter_option => '...' , ...);
    Catmantu->export($importer, Catmandu::Exporter::YAML->new);
πŸ™ˆ yes, that last line says Catmantu in the original. we do not fix it. we preserve it. it's part of the lore now.

πŸ’œ export_to_string

export data using a default or named exporter, straight into a string.

    my $importer = Catmandu::Importer::Mock->new;
    my $yaml = Catmandu->export_to_string($importer, 'YAML');
    # is the same as
    my $yaml = "";
    Catmandu->export($importer, 'YAML', file => \$yaml);

πŸ’œ import_from_string

import data from a string using a default or named importer. the return value should be an array of hashes.

    my $json = qq([{"name":"Nicolas"}]);
    {
        my $record = Catmandu->import_from_string( $json, "JSON" );
    }
    # is the same as
    {
        my $record = Catmandu->importer('JSON', file => \$json)->to_array()
    }

πŸ’œ define_importer

configure a new named importer.

    Catmandu->define_importer(books => CSV => (fields => 'title,author,publisher'));
    Catmandu->importer(books => (file => 'mybooks.csv'))->each(sub {
        my $book = shift;
        say $book->{title};
    });

    # this is equivalent to

    Catmandu->config->{importer}{books} = {
        package => 'CSV',
        options => {
            fields => 'title,author,publisher',
        },
    }

πŸ’œ define_exporter

configure a new named exporter.

    Catmandu->define_exporter('books', 'CSV', fix => 'capitalize(title)');
    my $csv = Catmandu->export_to_string({title => 'nexus'}, 'books');

    # this is equivalent to

    Catmandu->config->{exporter}{books} = {
        package => 'CSV',
        options => {
            fix => 'capitalize(title)',
        },
    }

πŸ’œ define_store

configure a new named store.

    Catmandu->define_store(mydb => MongoDB => (database_name => 'mydb'));
    Catmandu->store->bag('books')->get(1234);

    # this is equivalent to

    Catmandu->config->{store}{mydb} = {
        package => 'MongoDB',
        options => {
            database_name => 'mydb',
        },
    }

πŸ’œ define_fixer

configure a new named fixer.

    Catmandu->define_fixer('cleanup', [
        'trim(title)',
        'capitalize(title)',
        'remove_field(junk)',
        # ...
    ]);
    Catmandu->fixer('cleanup')->fix($record);

okay that's the first half of the page bestie, we're not done yet β€” more is coming, don't go anywhere πŸ’–βœ¨πŸ¦‹

✨ EXPORTS ✨

okay bestie so when you use Catmandu with an import list, these are the little functions it can hand you πŸ’– and every single one of them is just a shortcut β€” same energy, same behaviour, just less typing:

  • config β€” literally the same thing as Catmandu->config. πŸ’…
  • store β€” same as Catmandu->store. πŸŽ€
  • importer β€” same as Catmandu->importer. ✨
  • exporter β€” same as Catmandu->exporter. πŸ¦‹
  • validator β€” same as Catmandu->validator. 🌸
  • export β€” same as Catmandu->export. πŸ”₯
  • export_to_string β€” same as Catmandu->export_to_string. πŸ’œ
  • import_from_string β€” same as Catmandu->import_from_string. 🌈
  • fixer β€” same as Catmandu->fixer. 🧚
  • log β€” same as Catmandu->log. ⭐
  • -all, :all β€” this one just imports everything. the whole menu, no cap πŸ‘‘

🫢 the -load / :load moment

this one is a whole vibe, so let's look at it slow:

use Catmandu -load;
use Catmandu -load => [];
# is the same as
Catmandu->load;

use Catmandu -load => ['/config/path'];
# is the same as
Catmandu->load('/config/path');

so like β€” bare use Catmandu -load; and use Catmandu -load => []; both do the exact same thing as calling Catmandu->load; yourself. and if you pass an arrayref of paths, use Catmandu -load => ['/config/path']; is exactly Catmandu->load('/config/path');. same, same, no difference, fr fr πŸ’–

and that's it bestie, that's the whole page! go forth and export things responsibly πŸ’…βœ¨

and that's a wrap on this icon of a manual page πŸŽ€ it's from perl v5.40.1, dated 2026-01-24, and it lives in the Catman...PerlAPI(3pm) files β€” as documented by the Perl documentation crew, no cap 🌸🫢

πŸ“¦ where this page comes from