File: IndexList.pm

package info (click to toggle)
libspoon-perl 0.24-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 296 kB
  • sloc: perl: 2,640; makefile: 2
file content (39 lines) | stat: -rw-r--r-- 991 bytes parent folder | download | duplicates (3)
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
package Spoon::IndexList;
use Spiffy -selfless;
use IO::All;
use DB_File;

sub index_list {
    my $list = io(shift);
    my $index = io($list . '.db')->dbm('DB_File')->rdonly;
    unless ($index->exists) {
        $index->assert->open;
        $index->close;
    }
    unless ($list->exists) {
        my $mtime = $index->mtime;
        $list->print('');
        for (sort keys %$index) {
            $list->print("$_\n");
        }
        $index->close;
        $list->close;
        $list->utime($mtime - 1);
    }
    if ($list->mtime > $index->mtime) {
        my %copy = %$index;
        $index->close;
        $index->rdonly(0)->rdwr(1)->open;
        for my $key ($list->chomp->slurp) {
            $key =~ s/^\s*(.*?)\s*$/$1/;
            next unless $key;
            $index->{$key} = 1;
            delete $copy{$key};
        }
        for my $key (keys %copy) {
            delete $index->{$key};
        }
        $index->rdonly(1)->rdwr(0)->close;
    }
    return $index;
}