| 1 | # -*- perl -*- | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # $Id: Glossary.pm,v 1.5 2004/02/23 07:27:49 eserte Exp $ | ||||||||||||||||||||||||||||
| 5 | # Author: Slaven Rezic | ||||||||||||||||||||||||||||
| 6 | # | ||||||||||||||||||||||||||||
| 7 | # Copyright (C) 2002 Slaven Rezic. All rights reserved. | ||||||||||||||||||||||||||||
| 8 | # | ||||||||||||||||||||||||||||
| 9 | # Mail: slaven@rezic.de | ||||||||||||||||||||||||||||
| 10 | # WWW: http://rezic.de/eserte | ||||||||||||||||||||||||||||
| 11 | # | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | package WE::DB::Glossary; | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 2 | ▶ | use base qw(Class::Accessor); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 2 | __PACKAGE__->mk_accessors(qw(DB Root)); | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 2 | ▶ | use vars qw($VERSION); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 2 | $VERSION = sprintf("%d.%02d", q$Revision: 1.5 $ =~ /(\d+)\.(\d+)/); | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 2 | ▶ | use MLDBM; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 2 | ▶ | use Fcntl; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | { | ||||||||||||||||||||||||||||
| 26 | package WE::GlossaryObj; | ||||||||||||||||||||||||||||
| 27 | 2 | ▶ | use base qw(Class::Accessor); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 2 | __PACKAGE__->mk_accessors | |||||||||||||||||||||||||||
| 29 | (qw(Keyword Description)); | ||||||||||||||||||||||||||||
| 30 | 0 | ▶ | sub new { bless {}, $_[0] } | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | { | ||||||||||||||||||||||||||||
| 34 | # this will be written to the database and should not be used otherwise | ||||||||||||||||||||||||||||
| 35 | 2 | package WE::DB::Glossary::DBInfo; | |||||||||||||||||||||||||||
| 36 | 2 | ▶ | use base qw(Class::Accessor); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | 2 | __PACKAGE__->mk_accessors(qw()); | |||||||||||||||||||||||||||
| 38 | 0 | ▶ | sub new { bless {}, $_[0] } | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 2 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=16 SCAR=44.2
Pod
documented
| |||||||||||||||||||||||||||||
| 42 | 1 | my($class, $root, $file, %args) = @_; | |||||||||||||||||||||||||||
| 43 | 1 | my $self = {}; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 1 | ▶ | $args{-db} = "DB_File" unless defined $args{-db}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 1 | ▶ | $args{-serializer} = "Data::Dumper" unless defined $args{-serializer}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 1 | ▶ | $args{-locking} = 0 unless defined $args{-locking}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 1 | ▶ | $args{-readonly} = 0 unless defined $args{-readonly}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 1 | ▶ | $args{-writeonly} = 0 unless defined $args{-writeonly}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 1 | my @tie_args; | |||||||||||||||||||||||||||
| 52 | 1 | ▶ | if ($args{-readonly}) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 53 | 0 | push @tie_args, O_RDONLY; | |||||||||||||||||||||||||||
| 54 | } elsif ($args{-writeonly}) { | ||||||||||||||||||||||||||||
| 55 | 0 | push @tie_args, O_RDWR; | |||||||||||||||||||||||||||
| 56 | } else { | ||||||||||||||||||||||||||||
| 57 | 1 | push @tie_args, O_RDWR|O_CREAT; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 1 | ▶ | push @tie_args, $args{-db} eq 'Tie::TextDir' ? 0770 : 0660; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 1 | ▶ | if ($args{-db} eq 'DB_File') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 1 | require DB_File; | |||||||||||||||||||||||||||
| 64 | 0 | push @tie_args, $DB_File::DB_BTREE; | |||||||||||||||||||||||||||
| 65 | 0 | ▶ | if ($args{-locking}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 0 | $MLDBM::UseDB = 'DB_File::Lock'; | |||||||||||||||||||||||||||
| 67 | 0 | ▶ | push @tie_args, $args{-readonly} ? "read" : "write"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | } else { | ||||||||||||||||||||||||||||
| 69 | 0 | $MLDBM::UseDB = 'DB_File'; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | } else { | ||||||||||||||||||||||||||||
| 72 | 0 | $MLDBM::UseDB = $args{-db}; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 0 | $MLDBM::Serializer = $args{-serializer}; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 0 | ▶ | tie %{ $self->{DB} }, 'MLDBM', $file, @tie_args | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | or require Carp, Carp::confess("Can't tie MLDBM database $file with args <@tie_args>, db <$MLDBM::UseDB> and serializer <$MLDBM::Serializer>: $!"); | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | bless $self, $class; | |||||||||||||||||||||||||||
| 81 | 0 | $self->Root($root); | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | # read database information | ||||||||||||||||||||||||||||
| 84 | 0 | my $db_info = $self->DB->{__DBINFO__}; | |||||||||||||||||||||||||||
| 85 | 0 | ▶ | if (!defined $db_info) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 0 | $db_info = $self->DB->{__DBINFO__} = new WE::DB::Glossary::DBInfo; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | # sync members with DBINFO | ||||||||||||||||||||||||||||
| 89 | 0 | ▶ | if ($db_info) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | # none yet | ||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | # set %args | ||||||||||||||||||||||||||||
| 93 | # none yet | ||||||||||||||||||||||||||||
| 94 | # write back database information | ||||||||||||||||||||||||||||
| 95 | 0 | ▶ | if (!$args{-readonly}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 0 | $self->DB->{__DBINFO__} = $db_info; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 0 | $self; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 0 | ▶ | sub add_entry { | ||||||||||||||||||||||||||
|
Subroutine
add_entry: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||
| 103 | 0 | my($self, @args) = @_; | |||||||||||||||||||||||||||
| 104 | 0 | my %args; | |||||||||||||||||||||||||||
| 105 | 0 | ▶ | for(my $i=0; $i<=$#args; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 0 | ▶ | if ($args[$i] =~ /^-/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 0 | $args{$args[$i]} = $args[$i+1]; | |||||||||||||||||||||||||||
| 108 | 0 | splice @args, $i, 2; | |||||||||||||||||||||||||||
| 109 | 0 | $i--; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 0 | my $obj; | |||||||||||||||||||||||||||
| 114 | 0 | ▶ | if (UNIVERSAL::isa($args[0], "WE::GlossaryObj")) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 0 | $obj = $args[0]; | |||||||||||||||||||||||||||
| 116 | } else { | ||||||||||||||||||||||||||||
| 117 | 0 | my %obj_args = @args; | |||||||||||||||||||||||||||
| 118 | 0 | $obj = WE::GlossaryObj->new; | |||||||||||||||||||||||||||
| 119 | 0 | $obj->$_($obj_args{$_}) for (qw(Keyword Description)); | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 0 | ▶ | if (exists $self->{DB}->{$obj->Keyword}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 0 | ▶ | if (!$args{-force}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 0 | require Carp, Carp::confess("There is already a glossary entry for keyword " . $obj->Keyword); | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 0 | $self->{DB}->{$obj->Keyword} = $obj; | |||||||||||||||||||||||||||
| 129 | 0 | $obj; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 0 | ▶ | sub delete_entry { | ||||||||||||||||||||||||||
|
Subroutine
delete_entry: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 133 | 0 | my($self, $keyword) = @_; | |||||||||||||||||||||||||||
| 134 | 0 | delete $self->{DB}->{$keyword}; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 0 | ▶ | sub get_entry { | ||||||||||||||||||||||||||
|
Subroutine
get_entry: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 138 | 0 | my($self, $keyword) = @_; | |||||||||||||||||||||||||||
| 139 | 0 | $self->{DB}->{$keyword}; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 0 | ▶ | sub get_descr { | ||||||||||||||||||||||||||
|
Subroutine
get_descr: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 143 | 0 | my($self, $keyword) = @_; | |||||||||||||||||||||||||||
| 144 | 0 | my $obj = $self->get_entry($keyword); | |||||||||||||||||||||||||||
| 145 | 0 | ▶ | return undef if !$obj; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | 0 | $obj->Description; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 0 | ▶ | sub all_keywords_regex { | ||||||||||||||||||||||||||
|
Subroutine
all_keywords_regex: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 150 | 0 | my($self, $filter) = @_; | |||||||||||||||||||||||||||
| 151 | 0 | my @keywords; | |||||||||||||||||||||||||||
| 152 | 0 | ▶ | while(my($k,$v) = each %{ $self->{DB} }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | 0 | ▶ | next if $k =~ /^__/; # skip special keys | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 0 | ▶ | next if ($filter && !$filter->($k)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$filter and not &$filter($k)
MC/DC0%0/2
$filter and not &$filter($k)
$filter
not &$filter($k)
Truth table0%0/3
A and not(B)
A= $filterB= not &$filter($k)
| |||||||||||||||||||||||||||||
| 155 | 0 | push @keywords, $k; | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | 0 | "(" . join("|", map { "\\b" . quotemeta($_) . "\\b" } @keywords) . ")"; | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 2 | 1; | |||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | __END__ | ||||||||||||||||||||||||||||