1 package Equinox::Migration::SimpleTagList;
8 Equinox::Migration::SimpleTagList - Generate taglist from file
16 our $VERSION = '1.001';
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 optional argument, C<file>. If this is speficied, the tag
44 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->generate($args{file});
63 die "Can't open tags file '", $args{file}, "': $!\n";
66 $self->generate($args{str},'scalar');
76 Passed a data field tag, returns 1 if that tag is in the list and 0 if
79 When specifying tags under 100, they must be quoted if you wish to
80 include the leading zeroes
82 $stl->has('011'); # is equivalent to
85 or Perl will think you're passing a (possibly malformed) octal value.
89 sub has { my ($self, $t) = @_; $t =~ s/^0+//; return (defined $self->{tags}{$t}) ? 1 : 0 }
93 Returns a hashref of the entire, assembled tag list.
97 sub as_hashref { my ($self) = @_; return $self->{tags} }
101 Returns a listref of the entire, assembled tag list (sorted
106 sub as_listref { my ($self) = @_; return [ sort {$a <=> $b} keys %{$self->{tags}} ] }
109 my ($self, $file, $scalar) = @_;
112 open TAGFILE, '<', \$file;
114 open TAGFILE, '<', $file;
120 $self->{conf}{lastwasrange} = 0;
121 $self->{conf}{range}{high} = 0;
122 $self->{conf}{range}{low} = 0;
124 my @chunks = split /\s+/;
125 while (my $chunk = shift @chunks) {
126 last if ($chunk =~ /^#/);
129 if ($chunk =~ /^\d{1,3}$/) {
130 $self->add_tag($chunk);
131 $self->{conf}{except} = 0;
136 if ($chunk =~ /^\d{1,3}\.\.\d{1,3}$/) {
137 $self->add_range($chunk);
138 $self->{conf}{except} = 0;
143 if ($chunk eq 'except') {
144 die "Keyword 'except' can only follow a range (line $.)\n"
145 unless $self->{conf}{lastwasrange};
146 $self->{conf}{except} = 1;
150 die "Unknown chunk $chunk in tags file (line $.)\n";
160 my ($self, $chunk) = @_;
161 my ($low,$high) = split /\.\./, $chunk;
165 die "Ranges must be 'low..high' ($low is greater than $high)\n"
167 if ($self->{conf}{except}) {
168 die "Exception ranges must be within last addition range ($low..$high)\n"
169 if ($low < $self->{conf}{range}{low} or $high > $self->{conf}{range}{high});
171 for my $tag ($low..$high) {
175 unless ($self->{conf}{except}) {
176 $self->{conf}{range}{high} = $high;
177 $self->{conf}{range}{low} = $low;
179 $self->{conf}{lastwasrange} = 1;
187 my ($self, $tag) = @_;
190 die "Values must be numeric\n" if ($tag =~ /[^\d\-]/);
192 die "Values must be valid tags (0-999)\n"
193 unless ($tag >= 0 and $tag <= 999);
195 if ($self->{conf}{except}) {
196 $self->remove_tag($tag);
198 die "Tag '$tag' specified twice\n"
199 if $self->{tags}{$tag};
200 $self->{tags}{$tag} = 1;
201 $self->{conf}{lastwasrange} = 0;
210 my ($self, $tag) = @_;
213 die "Tag '$tag' isn't in the list\n"
214 unless $self->{tags}{$tag};
215 delete $self->{tags}{$tag};
220 Shawn Boyette, C<< <sboyette at esilibrary.com> >>
224 Please report any bugs or feature requests to the above email address.
228 You can find documentation for this module with the perldoc command.
230 perldoc Equinox::Migration::SimpleTagList
233 =head1 COPYRIGHT & LICENSE
235 Copyright 2009 Equinox, all rights reserved.
237 This program is free software; you can redistribute it and/or modify it
238 under the same terms as Perl itself.
243 1; # End of Equinox::Migration::SimpleTagList