Skip to content

Commit

Permalink
Add parser method: all
Browse files Browse the repository at this point in the history
  • Loading branch information
nichtich committed Aug 18, 2023
1 parent 83242af commit e109e97
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Revision history for PICA::Data

{{$NEXT}}
- Add parser method: all

2.10 2023-08-09T14:01:25Z
- Add PICA Import format parser (#129)
Expand Down
19 changes: 18 additions & 1 deletion lib/PICA/Parser/Base.pm
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,17 @@ sub next {
return;
}

sub all {
my ($self) = @_;
my @records;

while (my $record = $self->next) {
push @records, $record;
}

return \@records;
}

1;
__END__
Expand All @@ -103,6 +114,8 @@ PICA::Parser::Base - abstract base class of PICA parsers
my $parser = PICA::Parser::XML->new( $filename, start => 1 );
...
my $records = $parser->all;
=head1 DESCRIPTION
This abstract base class of PICA+ parsers should not be instantiated directly.
Expand Down Expand Up @@ -151,13 +164,17 @@ reference to a Unicode string. L<PICA::Parser::XML> also detects plain XML strin
=head2 next
Reads the next PICA+ record. Returns a L<PICA::Data> object (that is a blessed
Read the next PICA+ record and return a L<PICA::Data> object (that is a blessed
hash with keys C<record> and optional C<_id>).
=head2 count
Get the number of records read so far.
=head2 all
Read all records and return as array reference.
=head1 SEE ALSO
See L<Catmandu::Importer::PICA> for usage of this module in L<Catmandu>.
Expand Down
4 changes: 4 additions & 0 deletions t/20-parser.t
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ is $first->{record}->[6]->[7], '柳经纬主编;', 'Unicode';
is_deeply $first->{record}->[11],
['145Z', '40', 'a', '$', 'b', 'test$', 'c', '...'], 'sub field with $';

my $all = pica_parser(plain => 't/files/pica.plain')->all;
is_deeply $all->[0], $first, 'read all';
is @$all, 2, 'read all';

foreach my $type (qw(Plain Plus JSON Binary XML PPXML PIXML Import)) {
my $module = "PICA::Parser::$type";
my $file = 't/files/pica.' . lc($type);
Expand Down

0 comments on commit e109e97

Please sign in to comment.