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 datafield
22 tags and provides several access mechanisms to that set.
24 use Equinox::Migration::SimpleTagList;
26 my $stl = Equinox::Migration::SimpleTagList->new( file => "trashtags.txt" );
27 my $tags = $stl->as_hashref;
31 my $stl = Equinox::Migration::SimpleTagList->new( file => "trashtags.txt" );
32 if ( $stl->has($foo) ) {
33 # if $foo is an element of $stl's parsed list
43 Takes one argument, optional argument, C<file>. If this is speficied,
44 the tag list will be populated as per that file on instantiation.
46 Returns a E::M::STL object.
51 my ($class, %args) = @_;
53 my $self = bless { conf => { except => 0,
54 range => { high => 0, low => 0 },
61 $self->{conf}{file} = $args{file};
64 die "Can't open tags file: $!\n";
75 Passed a data field tag, returns 1 if that tag is in the list and 0 if
78 When specifying tags under 100, they must be quoted if you wish to
79 include the leading zeroes
81 $stl->has('011'); # is equivalent to
84 or Perl will think you're passing a (possibly malformed) octal value.
88 sub has { my ($self, $t) = @_; $t =~ s/^0+//; return (defined $self->{tags}{$t}) ? 1 : 0 }
92 Returns a hashref of the entire, assembled tag list.
96 sub as_hashref { my ($self) = @_; return $self->{tags} }
100 Returns a listref of the entire, assembled tag list (sorted
105 sub as_listref { my ($self) = @_; return [ sort {$a <=> $b} keys %{$self->{tags}} ] }
110 open TAGFILE, '<', $self->{conf}{file};
112 $self->{conf}{lastwasrange} = 0;
113 $self->{conf}{range}{high} = 0;
114 $self->{conf}{range}{low} = 0;
116 my @chunks = split /\s+/;
117 while (my $chunk = shift @chunks) {
120 if ($chunk =~ /^\d{1,3}$/) {
121 $self->add_tag($chunk);
122 $self->{conf}{except} = 0;
127 if ($chunk =~ /^\d{1,3}\.\.\d{1,3}$/) {
128 $self->add_range($chunk);
129 $self->{conf}{except} = 0;
134 if ($chunk eq 'except') {
135 die "Keyword 'except' can only follow a range (line $.)\n"
136 unless $self->{conf}{lastwasrange};
137 $self->{conf}{except} = 1;
141 die "Unknown chunk $chunk in tags file (line $.)\n";
151 my ($self, $chunk) = @_;
152 my ($low,$high) = split /\.\./, $chunk;
156 die "Ranges must be 'low..high' ($low is greater than $high)\n"
158 if ($self->{conf}{except}) {
159 die "Exception ranges must be within last addition range ($low..$high)\n"
160 if ($low < $self->{conf}{range}{low} or $high > $self->{conf}{range}{high});
162 for my $tag ($low..$high) {
166 unless ($self->{conf}{except}) {
167 $self->{conf}{range}{high} = $high;
168 $self->{conf}{range}{low} = $low;
170 $self->{conf}{lastwasrange} = 1;
178 my ($self, $tag) = @_;
181 die "Values must be valid tags (0-999)\n"
182 unless ($tag >= 0 and $tag <= 999);
184 if ($self->{conf}{except}) {
185 $self->remove_tag($tag);
187 die "Tag '$tag' specified twice\n"
188 if $self->{tags}{$tag};
189 $self->{tags}{$tag} = 1;
190 $self->{conf}{lastwasrange} = 0;
199 my ($self, $tag) = @_;
202 die "Tag '$tag' isn't in the list\n"
203 unless $self->{tags}{$tag};
204 delete $self->{tags}{$tag};
209 Shawn Boyette, C<< <sboyette at esilibrary.com> >>
213 Please report any bugs or feature requests to the above email address.
217 You can find documentation for this module with the perldoc command.
219 perldoc Equinox::Migration::TrashTags
222 =head1 COPYRIGHT & LICENSE
224 Copyright 2009 Equinox, all rights reserved.
226 This program is free software; you can redistribute it and/or modify it
227 under the same terms as Perl itself.
232 1; # End of Equinox::Migration::SimpleTagList