App-Easer
view release on metacpan or search on metacpan
docs/docs/15-tutorial-splitting.md view on Meta::CPAN
---
title: 'Tutorial: splitting onto multiple modules'
layout: default
---
# Tutorial: splitting onto multiple modules
In the first [Tutorial: a to-do application][to-do-app] we took a look
at a basic example of a program with a nice, flat hierarchy whose
commands could benefit from a rather simple implementation (being mostly
centered around moving files).
Alas, life is not always that easy, and in many cases we might end up in
the need to cope with more complicated stuff. In this cases, a good
strategy is to break the problem down and try to address the different
parts one by one.
This breaking, sometimes, is meant *almost* phisically. I mean, it's
good to *break* the code into multiple files, so that we can concentrate
on one or another aspect at any time.
Depending on the style of implementation, this might be done by setting
up small execution callbacks towards more generic libraries, or calling
stuff in those libraries directly. At the end of the day, it's a matter
of size and preferences.
For this reason, in this tutorial we will try to break the `tudu`
application down in many different sub-modules, possibly in anticipation
that, one day, they will grow independently big.
## The main program
[App::Easer][] allows us to keep the very minimum inside the main
program, and spread most of the things over to modules. This includes
code as well as data.
This will be our main program:
```perl
#!/usr/bin/env perl
use v5.24;
use warnings;
use experimental 'signatures';
no warnings 'experimental::signatures';
use Path::Tiny 'path';
use App::Easer 'run';
my $application = {
factory => {prefixes => {'^' => 'MuDu::Command::'}},
configuration => {
name => 'mudu',
specfetch => '+SpecFromHashOrModule',
},
commands => {
MAIN => {
help => 'to-do application',
description => 'A simple to-do application, spread on files',
sources => '+SourcesWithFiles',
'config-files' => ["$ENV{HOME}/.tudu.conf", '/etc/tudu.conf'],
options => [
{
help => 'path to the configuration file',
getopt => 'config|c=s',
environment => 1,
},
{
help => 'base directory where tasks are kept',
getopt => 'basedir|dir|d=s',
environment => 1,
default => "$ENV{HOME}/.tudu",
},
{
help => 'max number of attempts to find non-colliding id',
getopt => 'attempts|max-attempts|M=i',
default => 9,
},
],
commit => \&ensure_basedir,
children => [qw<
^List
^Show
( run in 0.569 second using v1.01-cache-2.11-cpan-39bf76dae61 )