blob: a8df8734f5640a3c9f3f4b7840b94dda097ff445 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
#!/usr/bin/perl
use warnings;
use strict;
use feature qw{ say };
use Syntax::Construct qw{ // };
use Storable qw{ store retrieve };
sub help {
say STDERR "Usage: $0 help";
say STDERR " $0 create index doc1 doc2...";
say STDERR " $0 search index term";
}
sub create {
my ($index_file, @documents) = @_;
my %index;
for my $document (@documents) {
warn $document;
open my $in, '<', $document or do {
warn "$document: $!";
next
};
while (<$in>) {
push @{ $index{$1}{$document} }, $. while /(\w+)/g;
}
}
store(\%index, $index_file);
}
sub search {
my ($index_file, $term) = @_;
my %index = %{ retrieve($index_file) };
for my $document (keys %{ $index{$term} }) {
say "$document: ", join ' ', @{ $index{$term}{$document} };
}
}
sub unknown {
help();
die "Unknown action\n";
}
my $action = shift;
my %dispatch = (
help => \&help,
create => \&create,
search => \&search,
);
my $run = $dispatch{$action} // \&unknown;
$run->(@ARGV);
|