Skip to content

Commit

Permalink
Fixing marc_replace_all allowing marc_replace_all(field,(search),$1)
Browse files Browse the repository at this point in the history
  • Loading branch information
phochste committed Jul 6, 2017
1 parent 5259a86 commit 7d05569
Show file tree
Hide file tree
Showing 4 changed files with 20 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 Catmandu-MARC

{{$NEXT}}
- Fixing marc_replace_all evaluating search groups

1.16 2017-07-04 15:27:51 CEST
- Adding marc_copy (Carsten Klee) and marc_paste fix
Expand Down
3 changes: 3 additions & 0 deletions lib/Catmandu/Fix/marc_replace_all.pm
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ Catmandu::Fix::marc_replace_all - regex replace (sub)field values in a MARC file
# Replace all 'Joe'-s in 100a to the value in field x.y.z
marc_replace_all('100a','\bJoe\b',$.x.y.z)
# Replace all the content of 100a with everything in curly brackets
marc_replace_all('100a','^(.*)$','{$1}')
=head1 DESCRIPTION
Use regex search and replace on MARC field values.
Expand Down
3 changes: 2 additions & 1 deletion lib/Catmandu/MARC.pm
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,8 @@ sub marc_replace_all {

for (my $i = 0; $i < @subfields; $i += 2) {
if ($subfields[$i] =~ $context->{subfield}) {
$field->[$i + 4] =~ s{$regex}{$value}g;
# Trick to double eval the right hand side
$field->[$i + 4] =~ s{$regex}{"\"$value\""}eeg;
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions t/26_marc_replace_all.t
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,18 @@ use Catmandu::Fix;
], q|fix: marc_replace_all('630a','Active','Silly')|;
}


#---
{
my $fixer = Catmandu::Fix->new(fixes => [q|marc_replace_all('630','(Active)','{$1}')|,q|marc_map('630a','test.$append')|]);
my $importer = Catmandu::Importer::MARC->new( file => 't/camel.mrc', type => "ISO" );
my $record = $fixer->fix($importer->first);

is_deeply $record->{test}, [
'{Active} server pages.' ,
'{Active}X.'
], q|fix: marc_replace_all('630a','Active','{Active}')|;
}


done_testing;

0 comments on commit 7d05569

Please sign in to comment.