Re: sending marc records into a script that uses MARC::Batch
[email protected] (Timothy Prettyman) Thu, 29 May 2014 12:23:14 -0400
| Newsgroups | perl.perl4lib |
|---|---|
| Message-ID | <CA+rAGyP2LJrxkUtGeKvFs61DibUE5sw6APXzFTe_Te8oSjvhaw@mail.gmail.com> |
--047d7b86ed6289ca5404fa8c586a
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
For your first question, instead of:
$batch =3D MARC::Batch->new(=E2=80=98USMARC=E2=80=99,<STDIN>);
use:
$batch =3D MARC::Batch->new(=E2=80=98USMARC=E2=80=99,STDIN);
For your second, the error is likely caused when a field you're using
as_string() on doesn't exist in the record.
So, you could do something like the following:
$field =3D $record->field('008');
$field or do { # check for
existence of field
print "no 008 field for record\n"; # no field
next; # skip the field
(or whatever)
};
$field_008 =3D $field->as_string();
Hope this helps
-Tim
Timothy Prettyman
LIT/Library Systems
University of Michigan
On Thu, May 29, 2014 at 12:08 PM, John E Guillory <[email protected]> wrote:
> Hello,
>
> Two questions please:
>
>
>
> 1. I=E2=80=99ve written a script that opens a marc file for reading =
using
> this syntax:
>
>
>
> $file =3D $ARGV[0];
>
> $batch =3D MARC::Batch->new('USMARC',$file);
>
>
>
> It then loops thru the records using this syntax:
>
> while ( $record =3D $batch->next()) {
>
> =E2=80=A6..check position 6, 7 of leader and position 23 of 008 =
and make
> some changes
>
> }
>
>
>
> This works great. However, instead of accessing the file this way, I want
> to pipe the output of a previously run marc dump command directly into th=
is
> script via the pipe.
>
> I understand that this can be done using this syntax: while ($line
> =3D<STDIN>){ =E2=80=A6}, but I don=E2=80=99t understand how to use that S=
TDIN with
> =E2=80=9CMARC::Batch->new(=E2=80=98USMARC=E2=80=99,$file);=E2=80=9D Th=
is does not work: $batch =3D
> MARC::Batch->new(=E2=80=98USMARC=E2=80=99,<STDIN>);
>
>
>
> 2. My current script successfully reads and processes a marc file of
> over 5 gigs!....but exits entirely on record 160,585 with the error from
> MARC::Batch, =E2=80=9CCan't call method "as_string" on an undefined value=
at ./
> marc_batch.pl=E2=80=9D. Documentation on using MARC::Batch says that to =
tell it
> to continue processing even when errors are encountered one should use
> strict_off(), then print/report warnings at the bottom of the script. I
> don=E2=80=99t think my particular error is being handled by the strict_of=
f()
> setting. Doesn=E2=80=99t anybody know what causes/how to fix =E2=80=9CCan=
=E2=80=99t call method
> as_string?=E2=80=9D error? Full script below=E2=80=94it=E2=80=99s pretty =
short, thanks to
> MARC::Batch.
>
>
>
> Thanks for ensights!
>
>
>
>
>
> use MARC::Batch;
>
>
>
> $file =3D $ARGV[0];
>
> chomp($file);
>
>
>
> $batch =3D MARC::Batch->new('USMARC',$file);
>
> $batch->strict_off(); # otherwise script exits when encounters errors
>
>
>
> open(OUT,'>new_marc');
>
>
>
> while ( $record =3D $batch->next()) {
>
> $leader =3D $record->leader();
>
> $leader_pos_6 =3D substr($leader,6,1);
>
> $leader_pos_7 =3D substr($leader,7,1);
>
>
>
> $field =3D $record->field('008');
>
> $field_008 =3D $field->as_string();
>
> $field_008_position_23 =3D substr($field_008,23,1);
>
>
>
> if ( ($leader_pos_6 eq "a") && ($leader_pos_7 eq "m") &&
> ($field_008_position_23 eq "o") || ($field_008_position_23 eq "s") ) {
>
>
>
> $control_num =3D $record->field('001');
>
> $control_num =3D $control_num->as_string();
>
>
>
> print "008 position 23: $field_008_position_23 \n";
>
> print "OLD leader: $leader \n";
>
> $old_leader =3D $leader;
>
> substr($leader,6,1) =3D 'm';
>
> print "NEW leader: $leader \n";
>
>
>
> print OUT $record->as_usmarc();
>
> print "$control_num|$old_leader|$leader|$field_008\n";
>
>
>
> } else { # not a match so just print this one unchanged=E2=80=A6
>
> print OUT $record->as_usmarc();
>
> }
>
>
>
> }
>
>
>
> # handles errors:
>
> if (@warnings =3D $batch->warnings()) {
>
> print "\n Warnings detected: \n", @warnings;
>
> }
>
>
>
> close(OUT);
>
> close(LOG);
>
>
>
>
>
>
>
> John Guillory
>
> Louisiana Library Network
>
> 225.578.3758
>
>
>
--047d7b86ed6289ca5404fa8c586a
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">For your first question, instead of:<div><br></div><div><s=
pan style=3D"font-family:arial,sans-serif;font-size:13px">=C2=A0$batch =3D =
MARC::Batch->new(=E2=80=98USMARC=E2=80=99,<</span><span style=3D"font=
-family:arial,sans-serif;font-size:13px">STDIN>);</span></div>
<div><font face=3D"arial, sans-serif"><br></font></div><div><font face=3D"a=
rial, sans-serif">use:</font></div><div><font face=3D"arial, sans-serif"><b=
r></font></div><div><span style=3D"font-family:arial,sans-serif;font-size:1=
3px">=C2=A0$batch =3D MARC::Batch->new(=E2=80=98USMARC=E2=80=99,</span><=
span style=3D"font-family:arial,sans-serif;font-size:13px">STDIN);</span></=
div>
<div><font face=3D"arial, sans-serif"><br></font></div><div><font face=3D"a=
rial, sans-serif">For your second, the error is likely caused when a field =
you're using as_string() on doesn't exist in the record. =C2=A0<br>=
</font><div>
<br></div><div>So, you could do something like the following:</div><div><sp=
an style=3D"font-family:arial,sans-serif;font-size:13px"><br></span></div><=
div><span style=3D"font-family:arial,sans-serif;font-size:13px">$field=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0 =3D $record->field('008');</span><br>
</div><div><span style=3D"font-family:arial,sans-serif;font-size:13px">$fie=
ld or do { =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0# check for existence of field</span></div><div><span style=3D"fo=
nt-family:arial,sans-serif;font-size:13px">=C2=A0 =C2=A0print "no 008 =
field for record\n"; =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0# no fie=
ld</span></div>
<div><span style=3D"font-family:arial,sans-serif;font-size:13px">=C2=A0 =C2=
=A0next; =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0# skip the field (or whatever)</span></di=
v><div><span style=3D"font-family:arial,sans-serif;font-size:13px">};</span=
></div>
<div><p class=3D"MsoNormal" style=3D"font-family:arial,sans-serif;font-size=
:13px"><u></u></p><p class=3D"MsoNormal" style=3D"font-family:arial,sans-se=
rif;font-size:13px">$field_008=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0 =3D $field->as_string();</p></div><div>
<br></div></div><div>Hope this helps</div><div><br></div><div>-Tim</div><di=
v><br></div><div>Timothy Prettyman</div><div>LIT/Library Systems</div><div>=
University of Michigan</div></div><div class=3D"gmail_extra"><br><br><div c=
lass=3D"gmail_quote">
On Thu, May 29, 2014 at 12:08 PM, John E Guillory <span dir=3D"ltr"><<a =
href=3D"mailto:[email protected]" target=3D"_blank">[email protected]</a>></span=
> wrote:<br><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;bo=
rder-left:1px #ccc solid;padding-left:1ex">
<div lang=3D"EN-US" link=3D"blue" vlink=3D"purple">
<div>
<p class=3D"MsoNormal">Hello,<u></u><u></u></p>
<p class=3D"MsoNormal">Two questions please:<u></u><u></u></p>
<p class=3D"MsoNormal"><u></u>=C2=A0<u></u></p>
<p><u></u><span>1.<span style=3D"font:7.0pt "Times New Roman"">=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0
</span></span><u></u>I=E2=80=99ve written a script that opens a marc file f=
or reading using this syntax:<u></u><u></u></p>
<p class=3D"MsoNormal"><u></u>=C2=A0<u></u></p>
<p class=3D"MsoNormal">$file =3D $ARGV[0];<u></u><u></u></p>
<p class=3D"MsoNormal">$batch =3D MARC::Batch->new('USMARC',$fil=
e);<u></u><u></u></p>
<p class=3D"MsoNormal"><u></u>=C2=A0<u></u></p>
<p class=3D"MsoNormal">It then loops thru the records using this syntax:<u>=
</u><u></u></p>
<p class=3D"MsoNormal">while ( $record =3D $batch->next()) {<u></u><u></=
u></p>
<p class=3D"MsoNormal">=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 =E2=
=80=A6..check position 6, 7 of leader and position 23 of 008 and make some =
changes<u></u><u></u></p>
<p class=3D"MsoNormal">}<u></u><u></u></p>
<p class=3D"MsoNormal"><u></u>=C2=A0<u></u></p>
<p class=3D"MsoNormal">This works great. However, instead of accessing the =
file this way, I want to pipe the output of a previously run marc dump comm=
and directly into this script via the pipe. =C2=A0<u></u><u></u></p>
<p class=3D"MsoNormal">I understand that this can be done using this syntax=
:=C2=A0=C2=A0=C2=A0 while ($line =3D<STDIN>){ =E2=80=A6}, but I don=
=E2=80=99t understand how to use that STDIN with =E2=80=9CMARC::Batch->n=
ew(=E2=80=98USMARC=E2=80=99,$file);=E2=80=9D=C2=A0=C2=A0=C2=A0 This does no=
t work:=C2=A0=C2=A0=C2=A0 $batch =3D MARC::Batch->new(=E2=80=98USMARC=E2=
=80=99,<STDIN>);<u></u><u></u></p>
<p class=3D"MsoNormal"><u></u>=C2=A0<u></u></p>
<p><u></u><span>2.<span style=3D"font:7.0pt "Times New Roman"">=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0
</span></span><u></u>My current script successfully reads and processes a m=
arc file of over 5 gigs!....but exits entirely on record 160,585 with the e=
rror from MARC::Batch, =E2=80=9CCan't call method "as_string"=
on an undefined value at ./<a href=3D"http://marc_batch.pl" target=3D"_bla=
nk">marc_batch.pl</a>=E2=80=9D.=C2=A0 Documentation
on using MARC::Batch says that to tell it to continue processing even when=
errors are encountered one should use strict_off(), then print/report warn=
ings at the bottom of the script. I don=E2=80=99t think my particular error=
is being handled by the strict_off() setting.
Doesn=E2=80=99t anybody know what causes/how to fix =E2=80=9CCan=E2=80=99t=
call method as_string?=E2=80=9D error? Full script below=E2=80=94it=E2=80=
=99s pretty short, thanks to MARC::Batch.
<u></u><u></u></p>
<p class=3D"MsoNormal"><u></u>=C2=A0<u></u></p>
<p class=3D"MsoNormal">Thanks for ensights!=C2=A0 <u></u><u></u></p>
<p class=3D"MsoNormal"><u></u>=C2=A0<u></u></p>
<p class=3D"MsoNormal"><u></u>=C2=A0<u></u></p>
<p class=3D"MsoNormal">use MARC::Batch;<u></u><u></u></p>
<p class=3D"MsoNormal"><u></u>=C2=A0<u></u></p>
<p class=3D"MsoNormal">$file =3D $ARGV[0];<u></u><u></u></p>
<p class=3D"MsoNormal">chomp($file);<u></u><u></u></p>
<p class=3D"MsoNormal"><u></u>=C2=A0<u></u></p>
<p class=3D"MsoNormal">$batch =3D MARC::Batch->new('USMARC',$fil=
e);<u></u><u></u></p>
<p class=3D"MsoNormal">$batch->strict_off();=C2=A0=C2=A0=C2=A0 # otherwi=
se script exits when encounters errors<u></u><u></u></p>
<p class=3D"MsoNormal"><u></u>=C2=A0<u></u></p>
<p class=3D"MsoNormal">open(OUT,'>new_marc');<u></u><u></u></p>
<p class=3D"MsoNormal"><u></u>=C2=A0<u></u></p>
<p class=3D"MsoNormal">while ( $record =3D $batch->next()) {<u></u><u></=
u></p>
<p class=3D"MsoNormal">=C2=A0=C2=A0=C2=A0 $leader=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 =3D $rec=
ord->leader();<u></u><u></u></p>
<p class=3D"MsoNormal">=C2=A0=C2=A0=C2=A0 $leader_pos_6=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 =3D substr($leader,6,1);<u></u><u></u>=
</p>
<p class=3D"MsoNormal">=C2=A0=C2=A0=C2=A0 $leader_pos_7=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 =3D substr($leader,7,1);<u></u><u></u>=
</p>
<p class=3D"MsoNormal"><u></u>=C2=A0<u></u></p>
<p class=3D"MsoNormal">=C2=A0=C2=A0=C2=A0 $field=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 =3D $=
record->field('008');<u></u><u></u></p>
<p class=3D"MsoNormal">=C2=A0=C2=A0=C2=A0 $field_008=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 =3D $field->as_strin=
g();<u></u><u></u></p>
<p class=3D"MsoNormal">=C2=A0=C2=A0=C2=A0 $field_008_position_23 =3D substr=
($field_008,23,1);<u></u><u></u></p>
<p class=3D"MsoNormal"><u></u>=C2=A0<u></u></p>
<p class=3D"MsoNormal">if ( ($leader_pos_6 eq "a") && ($l=
eader_pos_7 eq "m") && ($field_008_position_23 eq "o=
") || ($field_008_position_23 eq "s") ) {<u></u><u></u></p>
<p class=3D"MsoNormal"><u></u>=C2=A0<u></u></p>
<p class=3D"MsoNormal">=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 $control_num=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 =3D $record->field('001'=
);<u></u><u></u></p>
<p class=3D"MsoNormal">=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 $control_num=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 =3D $control_num->as_string();<u=
></u><u></u></p>
<p class=3D"MsoNormal"><u></u>=C2=A0<u></u></p>
<p class=3D"MsoNormal">=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 print "008=
position 23: $field_008_position_23 \n";<u></u><u></u></p>
<p class=3D"MsoNormal">=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 print "OLD=
leader: $leader \n";<u></u><u></u></p>
<p class=3D"MsoNormal">=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 $old_leader =3D=
$leader;<u></u><u></u></p>
<p class=3D"MsoNormal">=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 substr($leader,=
6,1) =3D 'm';<u></u><u></u></p>
<p class=3D"MsoNormal">=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 print "NEW=
leader: $leader \n";<u></u><u></u></p>
<p class=3D"MsoNormal"><u></u>=C2=A0<u></u></p>
<p class=3D"MsoNormal">=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 print OUT $reco=
rd->as_usmarc();<u></u><u></u></p>
<p class=3D"MsoNormal">=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0print "$con=
trol_num|$old_leader|$leader|$field_008\n";<u></u><u></u></p>
<p class=3D"MsoNormal">=C2=A0=C2=A0 <u></u><u></u></p>
<p class=3D"MsoNormal">} else {=C2=A0 # not a match so just print this one =
unchanged=E2=80=A6<u></u><u></u></p>
<p class=3D"MsoNormal">=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 print OUT $reco=
rd->as_usmarc();<u></u><u></u></p>
<p class=3D"MsoNormal">}<u></u><u></u></p>
<p class=3D"MsoNormal"><u></u>=C2=A0<u></u></p>
<p class=3D"MsoNormal">}<u></u><u></u></p>
<p class=3D"MsoNormal"><u></u>=C2=A0<u></u></p>
<p class=3D"MsoNormal"># handles errors:<u></u><u></u></p>
<p class=3D"MsoNormal">if (@warnings =3D $batch->warnings()) {<u></u><u>=
</u></p>
<p class=3D"MsoNormal">=C2=A0=C2=A0=C2=A0=C2=A0 print "\n Warnings det=
ected: \n", @warnings;<u></u><u></u></p>
<p class=3D"MsoNormal">}<u></u><u></u></p>
<p class=3D"MsoNormal"><u></u>=C2=A0<u></u></p>
<p class=3D"MsoNormal">close(OUT);<u></u><u></u></p>
<p class=3D"MsoNormal">close(LOG);<u></u><u></u></p>
<p class=3D"MsoNormal"><u></u>=C2=A0<u></u></p>
<p class=3D"MsoNormal"><u></u>=C2=A0<u></u></p>
<p class=3D"MsoNormal"><u></u>=C2=A0<u></u></p>
<p class=3D"MsoNormal">John Guillory<u></u><u></u></p>
<p class=3D"MsoNormal">Louisiana Library Network<u></u><u></u></p>
<p class=3D"MsoNormal"><a href=3D"tel:225.578.3758" value=3D"+12255783758" =
target=3D"_blank">225.578.3758</a><u></u><u></u></p>
<p class=3D"MsoNormal"><u></u>=C2=A0<u></u></p>
</div>
</div>
</blockquote></div><br></div>
--047d7b86ed6289ca5404fa8c586a--