| 1 | |||||||||||||||||||||
| 2 | package Text::Shoebox::Lexicon; | ||||||||||||||||||||
| 3 | 3 | require 5; | |||||||||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 3 | ▶ | use vars qw(@ISA $Debug $VERSION $ENTRY_CLASS); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 3 | ▶ | use Carp (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 3 | ▶ | $Debug = 0 unless defined $Debug; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 9 | 3 | ▶ | BEGIN { | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 3 | $VERSION = "1.02"; | |||||||||||||||||||
| 11 | } | ||||||||||||||||||||
| 12 | 3 | ▶ | $ENTRY_CLASS ||= 'Text::Shoebox::Entry'; | ||||||||||||||||||
|
Condition50%1/2
$Text::Shoebox::Lexicon::ENTRY_CLASS ||= "Text::Shoebox::Entry"
MC/DC0%0/1
$Text::Shoebox::Lexicon::ENTRY_CLASS ||= "Text::Shoebox::Entry"
$Text::Shoebox::Lexicon::ENTRY_CLASS
Truth table50%1/2
A
A= $Text::Shoebox::Lexicon::ENTRY_CLASS
| |||||||||||||||||||||
| 13 | 3 | ▶ | use Text::Shoebox 1.02 (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 3 | ▶ | unless($Text::Shoebox::Entry::VERSION) { require Text::Shoebox::Entry; } | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | ########################################################################### | ||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | =head1 NAME | ||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | Text::Shoebox::Lexicon - an object-oriented interface to Shoebox lexicons | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | =head1 SYNOPSIS | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | use Text::Shoebox::Lexicon; | ||||||||||||||||||||
| 26 | my $lex = Text::Shoebox::Lexicon->read_file( "haida.sf" ); | ||||||||||||||||||||
| 27 | my @entries = $lex->entries; | ||||||||||||||||||||
| 28 | print "See, it has ", scalar( @entries ), " entries!\n"; | ||||||||||||||||||||
| 29 | $lex->dump; | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | =head1 DESCRIPTION | ||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | On object of class Text::Shoebox::Lexicon represents a SF-format lexicon. | ||||||||||||||||||||
| 34 | This mostly just means it's a container for a list of | ||||||||||||||||||||
| 35 | Text::Shoebox::Entry objects, which represent the entries in this lexicon. | ||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | This class (plus Text::Shoebox::Entry) exists basically to provide an OO | ||||||||||||||||||||
| 38 | interface around L<Text::Shoebox> -- but you're free to directly | ||||||||||||||||||||
| 39 | use Text::Shoebox instead if you prefer a functional interface. | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | =head1 METHODS | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | =over | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | =item $lex = Text::Shoebox::Lexicon->new; | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | This method returns a new Text::Shoebox Lexicon object, containing | ||||||||||||||||||||
| 48 | an empty list of entries. | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | =cut | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | ########################################################################### | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | 13 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||||||||
| 55 | 13 | ▶ | my $new = bless {}, ref($_[0]) || $_[0];; | ||||||||||||||||||
|
Condition33%1/3
ref $_[0] || $_[0]
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0]
$_[0]
Truth table33%1/3
A || B
A= ref $_[0]B= $_[0]
| |||||||||||||||||||||
| 56 | 13 | $new->init; | |||||||||||||||||||
| 57 | 13 | return $new; | |||||||||||||||||||
| 58 | } | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | 13 | ▶ | sub init { | ||||||||||||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 61 | 13 | my $self = shift; | |||||||||||||||||||
| 62 | 13 | $self->{'e'} = []; | |||||||||||||||||||
| 63 | } | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | #-------------------------------------------------------------------------- | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | =item $lex->read_file( $filespec ); | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | This reads entries from $filespec (e.g., "./whatever.sf") into $lex. | ||||||||||||||||||||
| 70 | If $filespec doesn't exist or isn't readable, then this dies. | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | =item $lex = Text::Shoebox::Lexicon->read_file( $filespec ); | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | This constructs a new lexicon object and reads entries from $filespec into | ||||||||||||||||||||
| 75 | it. I.e., it's basically a shortcut for: | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | $lex = Text::Shoebox::Lexicon->new; | ||||||||||||||||||||
| 78 | $lex->read_file($filespec); | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | =item $lex->read_handle( $filehandle ); | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | =item $lex = Text::Shoebox::Lexicon->read_handle( $filehandle ); | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | These work just like read_file except that the argument should be a | ||||||||||||||||||||
| 85 | filehandle instead of a filespec string. | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | =item $lex->write_file( $filespec ); | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | This writes the entries from $lex to the given filespec. If they can't | ||||||||||||||||||||
| 90 | be written, this dies. | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | =item $lex->write_handle( $filehandle ); | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | These work just like write_file except that the argument should be a | ||||||||||||||||||||
| 95 | filehandle instead of a filespec string. | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | =cut | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | 12 | ▶ | sub read_file { | ||||||||||||||||||
|
Subroutine
read_file: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 100 | 12 | my($self, $in) = @_; | |||||||||||||||||||
| 101 | 12 | ▶ | $self = $self->new unless ref $self; # tolerate being a class method | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 102 | Text::Shoebox::read_sf( 'from_file' => $in, 'into' => $self->{'e'}, | ||||||||||||||||||||
| 103 | 12 | ▶ | $self->{'rs'} ? ('rs' => $self->{'rs'}) : () | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 104 | ); | ||||||||||||||||||||
| 105 | 12 | $self->tidy_up; | |||||||||||||||||||
| 106 | 12 | return $self; | |||||||||||||||||||
| 107 | } | ||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | 0 | ▶ | sub read_handle { | ||||||||||||||||||
|
Subroutine
read_handle: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 110 | 0 | my($self, $in) = @_; | |||||||||||||||||||
| 111 | 0 | ▶ | $self = $self->new unless ref $self; # tolerate being a class method | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 112 | Text::Shoebox::read_sf( 'from_handle' => $in, 'into' => $self->{'e'}, | ||||||||||||||||||||
| 113 | 0 | ▶ | $self->{'rs'} ? ('rs' => $self->{'rs'}) : () | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 114 | ); | ||||||||||||||||||||
| 115 | 0 | $self->tidy_up; | |||||||||||||||||||
| 116 | 0 | return $self; | |||||||||||||||||||
| 117 | } | ||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | 6 | ▶ | sub write_file { | ||||||||||||||||||
|
Subroutine
write_file: called CC=4 SCAR=14.2
Pod
documented
| |||||||||||||||||||||
| 120 | 6 | my($self, $out) = @_; | |||||||||||||||||||
| 121 | 6 | ▶ | Carp::confess "write_file is an object method, not a class method" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 122 | unless ref $self; | ||||||||||||||||||||
| 123 | Text::Shoebox::write_sf( 'to_file' => $out, 'from' => $self->{'e'}, | ||||||||||||||||||||
| 124 | 6 | ▶ | $self->{'rs'} ? ('rs' => $self->{'rs'}) : () | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 125 | ) || Carp::confess "Couldn't write_file to $out: $!"; | ||||||||||||||||||||
| 126 | 6 | return $self; | |||||||||||||||||||
| 127 | } | ||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | 0 | ▶ | sub write_handle { | ||||||||||||||||||
|
Subroutine
write_handle: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||
| 130 | 0 | my($self, $out) = @_; | |||||||||||||||||||
| 131 | 0 | ▶ | Carp::confess "write_handle is an object method, not a class method" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 132 | unless ref $self; | ||||||||||||||||||||
| 133 | Text::Shoebox::write_sf( 'to_handle' => $out, 'from' => $self->{'e'}, | ||||||||||||||||||||
| 134 | 0 | ▶ | $self->{'rs'} ? ('rs' => $self->{'rs'}) : () | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 135 | ) || Carp::confess "Couldn't write_handle to $out: $!"; | ||||||||||||||||||||
| 136 | 0 | return $self; | |||||||||||||||||||
| 137 | } | ||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | #-------------------------------------------------------------------------- | ||||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | =item $lex->dump; | ||||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | This prints (not returns!) a dump of the contents of $lex. | ||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | =cut | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | 0 | ▶ | sub dump { | ||||||||||||||||||
|
Subroutine
dump: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 148 | 0 | my($self, $out) = @_; | |||||||||||||||||||
| 149 | 0 | ▶ | Carp::confess "dump is an object method, not a class method" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 150 | unless ref $self; | ||||||||||||||||||||
| 151 | 0 | print "Lexicon $self contains ", scalar @{ $self->{'e'} }, " entries:\n\n"; | |||||||||||||||||||
| 152 | 0 | foreach my $e ( @{ $self->{'e'} } ) { | |||||||||||||||||||
| 153 | 0 | $e->dump; | |||||||||||||||||||
| 154 | } | ||||||||||||||||||||
| 155 | 0 | return $self; | |||||||||||||||||||
| 156 | } | ||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | #-------------------------------------------------------------------------- | ||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | =item @them = $lex->entries; | ||||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | This returns a list of the entry objects in $lex. | ||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | =item $them = $lex->entries_as_lol; | ||||||||||||||||||||
| 166 | |||||||||||||||||||||
| 167 | This returns a reference to the array of entry objects in $lex. | ||||||||||||||||||||
| 168 | |||||||||||||||||||||
| 169 | This can be useful for doing things like C<push @$them, $newentry;>. | ||||||||||||||||||||
| 170 | |||||||||||||||||||||
| 171 | This is your only way of altering the entry-list in $lex, other | ||||||||||||||||||||
| 172 | than read_file and read_handle! | ||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | =cut | ||||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | 3 | ▶ | sub entries { | ||||||||||||||||||
|
Subroutine
entries: called CC=2 SCAR=9.2
Pod
documented
| |||||||||||||||||||||
| 178 | 3 | my $self = shift; | |||||||||||||||||||
| 179 | 3 | ▶ | return @{ $self->{'e'} } unless @_; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 180 | 0 | @{ $self->{'e'} } = @_ ; # otherwise, be a set method | |||||||||||||||||||
| 181 | 0 | return $self; | |||||||||||||||||||
| 182 | } | ||||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | 12 | ▶ | sub tidy_up { | ||||||||||||||||||
|
Subroutine
tidy_up: called CC=5 SCAR=16.5
Pod
undocumented
| |||||||||||||||||||||
| 185 | 12 | my $self = $_[0]; | |||||||||||||||||||
| 186 | 12 | ▶ | my $entry_class = $self->{'entry_class'} || $ENTRY_CLASS; | ||||||||||||||||||
|
Condition33%1/3
$self->{'entry_class'} || $Text::Shoebox::Lexicon::ENTRY_CLASS
MC/DC0%0/2
$self->{'entry_class'} || $Text::Shoebox::Lexicon::ENTRY_CLASS
$self->{'entry_class'}
$Text::Shoebox::Lexicon::ENTRY_CLASS
Truth table33%1/3
A || B
A= $self->{'entry_class'}B= $Text::Shoebox::Lexicon::ENTRY_CLASS
| |||||||||||||||||||||
| 187 | 12 | foreach my $e (@{ $self->{'e'} }) { | |||||||||||||||||||
| 188 | 24 | ▶ | if( ref($e) eq 'ARRAY' ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 189 | 24 | bless $e, $entry_class; | |||||||||||||||||||
| 190 | 24 | ▶ | $e->scrunch unless $self->{'no_scrunch'}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 191 | } | ||||||||||||||||||||
| 192 | } | ||||||||||||||||||||
| 193 | 12 | return $self; | |||||||||||||||||||
| 194 | } | ||||||||||||||||||||
| 195 | |||||||||||||||||||||
| 196 | 50 | ▶ | sub entries_as_lol { return $_[0]{'e'} } | ||||||||||||||||||
|
Subroutine
entries_as_lol: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 197 | |||||||||||||||||||||
| 198 | #-------------------------------------------------------------------------- | ||||||||||||||||||||
| 199 | # Dumb boilerplate accessors: | ||||||||||||||||||||
| 200 | |||||||||||||||||||||
| 201 | =back | ||||||||||||||||||||
| 202 | |||||||||||||||||||||
| 203 | =head2 Other Attributes | ||||||||||||||||||||
| 204 | |||||||||||||||||||||
| 205 | A lexicon object is mainly for just holding a list of entries. But besides | ||||||||||||||||||||
| 206 | that list, it also contains these attributes, which you usually don't have | ||||||||||||||||||||
| 207 | to know about: | ||||||||||||||||||||
| 208 | |||||||||||||||||||||
| 209 | =over | ||||||||||||||||||||
| 210 | |||||||||||||||||||||
| 211 | =item The "no_scrunch" attribute | ||||||||||||||||||||
| 212 | |||||||||||||||||||||
| 213 | Right after read_file (or read_handle) has finished reading entries, it | ||||||||||||||||||||
| 214 | goes over all of them and calls C<< $e->scrunch >> on each. (See | ||||||||||||||||||||
| 215 | L<Text::Shoebox::Entry> for an explanation of the scrunch method.) But | ||||||||||||||||||||
| 216 | you can override this by calling $lex->no_scrunch(1) to set the "no_scrunch" | ||||||||||||||||||||
| 217 | method to a true value. | ||||||||||||||||||||
| 218 | |||||||||||||||||||||
| 219 | (You can also explicitly turn this off with $lex->no_scrunch(0), or check | ||||||||||||||||||||
| 220 | it with $lex->no_scrunch().) | ||||||||||||||||||||
| 221 | |||||||||||||||||||||
| 222 | |||||||||||||||||||||
| 223 | =item The "rs" attribute | ||||||||||||||||||||
| 224 | |||||||||||||||||||||
| 225 | When Text::Shoebox::Lexicon reads or writes a lexicon, it normally | ||||||||||||||||||||
| 226 | lets L<Text::Shoebox> determine the right value for the newline string | ||||||||||||||||||||
| 227 | (also known as the "RS", even tho for SF format it's not a record | ||||||||||||||||||||
| 228 | separator at all), and that's usually the right thing. | ||||||||||||||||||||
| 229 | |||||||||||||||||||||
| 230 | But if that's not working right and you need to override that newline-guessing | ||||||||||||||||||||
| 231 | (notably, this might be necessary with | ||||||||||||||||||||
| 232 | read_handle, which isn't as good as guessing as read_file is), then you | ||||||||||||||||||||
| 233 | can set the lexicon's C<rs> attribute directly, with C<< | ||||||||||||||||||||
| 234 | $lex->rs("\cm\cj") >>. Or you can even force it to the system-default | ||||||||||||||||||||
| 235 | value with just C<< $lex->rs($/) >>. Or you can just check the | ||||||||||||||||||||
| 236 | value of the C<rs> attribute with just C<< $lex->rs() >>. | ||||||||||||||||||||
| 237 | |||||||||||||||||||||
| 238 | =back | ||||||||||||||||||||
| 239 | |||||||||||||||||||||
| 240 | =cut | ||||||||||||||||||||
| 241 | |||||||||||||||||||||
| 242 | 11 | ▶ | sub no_scrunch { | ||||||||||||||||||
|
Subroutine
no_scrunch: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||
| 243 | 11 | ▶ | return $_[0]{'no_scrunch'} if @_ == 1; # get | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 244 | 11 | $_[0]{'no_scrunch'} = $_[1]; # set... | |||||||||||||||||||
| 245 | 11 | return $_[0]; | |||||||||||||||||||
| 246 | } | ||||||||||||||||||||
| 247 | 11 | ▶ | sub rs { | ||||||||||||||||||
|
Subroutine
rs: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||
| 248 | 11 | ▶ | return $_[0]{'rs'} if @_ == 1; # get | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 249 | 11 | $_[0]{'rs'} = $_[1]; # set... | |||||||||||||||||||
| 250 | 11 | return $_[0]; | |||||||||||||||||||
| 251 | } | ||||||||||||||||||||
| 252 | #-------------------------------------------------------------------------- | ||||||||||||||||||||
| 253 | |||||||||||||||||||||
| 254 | 3 | 1; | |||||||||||||||||||
| 255 | __END__ | ||||||||||||||||||||