Capstone: todo CLI
What the tool does
The first capstone is a todo list in the terminal. It is small enough to finish in an evening and it uses almost every earlier lesson: options, files, JSON, references, modules and tests.
todo/
├── todo
├── lib/Todo/Store.pm
└── t/store.t
The command set stays tiny on purpose. Four verbs, one file of data, and no database server to install:
./todo add "Write lesson 46"
added #1
./todo list
[ ] 1 Write lesson 46
The data lives in $HOME/.todo.json, so the tool works from any folder. --file overrides the path, which is what the tests will use.
Storage in a module
Keep reading and writing in one module, so the command code never touches a file handle. The store knows the JSON format; everything else works with plain array references.
package Todo::Store; use strict; use warnings; use JSON::PP; use Exporter 'import'; our @EXPORT_OK = qw(load_todos save_todos); sub load_todos { my ($path) = @_; return [] unless -e $path; open my $fh, "<:raw", $path or die "cannot read $path: $!\n"; my $text = do { local $/; <$fh> }; return JSON::PP->new->utf8->decode($text); } sub save_todos { my ($path, $todos) = @_; my $json = JSON::PP->new->utf8->canonical->pretty; my $tmp = "$path.tmp"; open my $fh, ">:raw", $tmp or die "cannot write $tmp: $!\n"; print $fh $json->encode($todos); close $fh or die "cannot close $tmp: $!\n"; rename $tmp, $path or die "cannot replace $path: $!\n"; } 1;
Writing to a temporary file and renaming it means a crash cannot leave a half-written list behind. The rename is atomic on the same filesystem, which is the one property that matters here.
Commands and dispatch
The script reads global options, then takes the first remaining word as a command and looks it up in a hash of subroutines. Adding a command later means adding one entry, not another branch in an if-chain.
#!/usr/bin/perl use strict; use warnings; use Getopt::Long; use List::Util 'max'; use FindBin; use lib "$FindBin::Bin/lib"; use Todo::Store qw(load_todos save_todos); my $path = "$ENV{HOME}/.todo.json"; GetOptions("file=s" => \$path) or die "usage: todo [--file PATH] COMMAND ...\n"; my %cmd = ( add => \&cmd_add, list => \&cmd_list, done => \&cmd_done, remove => \&cmd_remove, ); my $command = shift @ARGV // "list"; my $run = $cmd{$command} or die "unknown command: $command\n"; $run->(@ARGV);
Each command is short enough to read at a glance. Adding a todo assigns the next free number, list prints one aligned line per record, done flips the flag on one record, and remove filters a record out:
sub cmd_add { my @words = @_; die "usage: todo add TITLE\n" unless @words; my $todos = load_todos($path); my $id = 1 + max(0, map { $_->{id} } @$todos); push @$todos, { id => $id, title => join(" ", @words), done => 0, }; save_todos($path, $todos); print "added #$id\n"; } sub cmd_list { my $todos = load_todos($path); printf "[%s] %3d %s\n", $_->{done} ? "x" : " ", $_->{id}, $_->{title} for @$todos; } sub cmd_done { my ($id) = @_; die "usage: todo done ID\n" unless defined $id; my $todos = load_todos($path); my ($todo) = grep { $_->{id} == $id } @$todos; die "no todo #$id\n" unless $todo; $todo->{done} = 1; save_todos($path, $todos); } sub cmd_remove { my ($id) = @_; die "usage: todo remove ID\n" unless defined $id; my $todos = load_todos($path); my $found = grep { $_->{id} == $id } @$todos; die "no todo #$id\n" unless $found; @$todos = grep { $_->{id} != $id } @$todos; save_todos($path, $todos); print "removed #$id\n"; }
Marking a record done prints nothing, so ask for the list again to see the flag change:
./todo done 1
./todo list
[x] 1 Write lesson 46
Tests for the store
The store is the part that can destroy data, so it gets the tests. A temp directory keeps them away from your real list, and is_deeply compares the structure that comes back.
use strict; use warnings; use Test::More; use File::Temp 'tempdir'; use lib "lib"; use Todo::Store qw(load_todos save_todos); my $dir = tempdir(CLEANUP => 1); my $path = "$dir/todo.json"; is_deeply(load_todos($path), [], "missing file is an empty list"); my $todos = [{ id => 1, title => "write tests", done => 0 }]; save_todos($path, $todos); is_deeply(load_todos($path), $todos, "round trip keeps the data"); ok(-e $path, "file exists after save"); ok(!-e "$path.tmp", "temporary file is gone"); done_testing();
prove -l t/
t/store.t .. ok
All tests successful.
Files=1, Tests=4, 0 wallclock secs
Result: PASS
The wallclock time and the CPU breakdown that prove adds to the Files= line vary by machine and by run; the result above them does not.
Where to take it
The tool is deliberately unfinished. Each of these additions is a lesson in disguise, and none of them requires a rewrite:
- Due dates with Time::Piece, and a --overdue filter.
- Priorities and a sort by priority, using the techniques from lesson 5.6.
- Search with grep { $_->{title} =~ /\Q$term\E/i }, the filter from the grep lesson.
- A SQLite backend: keep the same four commands and swap the store for DBI.
Keep the module boundary intact while you extend it. When the store grows a second implementation, the tests stay the same and prove both behave alike.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Why does
save_todoswrite to a temporary file and then rename it?Answer
A crash cannot leave a half-written list, because the rename is atomic
The old file stays whole until the new one is complete, and the rename swaps them in one step.
-
Why does the script look the command up in a hash of subroutines?
Answer
A new command is one more entry, not another branch in an if-chain
The dispatch table keeps the command code separate from the parsing, and one line adds a verb.
-
Why does the tool take
--fileas well as$HOME/.todo.json?Answer
The tests need a path in a temporary folder
A test that wrote to the real list would destroy the user data, so the path is an option with a sensible default.
-
What does
prove -l t/do?Answer
It adds lib to the search path and runs the tests
The flag puts lib in front of @INC, so the test loads the module under test without an install.
-
Which part of the capstone project gets the tests first?
Answer
The store, because it is the part that can destroy data
Reading and writing the file is where a bug loses work, and the tests use a temporary folder for that reason.
-
What does this program print?
use List::Util 'max'; my @todos = ({ id => 1 }, { id => 4 }); my $id = 1 + max(0, map { $_->{id} } @todos); print $id, "\n";Answer
5
The largest id is 4, and the next free number is one more than it.
-
What does this program print?
my $todo = { id => 3, title => "Review lesson 45", done => 1 }; printf "[%s] %3d %s\n", $todo->{done} ? "x" : " ", $todo->{id}, $todo->{title};Answer
[x] 3 Review lesson 45
The done flag becomes x, the id is printed in three columns, and the title follows two spaces later.
-
What does this program print?
my @todos = ({ id => 1 }, { id => 2 }, { id => 3 }); my $id = 2; my $found = grep { $_->{id} == $id } @todos; @todos = grep { $_->{id} != $id } @todos; print scalar(@todos), " $found\n";Answer
2 1
One record is removed, so two remain, and the scalar grep found exactly one match before that.
-
What does this program print?
my %cmd = ( add => sub { "added" }, list => sub { "listed" }, ); my $command = "list"; my $run = $cmd{$command} or die "unknown command\n"; print $run->(), "\n";Answer
listed
The hash gives the code reference for list, and the call returns the string that sub prints.
-
What does this program print?
use JSON::PP; my $todos = [{ id => 1, title => "write tests", done => 0 }]; my $json = JSON::PP->new->utf8->canonical->pretty; my $back = JSON::PP->new->utf8->decode($json->encode($todos)); print $back->[0]{title}, "\n";Answer
write tests
The decode rebuilds the same structure, and the first record keeps its title.
Article author: Arthur Isaev