| 1 | |||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # Time-stamp: "2004-04-03 20:20:51 ADT" | ||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 4 | require 5; | |||||||||||||||||||||||||||||||||||||||||||||
| 4 | package Text::Shoebox; | ||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use integer; # we don't need noninteger math in here | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use Carp qw(carp croak); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use vars qw(@ISA @EXPORT $Debug $VERSION %p); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 4 | require Exporter; | |||||||||||||||||||||||||||||||||||||||||||||
| 10 | 4 | require UNIVERSAL; | |||||||||||||||||||||||||||||||||||||||||||||
| 11 | 4 | @ISA = qw(Exporter); | |||||||||||||||||||||||||||||||||||||||||||||
| 12 | 4 | @EXPORT = qw(read_sf write_sf are_hw_keys_uniform are_hw_values_unique); | |||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 4 | ▶ | $Debug = 0 unless defined $Debug; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 4 | $VERSION = "1.02"; | |||||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||||||
| 17 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||||||
| 19 | Text::Shoebox - read and write SIL Shoebox Standard Format (.sf) files | ||||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||||
| 21 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||||||
| 23 | use Text::Shoebox; | ||||||||||||||||||||||||||||||||||||||||||||||
| 24 | my $lex = []; | ||||||||||||||||||||||||||||||||||||||||||||||
| 25 | foreach my $file (@ARGV) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 26 | read_sf( | ||||||||||||||||||||||||||||||||||||||||||||||
| 27 | from_file => $file, into => $lex, | ||||||||||||||||||||||||||||||||||||||||||||||
| 28 | ) or warn "read from $file failed\n"; | ||||||||||||||||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 30 | print scalar(@$lex), " entries read.\n"; | ||||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||
| 32 | die "hw field-names differ\n" | ||||||||||||||||||||||||||||||||||||||||||||||
| 33 | unless are_hw_keys_uniform($lex); | ||||||||||||||||||||||||||||||||||||||||||||||
| 34 | warn "hw field-values aren't unique\n" | ||||||||||||||||||||||||||||||||||||||||||||||
| 35 | unless are_hw_values_unique($lex); | ||||||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||||||
| 37 | write_sf(from => $lex, to_file => "merged.sf") | ||||||||||||||||||||||||||||||||||||||||||||||
| 38 | or die "Couldn't write to merged.sf: $!"; | ||||||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||||||
| 40 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||||||
| 42 | The Summer Institute of Linguistics (C<http://www.sil.org/>) makes a | ||||||||||||||||||||||||||||||||||||||||||||||
| 43 | piece of free software called "the Linguist's Shoebox", or just | ||||||||||||||||||||||||||||||||||||||||||||||
| 44 | "Shoebox" for short. It's a simple database program generally used | ||||||||||||||||||||||||||||||||||||||||||||||
| 45 | for making lexicon databases (altho it can also be used for databases | ||||||||||||||||||||||||||||||||||||||||||||||
| 46 | of field notes, etc.). | ||||||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||||||
| 48 | Shoebox can export its databases to SF (Standard Format) files, a | ||||||||||||||||||||||||||||||||||||||||||||||
| 49 | simple text format. Reading and writing those SF files is what this | ||||||||||||||||||||||||||||||||||||||||||||||
| 50 | Perl module, Text::Shoebox, is for. (I have heard that Standard Format | ||||||||||||||||||||||||||||||||||||||||||||||
| 51 | predates Shoebox quite a bit, and is used by other programs. If you | ||||||||||||||||||||||||||||||||||||||||||||||
| 52 | use SF files with something other than Shoebox, I'd be interested in | ||||||||||||||||||||||||||||||||||||||||||||||
| 53 | hearing about it, particularly about whether such files and | ||||||||||||||||||||||||||||||||||||||||||||||
| 54 | Text::Shoebox are happily compatible.) | ||||||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||||||
| 56 | =head1 OBJECT-ORIENTED INTERFACE | ||||||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||||||
| 58 | This module provides a functional interface. If you want an | ||||||||||||||||||||||||||||||||||||||||||||||
| 59 | object-oriented interface, with a bit more convenience, then see | ||||||||||||||||||||||||||||||||||||||||||||||
| 60 | the classes L<Text::Shoebox::Lexicon> and L<Text::Shoebox::Entry>. | ||||||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||||||
| 63 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||||||
| 65 | =over | ||||||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||||||
| 67 | =item $lex_lol = read_sf(...options...) | ||||||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||||||
| 69 | Reads entries in Standard Format from the source specified. If no | ||||||||||||||||||||||||||||||||||||||||||||||
| 70 | entries were read, returns false. Otherwise, returns a reference to | ||||||||||||||||||||||||||||||||||||||||||||||
| 71 | the array that the entries were added to (which will be a new array, | ||||||||||||||||||||||||||||||||||||||||||||||
| 72 | unless the "into" option is set). If there's an I/O error while reading | ||||||||||||||||||||||||||||||||||||||||||||||
| 73 | (like if you specify an unreadable file), then this routine dies. | ||||||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||||||
| 75 | The options are: | ||||||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||||||
| 77 | =over | ||||||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||||||
| 79 | =item from_file => STRING | ||||||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||||||
| 81 | This specifies that the source of the SF data is a file, whose | ||||||||||||||||||||||||||||||||||||||||||||||
| 82 | filespec is given. | ||||||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||||||
| 84 | =item from_handle => FILEHANDLE | ||||||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||||||
| 86 | This specifies that the source of the SF data is a given filehandle. | ||||||||||||||||||||||||||||||||||||||||||||||
| 87 | (Examples of filehandle values: a global filehandle passed either | ||||||||||||||||||||||||||||||||||||||||||||||
| 88 | like C<*MYFH{IO}> or C<*MYFH>; or an object value from an IO class like | ||||||||||||||||||||||||||||||||||||||||||||||
| 89 | IO::Socket or IO::Handle.) | ||||||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||||||
| 91 | The filehandle isn't closed when all its data is read. | ||||||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||||||
| 93 | =item rs => STRING | ||||||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||||||
| 95 | This specifies that the given string should be used as the record | ||||||||||||||||||||||||||||||||||||||||||||||
| 96 | separator (newline string) for the data source being read from. | ||||||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||||||
| 98 | If the SF source is specified by a "from_file" option, and you don't | ||||||||||||||||||||||||||||||||||||||||||||||
| 99 | specify an "rs" option, then Text::Shoebox will try guessing the line | ||||||||||||||||||||||||||||||||||||||||||||||
| 100 | format of the file by reading the first 2K of the file and looking for | ||||||||||||||||||||||||||||||||||||||||||||||
| 101 | a CRLF ("\cm\cj"), an LF ("\cj"), or a CR ("\cm"). If you need to | ||||||||||||||||||||||||||||||||||||||||||||||
| 102 | stop it from trying to guess, just stipulate an "rs" value of C<$/>. | ||||||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||||||
| 104 | If the SF source is specified by a "from_handle" option, and you don't | ||||||||||||||||||||||||||||||||||||||||||||||
| 105 | specify an "rs" option, then Text::Shoebox will just use the value in | ||||||||||||||||||||||||||||||||||||||||||||||
| 106 | the Perl variable C<$/> (the global RS value). | ||||||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||||||
| 108 | =item into => ARRAYREF | ||||||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||||||
| 110 | If this option is stipulated, then entries read will be pushed to the | ||||||||||||||||||||||||||||||||||||||||||||||
| 111 | end of the array specified. Otherwise the entries will be put into a | ||||||||||||||||||||||||||||||||||||||||||||||
| 112 | new array. | ||||||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||||||
| 114 | =back | ||||||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||||||
| 116 | Example use: | ||||||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||||||
| 118 | use Text::Shoebox; | ||||||||||||||||||||||||||||||||||||||||||||||
| 119 | my $lexicon = read_sf(from_file => 'foo.sf') | ||||||||||||||||||||||||||||||||||||||||||||||
| 120 | || die "No entries?"; | ||||||||||||||||||||||||||||||||||||||||||||||
| 121 | print scalar(@$lexicon), " entries read.\n"; | ||||||||||||||||||||||||||||||||||||||||||||||
| 122 | print "First entry has ", | ||||||||||||||||||||||||||||||||||||||||||||||
| 123 | @{ $lexicon->[0] } / 2 , " fields.\n"; | ||||||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||||||
| 125 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 21 | ▶ | sub read_sf { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
read_sf: called CC=26 SCAR=36.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 21 | my(%options) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 21 | my($target); | |||||||||||||||||||||||||||||||||||||||||||||
| 131 | 21 | ▶ | if(exists $options{'into'} ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 12 | $target = $options{'into'}; | |||||||||||||||||||||||||||||||||||||||||||||
| 133 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 9 | $target = []; | |||||||||||||||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 21 | local $/ = $/; | |||||||||||||||||||||||||||||||||||||||||||||
| 138 | 21 | my($fh, $to_close); | |||||||||||||||||||||||||||||||||||||||||||||
| 139 | 21 | ▶ | if( exists $options{'from_handle'}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 0 | $fh = $options{'from_handle'}; | |||||||||||||||||||||||||||||||||||||||||||||
| 141 | 0 | ▶ | $/ = $options{'rs'} if exists $options{'rs'}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 142 | # otherwise use default $/ value | ||||||||||||||||||||||||||||||||||||||||||||||
| 143 | } elsif(exists $options{'from_file'}) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 21 | local *IN; | |||||||||||||||||||||||||||||||||||||||||||||
| 145 | 21 | my $from_file = $options{'from_file'}; | |||||||||||||||||||||||||||||||||||||||||||||
| 146 | 21 | ▶ | open(IN, "<$from_file") or croak "Can't read-open $from_file: $!"; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 21 | binmode(IN); | |||||||||||||||||||||||||||||||||||||||||||||
| 148 | 21 | $fh = *IN{IO}; | |||||||||||||||||||||||||||||||||||||||||||||
| 149 | 21 | $to_close = 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 21 | ▶ | if(exists $options{'rs'}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 11 | $/ = $options{'rs'}; | |||||||||||||||||||||||||||||||||||||||||||||
| 153 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 10 | my $chunk; | |||||||||||||||||||||||||||||||||||||||||||||
| 155 | 10 | read($fh, $chunk, 2048); # should be plenty long enough! | |||||||||||||||||||||||||||||||||||||||||||||
| 156 | 10 | seek($fh, 0,0); # rewind | |||||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||||||
| 158 | # All the NL formats I know about... | ||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 10 | ▶ | if(defined $chunk and $chunk =~ m<(\cm\cj|\cm|\cj)>s) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $chunk and $chunk =~ /(\cm\cj|\cm|\cj)/s
MC/DC0%0/2
defined $chunk and $chunk =~ /(\cm\cj|\cm|\cj)/s
defined $chunk
$chunk =~ /(\cm\cj|\cm|\cj)/s
Truth table33%1/3
A and B
A= defined $chunkB= $chunk =~ /(\cm\cj|\cm|\cj)/s
| |||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 10 | $/ = $1; | |||||||||||||||||||||||||||||||||||||||||||||
| 161 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 0 | ▶ | print "Couldn't set \$/ for some reason.\n" if $Debug; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 163 | # Otherwise let it stand. | ||||||||||||||||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 166 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 0 | croak "read_sf needs an option specifying input source"; | |||||||||||||||||||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 21 | ▶ | print "\$/ is ", unpack("H*", $/), "\n" if $Debug; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||||||
| 172 | #my $lines_so_far = 0; | ||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 21 | my $line; # current line | |||||||||||||||||||||||||||||||||||||||||||||
| 174 | my $hw_field; # set from the first real field name we see | ||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 21 | my @new_entries; # to fill up with new things from this file | |||||||||||||||||||||||||||||||||||||||||||||
| 176 | 21 | my $Debug = $Debug; # lexical for speed | |||||||||||||||||||||||||||||||||||||||||||||
| 177 | 21 | my $last_field_was_comment = 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 21 | ▶ | while(defined($line = <$fh>)) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 180 | 210 | chomp($line); | |||||||||||||||||||||||||||||||||||||||||||||
| 181 | #next if !defined($hw_field) and | ||||||||||||||||||||||||||||||||||||||||||||||
| 182 | ## ++$lines_so_far == 1 and | ||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 210 | ▶ | if(length $line > 1 and substr($line,0,2) eq '\_') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
length $line > 1 and substr($line, 0, 2) eq "\\_"
MC/DC0%0/2
length $line > 1 and substr($line, 0, 2) eq "\\_"
length $line > 1
substr($line, 0, 2) eq "\\_"
Truth table66%2/3
A and B
A= length $line > 1B= substr($line, 0, 2) eq "\\_"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 184 | 0 | $last_field_was_comment = 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 185 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||||||
| 188 | 210 | ▶ | if($line =~ m<^\\(\S+) ?(.*)>s) { # It's a normal "\foo val" line... | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 189 | # This is the typical line in typical .sf files | ||||||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||||||
| 191 | # That RE matches either "\foo" or "\foo bar..." | ||||||||||||||||||||||||||||||||||||||||||||||
| 192 | # (Because the \S+ stops either at end-of-string, or at ' '. | ||||||||||||||||||||||||||||||||||||||||||||||
| 193 | # Note that in either case, $2 is never undef. | ||||||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||||||
| 195 | 105 | ▶ | print "<$line> parses as <$1> = <$2>\n" if $Debug > 1; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 196 | 105 | $last_field_was_comment = 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 197 | 105 | ▶ | if(@new_entries) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 198 | 84 | ▶ | if($1 eq $hw_field) { # it's a non-initial new entry | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 199 | # A new entry means that the preceding entry's last value got | ||||||||||||||||||||||||||||||||||||||||||||||
| 200 | # one too many \n's at the end. So chop it. | ||||||||||||||||||||||||||||||||||||||||||||||
| 201 | # (Assumes "\n" is a single byte long; safe, I hope.) | ||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 21 | ▶ | chop($new_entries[-1][-1]) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 203 | if substr($new_entries[-1][-1], -1, 1) eq "\n"; | ||||||||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||||||
| 205 | # Start off a new entry | ||||||||||||||||||||||||||||||||||||||||||||||
| 206 | 21 | push @new_entries, [$1, $2]; | |||||||||||||||||||||||||||||||||||||||||||||
| 207 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 208 | 63 | push @{$new_entries[-1]}, $1, $2; | |||||||||||||||||||||||||||||||||||||||||||||
| 209 | # This is all that happens to typical lines: | ||||||||||||||||||||||||||||||||||||||||||||||
| 210 | # Just tack more items to the end of the last entry. | ||||||||||||||||||||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 212 | } else { # No entries seen yet | ||||||||||||||||||||||||||||||||||||||||||||||
| 213 | 21 | $hw_field = $1; | |||||||||||||||||||||||||||||||||||||||||||||
| 214 | # First field ever seen (ignoring _sh). | ||||||||||||||||||||||||||||||||||||||||||||||
| 215 | # That must be the headword field! Note it as such. | ||||||||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||||||
| 217 | # Now start off a new entry (the first, it so happens) | ||||||||||||||||||||||||||||||||||||||||||||||
| 218 | 21 | push @new_entries, [$1, $2]; | |||||||||||||||||||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||||||||||||
| 221 | } else { # It's a continuation line... | ||||||||||||||||||||||||||||||||||||||||||||||
| 222 | 105 | ▶ | next if $last_field_was_comment; # just toss this. | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 105 | ▶ | print "<$line> is a continuation line.\n" if $Debug > 1; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 225 | 105 | ▶ | if(@new_entries) { # expected case! | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 226 | 105 | $line =~ s<^ \\><\\>s; | |||||||||||||||||||||||||||||||||||||||||||||
| 227 | # Continuations starting with '\' get a leading space put on | ||||||||||||||||||||||||||||||||||||||||||||||
| 228 | # the front them -- so take it off. (Even tho it could have | ||||||||||||||||||||||||||||||||||||||||||||||
| 229 | # originated as a real ' \'.) | ||||||||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||||||||
| 231 | 105 | $new_entries[-1][-1] .= "\n" . $line; | |||||||||||||||||||||||||||||||||||||||||||||
| 232 | # So, yes, newline in the file ($/) turns into "\n". | ||||||||||||||||||||||||||||||||||||||||||||||
| 233 | # Tack this line onto the end of the last value in the last new entry | ||||||||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||||||
| 235 | } else { # most unexpected -- continuation of nothing! | ||||||||||||||||||||||||||||||||||||||||||||||
| 236 | 0 | ▶ | warn "line \"$line\" is a continuation, but of what?" | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 237 | if $line =~ m<\S>s; | ||||||||||||||||||||||||||||||||||||||||||||||
| 238 | # (but forgive such things if they're pure whitespace) | ||||||||||||||||||||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 240 | } # end of continuation line | ||||||||||||||||||||||||||||||||||||||||||||||
| 241 | } # end while() over the lines | ||||||||||||||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||||||||||||||
| 243 | 21 | ▶ | close($fh) if $to_close; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||||||||||||||||
| 245 | 21 | ▶ | print "All read: ", scalar(@new_entries), " entries read.\n" if $Debug; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||||||
| 247 | 21 | ▶ | return 0 unless @new_entries; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||||||||||||
| 249 | 21 | push @$target, @new_entries; | |||||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||||||
| 251 | 21 | return $target; | |||||||||||||||||||||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||||||
| 254 | #-------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||||||||||||
| 256 | =item write_sf(...options...) | ||||||||||||||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||||||||||||||
| 258 | This writes the given lexicon, in Standard Format, to the destination | ||||||||||||||||||||||||||||||||||||||||||||||
| 259 | specified. If all entries were written, returns true; otherwise (in | ||||||||||||||||||||||||||||||||||||||||||||||
| 260 | case of an IO error), returns false, in which case you should | ||||||||||||||||||||||||||||||||||||||||||||||
| 261 | check C<$!>. Note that this routine I<doesn't> die in the case of | ||||||||||||||||||||||||||||||||||||||||||||||
| 262 | an I/O error, so you should always check the return value of this | ||||||||||||||||||||||||||||||||||||||||||||||
| 263 | function, as with: | ||||||||||||||||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||||||||||||||||
| 265 | write_sf(...) || die "Couldn't write: $!"; | ||||||||||||||||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||||||||||||||||
| 267 | The options are: | ||||||||||||||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||||||||||||||
| 269 | =over | ||||||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||||||
| 271 | =item from => ARRAYREF | ||||||||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||||||
| 273 | This option must be present, to specify the lexicon that you want to | ||||||||||||||||||||||||||||||||||||||||||||||
| 274 | write out. | ||||||||||||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||||||||||||
| 276 | =item to_file => STRING | ||||||||||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||||||||||
| 278 | This specifies that the SF data is to be written to the file | ||||||||||||||||||||||||||||||||||||||||||||||
| 279 | specified. (Note that the file is opened in overwrite mode, not | ||||||||||||||||||||||||||||||||||||||||||||||
| 280 | append mode.) | ||||||||||||||||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||||||||||||||||
| 282 | =item to_handle => FILEHANDLE | ||||||||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||||||
| 284 | This specifies that the destination for the SF data is the given | ||||||||||||||||||||||||||||||||||||||||||||||
| 285 | filehandle. | ||||||||||||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||||||||||||
| 287 | The filehandle isn't closed when all the data is written to it. | ||||||||||||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||||||||||||
| 289 | =item rs => STRING | ||||||||||||||||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||||||||||||||||
| 291 | This specifies that the given string should be used as the record | ||||||||||||||||||||||||||||||||||||||||||||||
| 292 | separator (newline string) for the SF data written. | ||||||||||||||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||||||||||||||
| 294 | If not specified, defaults to "\n". | ||||||||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||||||
| 296 | =back | ||||||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||||||
| 298 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||||||||||||||||
| 300 | 11 | ▶ | sub write_sf { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
write_sf: called CC=35 SCAR=45.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 301 | 11 | my(%options) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 302 | 11 | my $from; | |||||||||||||||||||||||||||||||||||||||||||||
| 303 | 11 | ▶ | if(exists $options{'from'}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 304 | 11 | $from = $options{'from'}; | |||||||||||||||||||||||||||||||||||||||||||||
| 305 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 306 | 0 | ▶ | croak("'from' should be a reference") | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $from and ref $from
MC/DC0%0/2
defined $from and ref $from
defined $from
ref $from
Truth table0%0/3
A and B
A= defined $fromB= ref $from
| |||||||||||||||||||||||||||||||||||||||||||||||
| 307 | unless defined $from and ref $from; | ||||||||||||||||||||||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||||||||||||||
| 310 | 11 | my($fh, $to_close); | |||||||||||||||||||||||||||||||||||||||||||||
| 311 | 11 | ▶ | if(exists $options{'to_handle'}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 312 | 0 | $fh = $options{'to_handle'}; | |||||||||||||||||||||||||||||||||||||||||||||
| 313 | 0 | ▶ | print "Writing to $fh from object $from\n" if $Debug; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 314 | } elsif(exists $options{'to_file'}) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 315 | # passed a filespec | ||||||||||||||||||||||||||||||||||||||||||||||
| 316 | 11 | local *OUT; | |||||||||||||||||||||||||||||||||||||||||||||
| 317 | 11 | my $dest = $options{'to_file'}; | |||||||||||||||||||||||||||||||||||||||||||||
| 318 | 11 | ▶ | print "Writing to $dest from object $from\n" if $Debug; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 319 | 11 | ▶ | open(OUT, ">$dest") or return 0; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 320 | 11 | $fh = *OUT{IO}; | |||||||||||||||||||||||||||||||||||||||||||||
| 321 | 11 | binmode($fh); | |||||||||||||||||||||||||||||||||||||||||||||
| 322 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 323 | 0 | croak "write_sf needs either a to_handle or a to_file option"; | |||||||||||||||||||||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||||||||||||||
| 326 | 11 | my $nl; | |||||||||||||||||||||||||||||||||||||||||||||
| 327 | 11 | ▶ | if(exists $options{'rs'}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 328 | 9 | $nl = $options{'rs'}; | |||||||||||||||||||||||||||||||||||||||||||||
| 329 | # Some sanity checks: | ||||||||||||||||||||||||||||||||||||||||||||||
| 330 | 9 | ▶ | croak "rs can't be undef" unless defined $nl; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 331 | 9 | ▶ | croak "rs can't be empty-string" unless length $nl; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 332 | 9 | ▶ | croak "rs can't be a reference" if ref $nl; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 333 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 334 | 2 | $nl = "\n"; | |||||||||||||||||||||||||||||||||||||||||||||
| 335 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||||||||||
| 337 | 11 | my $qnl = quotemeta $nl; | |||||||||||||||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||||||||||||||||
| 339 | 11 | my $nl_is_weird = 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 340 | 11 | ▶ | $nl_is_weird = 1 unless $nl =~ m<^[\cm\cj]+$>s; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||||||||||||||||
| 342 | 11 | my $am_first_entry = 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 343 | 11 | my($k, $v, $i, $i_entry, $e); # scratch vars | |||||||||||||||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||||||||||||||||
| 345 | Entry: | ||||||||||||||||||||||||||||||||||||||||||||||
| 346 | 11 | ▶ | for($i_entry = 0; $i_entry < @$from; ++$i_entry) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 347 | 22 | ▶ | unless(defined( | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
defined($e = $from->[$i_entry]) and ref $e eq "ARRAY" || UNIVERSAL::isa($e, "ARRAY")
ref $e eq "ARRAY" or UNIVERSAL::isa($e, "ARRAY")
MC/DC0%0/3
defined($e = $from->[$i_entry]) and ref $e eq "ARRAY" || UNIVERSAL::isa($e, "ARRAY")
defined($e = $from->[$i_entry])
ref $e eq "ARRAY"
UNIVERSAL::isa($e, "ARRAY")
Truth table50%2/4
A and B or C
A= defined($e = $from->[$i_entry])B= ref $e eq "ARRAY"C= UNIVERSAL::isa($e, "ARRAY")
| |||||||||||||||||||||||||||||||||||||||||||||||
| 348 | $e = $from->[$i_entry] # copy the entry ref | ||||||||||||||||||||||||||||||||||||||||||||||
| 349 | ) and ( | ||||||||||||||||||||||||||||||||||||||||||||||
| 350 | ref $e eq 'ARRAY' | ||||||||||||||||||||||||||||||||||||||||||||||
| 351 | or UNIVERSAL::isa($e, 'ARRAY') | ||||||||||||||||||||||||||||||||||||||||||||||
| 352 | ) | ||||||||||||||||||||||||||||||||||||||||||||||
| 353 | ) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 354 | 0 | ▶ | print "Skipping $e -- not an entry\n" if $Debug; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 355 | 0 | Carp::cluck "Skipping $e -- not an entry"; | |||||||||||||||||||||||||||||||||||||||||||||
| 356 | 0 | next Entry; | |||||||||||||||||||||||||||||||||||||||||||||
| 357 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 358 | 22 | ▶ | unless(@$e) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 359 | 0 | ▶ | print "Skipping $e -- a null entry\n" if $Debug; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 360 | 0 | next Entry; | |||||||||||||||||||||||||||||||||||||||||||||
| 361 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||||||||||||||||
| 363 | 22 | ▶ | if($am_first_entry) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 364 | 11 | $am_first_entry = undef; # do nothing but turn it off. | |||||||||||||||||||||||||||||||||||||||||||||
| 365 | } else { # print a NL before every entry except the first | ||||||||||||||||||||||||||||||||||||||||||||||
| 366 | 11 | ▶ | return 0 unless print $fh $nl; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 367 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||||||||||||||||||||
| 369 | Field: | ||||||||||||||||||||||||||||||||||||||||||||||
| 370 | 22 | ▶ | for($i = 0; $i < @$e; $i += 2) { # iterate across keys | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 371 | 55 | ▶ | unless(defined( | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined($k = $e->[$i]) and length $k
MC/DC0%0/2
defined($k = $e->[$i]) and length $k
defined($k = $e->[$i])
length $k
Truth table33%1/3
A and B
A= defined($k = $e->[$i])B= length $k
| |||||||||||||||||||||||||||||||||||||||||||||||
| 372 | $k = $e->[$i] # copy the key | ||||||||||||||||||||||||||||||||||||||||||||||
| 373 | ) and length $k | ||||||||||||||||||||||||||||||||||||||||||||||
| 374 | ) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 375 | 0 | next Field; | |||||||||||||||||||||||||||||||||||||||||||||
| 376 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||||||||||||||||
| 378 | 55 | ▶ | if($nl_is_weird) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 379 | 10 | $k =~ s<$qnl><>g; # basic attempt at sanity. | |||||||||||||||||||||||||||||||||||||||||||||
| 380 | 10 | $k =~ tr< ><>d; | |||||||||||||||||||||||||||||||||||||||||||||
| 381 | # Up to the user to keep [\cm\cj\t] out of the keys! | ||||||||||||||||||||||||||||||||||||||||||||||
| 382 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 383 | 45 | $k =~ tr<\cm\cj\t ><>d; # basic sanity for any sane NL value | |||||||||||||||||||||||||||||||||||||||||||||
| 384 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||||||||||||||||||||
| 386 | 55 | ▶ | unless(length $k) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 387 | 0 | ▶ | carp "Key field in lexicon->[ $i_entry ][ $i ] is null!\n" if $Debug; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 388 | 0 | next Field; | |||||||||||||||||||||||||||||||||||||||||||||
| 389 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||||||||||||||||
| 391 | 55 | ▶ | if(defined( | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 392 | $v = $e->[1 + $i] # copy value | ||||||||||||||||||||||||||||||||||||||||||||||
| 393 | )) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 394 | 55 | ▶ | if(length $v) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 395 | 44 | $v =~ s<\n\\><\n \\>g; | |||||||||||||||||||||||||||||||||||||||||||||
| 396 | 44 | ▶ | $v =~ s<\n><$nl>g if $nl ne "\n"; # swap NL | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 397 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 398 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 399 | 0 | $v = ''; | |||||||||||||||||||||||||||||||||||||||||||||
| 400 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||||||||||||||||||||
| 402 | 55 | ▶ | return 0 unless # return if there's an error in printing | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 403 | length($v) ? (print $fh "\\", $k, ' ', $v, $nl) # "\foo bar" + NL | ||||||||||||||||||||||||||||||||||||||||||||||
| 404 | : (print $fh "\\", $k, $nl) # "\foo" + NL | ||||||||||||||||||||||||||||||||||||||||||||||
| 405 | ; | ||||||||||||||||||||||||||||||||||||||||||||||
| 406 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 407 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 408 | 11 | ▶ | close($fh) if $to_close; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 409 | 11 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 410 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||||||||||||||||||||
| 412 | #-------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||||||||||||||||||||
| 414 | =item are_hw_keys_uniform($lol) | ||||||||||||||||||||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||||||||||||||||||||
| 416 | This function returns true iff all the entries in the lexicon have the | ||||||||||||||||||||||||||||||||||||||||||||||
| 417 | same key for their headword fields (i.e., the first field per record). | ||||||||||||||||||||||||||||||||||||||||||||||
| 418 | This will always be true if you read the lexicon from one file; but if | ||||||||||||||||||||||||||||||||||||||||||||||
| 419 | you read it from several, it's possible that the different files have | ||||||||||||||||||||||||||||||||||||||||||||||
| 420 | different keys marking headword fields. | ||||||||||||||||||||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||||||||||||||||||||
| 422 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||||||||||||||||||||
| 424 | 1 | ▶ | sub are_hw_keys_uniform { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
are_hw_keys_uniform: called CC=16 SCAR=38.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 425 | 1 | ▶ | carp('Wrong number of arguments to are_hw_keys_uniform'), return 0 | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 426 | unless @_ == 1; | ||||||||||||||||||||||||||||||||||||||||||||||
| 427 | 1 | my $lex = $_[0]; | |||||||||||||||||||||||||||||||||||||||||||||
| 428 | 1 | ▶ | $Debug && carp('Argument to are_hw_keys_uniform isn\'t a listref'), return 0 | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition16%1/6
defined $lex and ref $lex eq "ARRAY"
$Text::Shoebox::Debug and carp("Argument to are_hw_keys_uniform isn't a listref")
MC/DC0%0/2
defined $lex and ref $lex eq "ARRAY"
defined $lex
ref $lex eq "ARRAY"
MC/DC0%0/2
$Text::Shoebox::Debug and carp("Argument to are_hw_keys_uniform isn't a listref")
$Text::Shoebox::Debug
carp("Argument to are_hw_keys_uniform isn't a listref")
Truth table33%1/3
A and B
A= defined $lexB= ref $lex eq "ARRAY"
Truth table0%0/3
A and B
A= $Text::Shoebox::DebugB= carp("Argument to are_hw_keys_uniform isn't a listref")
| |||||||||||||||||||||||||||||||||||||||||||||||
| 429 | unless defined $lex and ref $lex eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||||||
| 430 | 1 | ▶ | $Debug && carp('Empty lexicon to are_hw_keys_uniform'), return 0 | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$Text::Shoebox::Debug and carp("Empty lexicon to are_hw_keys_uniform")
MC/DC0%0/2
$Text::Shoebox::Debug and carp("Empty lexicon to are_hw_keys_uniform")
$Text::Shoebox::Debug
carp("Empty lexicon to are_hw_keys_uniform")
Truth table0%0/3
A and B
A= $Text::Shoebox::DebugB= carp("Empty lexicon to are_hw_keys_uniform")
| |||||||||||||||||||||||||||||||||||||||||||||||
| 431 | unless @$lex; | ||||||||||||||||||||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||||||||||||||||||||
| 433 | 1 | my($hw_key, $e, $i); | |||||||||||||||||||||||||||||||||||||||||||||
| 434 | 1 | ▶ | for(my $i = 0; $i < @$lex; ++$i) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 435 | 2 | ▶ | next unless @{$e = $lex->[$i]}; # just skip null entries, I guess. | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 436 | 2 | ▶ | $Debug && carp("Entry $i has an undef headword"), return 0 | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$Text::Shoebox::Debug and carp("Entry $i has an undef headword")
MC/DC0%0/2
$Text::Shoebox::Debug and carp("Entry $i has an undef headword")
$Text::Shoebox::Debug
carp("Entry $i has an undef headword")
Truth table0%0/3
A and B
A= $Text::Shoebox::DebugB= carp("Entry $i has an undef headword")
| |||||||||||||||||||||||||||||||||||||||||||||||
| 437 | unless defined $e->[0]; | ||||||||||||||||||||||||||||||||||||||||||||||
| 438 | 2 | ▶ | if(defined($hw_key)) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 439 | 1 | ▶ | if($e->[0] ne $hw_key) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 440 | 0 | ▶ | carp("Entry $i\'s hw key \"" . $e->[0] . | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 441 | "\" differs from previous hw key \"$hw_key\"") if $Debug; | ||||||||||||||||||||||||||||||||||||||||||||||
| 442 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 443 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 444 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 445 | 1 | $hw_key = $e->[0]; | |||||||||||||||||||||||||||||||||||||||||||||
| 446 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 447 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 448 | 1 | ▶ | $Debug && carp("Entry $i\'s hw key \"" . $e->[0]), return 0 | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$Text::Shoebox::Debug and carp(qq[Entry ${i}'s hw key "] . $e->[0])
MC/DC0%0/2
$Text::Shoebox::Debug and carp(qq[Entry ${i}'s hw key "] . $e->[0])
$Text::Shoebox::Debug
carp(qq[Entry ${i}'s hw key "] . $e->[0])
Truth table0%0/3
A and B
A= $Text::Shoebox::DebugB= carp(qq[Entry ${i}'s hw key "] . $e->[0])
| |||||||||||||||||||||||||||||||||||||||||||||||
| 449 | unless defined $hw_key; | ||||||||||||||||||||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||||||||||||||||||||
| 451 | 1 | return 1; # all fine. | |||||||||||||||||||||||||||||||||||||||||||||
| 452 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||||||||||||||||||||
| 454 | #-------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||||||||||||||||||
| 456 | =item are_hw_values_unique($lex_lol) | ||||||||||||||||||||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||||||||||||||||||||
| 458 | This function returns true iff all the headword values in all non-null | ||||||||||||||||||||||||||||||||||||||||||||||
| 459 | entries in the lexicon $lol are unique -- i.e., if no two (or more) | ||||||||||||||||||||||||||||||||||||||||||||||
| 460 | entries have the same values for their headword fields. I don't know | ||||||||||||||||||||||||||||||||||||||||||||||
| 461 | if uniqueness is a requirement for SF lexicons that you'd want to | ||||||||||||||||||||||||||||||||||||||||||||||
| 462 | import into Shoebox, but some tasks you put lexicons to might require | ||||||||||||||||||||||||||||||||||||||||||||||
| 463 | it. | ||||||||||||||||||||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||||||||||||||||||||
| 465 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||||||||||||||||||||
| 467 | 2 | ▶ | sub are_hw_values_unique { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
are_hw_values_unique: called CC=5 SCAR=16.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 468 | 2 | my %seen; | |||||||||||||||||||||||||||||||||||||||||||||
| 469 | 2 | foreach my $e (@{$_[0]}) { | |||||||||||||||||||||||||||||||||||||||||||||
| 470 | 5 | ▶ | return 0 if @$e and $seen{ defined($e->[1]) ? $e->[1] : '' }++; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
@$e and $seen{defined $e->[1] ? $e->[1] : ""}++
MC/DC50%1/2
@$e and $seen{defined $e->[1] ? $e->[1] : ""}++
@$e
$seen{defined $e->[1] ? $e->[1] : ""}++
Truth table66%2/3
A and B
A= @$eB= $seen{defined $e->[1] ? $e->[1] : ""}++
| |||||||||||||||||||||||||||||||||||||||||||||||
| 471 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 472 | 1 | return 1; # no duplicates found | |||||||||||||||||||||||||||||||||||||||||||||
| 473 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||||||||||||||||||||
| 475 | #-------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||
| 476 | %p = ( | ||||||||||||||||||||||||||||||||||||||||||||||
| 477 | 4 | ( map {; (chr($_), sprintf('\x%02X',$_)) } 0.. 255 ), | |||||||||||||||||||||||||||||||||||||||||||||
| 478 | "\a" => '\a', # ding! | ||||||||||||||||||||||||||||||||||||||||||||||
| 479 | "\b" => '\b', # BS | ||||||||||||||||||||||||||||||||||||||||||||||
| 480 | "\e" => '\e', # ESC | ||||||||||||||||||||||||||||||||||||||||||||||
| 481 | "\f" => '\f', # FF | ||||||||||||||||||||||||||||||||||||||||||||||
| 482 | "\t" => '\t', # tab | ||||||||||||||||||||||||||||||||||||||||||||||
| 483 | "\cm" => '\cm', | ||||||||||||||||||||||||||||||||||||||||||||||
| 484 | "\cj" => '\cj', | ||||||||||||||||||||||||||||||||||||||||||||||
| 485 | "\n" => '\n', # presumably overrides one of either \cm or \cj | ||||||||||||||||||||||||||||||||||||||||||||||
| 486 | '"' => '\"', | ||||||||||||||||||||||||||||||||||||||||||||||
| 487 | '\\' => '\\\\', | ||||||||||||||||||||||||||||||||||||||||||||||
| 488 | '$' => '\\$', | ||||||||||||||||||||||||||||||||||||||||||||||
| 489 | '@' => '\\@', | ||||||||||||||||||||||||||||||||||||||||||||||
| 490 | '%' => '\\%', | ||||||||||||||||||||||||||||||||||||||||||||||
| 491 | '#' => '\\#', | ||||||||||||||||||||||||||||||||||||||||||||||
| 492 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||||||||||||||||||||
| 494 | 0 | ▶ | sub _dump { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_dump: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||||||||||||||||||||
| 495 | 0 | my $lol = $_[0]; | |||||||||||||||||||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||||||||||||||||||||
| 497 | 0 | print "[ #", scalar(@$lol), " entries...\n"; | |||||||||||||||||||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||||||||||||||||||||
| 499 | 0 | my $safe; | |||||||||||||||||||||||||||||||||||||||||||||
| 500 | 0 | my $toggle = 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 501 | 0 | foreach my $e (@$lol) { | |||||||||||||||||||||||||||||||||||||||||||||
| 502 | 0 | ▶ | next unless defined $e and ref $e and UNIVERSAL::isa($e, 'ARRAY'); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
defined $e and ref $e and UNIVERSAL::isa($e, "ARRAY")
defined $e and ref $e
MC/DC0%0/3
defined $e and ref $e and UNIVERSAL::isa($e, "ARRAY")
defined $e
ref $e
UNIVERSAL::isa($e, "ARRAY")
Truth table0%0/4
A and B and C
A= defined $eB= ref $eC= UNIVERSAL::isa($e, "ARRAY")
| |||||||||||||||||||||||||||||||||||||||||||||||
| 503 | 0 | print " [ "; | |||||||||||||||||||||||||||||||||||||||||||||
| 504 | 0 | foreach my $v (@$e) { | |||||||||||||||||||||||||||||||||||||||||||||
| 505 | 0 | ($safe = $v) =~ | |||||||||||||||||||||||||||||||||||||||||||||
| 506 | s<([^\x20\x21\x23\x27-\x3F\x41-\x5B\x5D-\x7E\xA1-\xFE])> | ||||||||||||||||||||||||||||||||||||||||||||||
| 507 | 0 | <$p{$1}>eg; | |||||||||||||||||||||||||||||||||||||||||||||
| 508 | 0 | ▶ | print( | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 509 | ($toggle ^= 1) ? qq{"$safe" => } : qq{"$safe", \n } | ||||||||||||||||||||||||||||||||||||||||||||||
| 510 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 511 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 512 | 0 | print "],\n"; | |||||||||||||||||||||||||||||||||||||||||||||
| 513 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 514 | 0 | print "];\n"; | |||||||||||||||||||||||||||||||||||||||||||||
| 515 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||||||||||||||||||||
| 517 | #-------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||||||||||||||||||||
| 519 | =back | ||||||||||||||||||||||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||||||||||||||||||||
| 521 | =head1 A NOTE ABOUT VALIDITY | ||||||||||||||||||||||||||||||||||||||||||||||
| 522 | |||||||||||||||||||||||||||||||||||||||||||||||
| 523 | I make very few assumptions about what characters can be in a field | ||||||||||||||||||||||||||||||||||||||||||||||
| 524 | key in SF files. Just now, I happen to assume they can't start with | ||||||||||||||||||||||||||||||||||||||||||||||
| 525 | an underscore (lest they be considered comments), and can't contain | ||||||||||||||||||||||||||||||||||||||||||||||
| 526 | any whitespace characters. | ||||||||||||||||||||||||||||||||||||||||||||||
| 527 | |||||||||||||||||||||||||||||||||||||||||||||||
| 528 | I make essentially no assumptions about what can be in a field value, | ||||||||||||||||||||||||||||||||||||||||||||||
| 529 | except that there can be no newline followed immediately by a | ||||||||||||||||||||||||||||||||||||||||||||||
| 530 | backslash. (Any newline-backslash sequence in turned into | ||||||||||||||||||||||||||||||||||||||||||||||
| 531 | newline-space-backslash.) | ||||||||||||||||||||||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||||||||||||||||||||
| 533 | You should be aware that Shoebox, or whatever other programs use SF | ||||||||||||||||||||||||||||||||||||||||||||||
| 534 | files, may have a I<much> more restricted view of what can be in a | ||||||||||||||||||||||||||||||||||||||||||||||
| 535 | field key or value. | ||||||||||||||||||||||||||||||||||||||||||||||
| 536 | |||||||||||||||||||||||||||||||||||||||||||||||
| 537 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||||||||||||||||||||||||
| 539 | L<Text::Shoebox::Lexicon> | ||||||||||||||||||||||||||||||||||||||||||||||
| 540 | |||||||||||||||||||||||||||||||||||||||||||||||
| 541 | L<Text::Shoebox::Entry> | ||||||||||||||||||||||||||||||||||||||||||||||
| 542 | |||||||||||||||||||||||||||||||||||||||||||||||
| 543 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||||||||||||||||||||
| 544 | |||||||||||||||||||||||||||||||||||||||||||||||
| 545 | Copyright 2000-2004, Sean M. Burke C<sburke@cpan.org>, all rights | ||||||||||||||||||||||||||||||||||||||||||||||
| 546 | reserved. This program is free software; you can redistribute it | ||||||||||||||||||||||||||||||||||||||||||||||
| 547 | and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||||||||||||||
| 548 | |||||||||||||||||||||||||||||||||||||||||||||||
| 549 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||||||
| 550 | |||||||||||||||||||||||||||||||||||||||||||||||
| 551 | Sean M. Burke, C<sburke@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||||||
| 552 | |||||||||||||||||||||||||||||||||||||||||||||||
| 553 | Please contact me if you find that this module is not behaving | ||||||||||||||||||||||||||||||||||||||||||||||
| 554 | correctly. I've tested it only on Shoebox files I generate on my own. | ||||||||||||||||||||||||||||||||||||||||||||||
| 555 | |||||||||||||||||||||||||||||||||||||||||||||||
| 556 | I hasten to point out, incidentally, that I am not in any way | ||||||||||||||||||||||||||||||||||||||||||||||
| 557 | affiliated with the Summer Institute of Linguistics. | ||||||||||||||||||||||||||||||||||||||||||||||
| 558 | |||||||||||||||||||||||||||||||||||||||||||||||
| 559 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||
| 560 | |||||||||||||||||||||||||||||||||||||||||||||||
| 561 | 4 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 562 | |||||||||||||||||||||||||||||||||||||||||||||||
| 563 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||