1 package Equinox::Migration::SimpleTagList;
8 Equinox::Migration::SimpleTagList - Generate taglist from file
16 our $VERSION = '1.000';
21 Using a file as input, E::M::STL generates a set of MARC tags
22 (three-digit, zero-padded integers) and provides several access
23 mechanisms to that list.
25 use Equinox::Migration::SimpleTagList;
27 my $stl = Equinox::Migration::SimpleTagList->new( file => "trashtags.txt" );
28 my $tags = $stl->as_hashref;
32 my $stl = Equinox::Migration::SimpleTagList->new( file => "trashtags.txt" );
33 if ( $stl->has($foo) ) {
34 # if $foo is an element of $stl's parsed list
44 Takes one argument, C<file>, which is mandatory. Returns a E::M::STL
50 my ($class,%args) = @_;
52 my $self = bless { conf => { except => 0,
53 range => { high => 0, low => 0 },
58 $self->{conf}{file} = $args{file}
60 die "Can't open tags file: $!\n";
71 Passed a data field tag, returns 1 if that tag is in the list and 0 if
76 sub has { my ($self, $t) = @_; return (defined $self->{tags}{$t}) ? 1 : 0 }
80 Returns a hashref of the entire, assembled tag list.
84 sub as_hashref { my ($self) = @_; return $self->{tags} }
88 Returns a listref of the entire, assembled tag list.
92 sub as_listref { my ($self) = @_; return \(keys %{$self->{tags}}) }
97 open TAGFILE, '<', $self->{conf}{file};
100 $self->{conf}{range}{high} = 0;
101 $self->{conf}{range}{low} = 0;
102 $self->{conf}{except} = 0;
104 my @chunks = split /\s+/;
105 while (my $chunk = shift @chunks) {
108 if ($chunk =~ /^\d{1,3}$/) {
109 $self->add_tag($chunk);
115 if ($chunk =~ /^\d{1,3}\.\.\d{1,3}$/) {
116 my ($low, $high) = $self->add_range($chunk);
118 unless ($self->{conf}{except}) {
119 $self->{conf}{range}{high} = $high;
120 $self->{conf}{range}{low} = $low;
126 if ($chunk eq 'except') {
127 die "Keyword 'except' can only follow a range (line $.)\n"
128 unless $lastwasrange;
129 die "Keyword 'except' may only occur once per line (line $.)\n"
130 if $self->{conf}{except};
131 $$self->{conf}{except} = 1;
135 die "Unknown chunk $chunk in tags file (line $.)\n";
145 my ($self, $chunk) = @_;
146 my ($low,$high) = split /\.\./, $chunk;
147 die "Ranges must be 'low..high' ($low is greater than $high on line $.)\n"
149 if ($self->{conf}{except}) {
150 die "Exception ranges must be within last addition range (line $.)\n"
151 if ($low < $self->{range}{low} or $high > $self->{range}{high});
153 for my $tag ($low..$high) {
164 my ($self, $tag) = @_;
166 die "Values must be valid tags (000-999)\n"
167 unless ($tag >= 0 and $tag <= 999);
169 if ($self->{conf}{except}) {
170 delete $self->{tags}{$tag};
172 die "Trash tag '$tag' specified twice (line $.)\n"
173 if $self->{tags}{$tag};
174 $self->{tags}{$tag} = 1;
181 Shawn Boyette, C<< <sboyette at esilibrary.com> >>
187 =item * Remove single-except rule?
193 Please report any bugs or feature requests to the above email address.
197 You can find documentation for this module with the perldoc command.
199 perldoc Equinox::Migration::TrashTags
202 =head1 COPYRIGHT & LICENSE
204 Copyright 2009 Shawn Boyette, all rights reserved.
206 This program is free software; you can redistribute it and/or modify it
207 under the same terms as Perl itself.
212 1; # End of Equinox::Migration::SimpleTagList