sending marc records into a script that uses MARC::Batch

[email protected] (John E Guillory) Thu, 29 May 2014 16:08:00 +0000
Newsgroups perl.perl4lib
Message-ID <a743c6fab1484a43ae648e41ce579e9c@BLUPR0601MB802.namprd06.prod.outlook.com>
--_000_a743c6fab1484a43ae648e41ce579e9cBLUPR0601MB802namprd06p_
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

Hello,
Two questions please:


1.      I've 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()) {
         .....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 t=
o pipe the output of a previously run marc dump command directly into this =
script via the pipe.
I understand that this can be done using this syntax:    while ($line =3D<S=
TDIN>){ ...}, but I don't understand how to use that STDIN with "MARC::Batc=
h->new('USMARC',$file);"    This does not work:    $batch =3D MARC::Batch->=
new('USMARC',<STDIN>);


2.      My current script successfully reads and processes a marc file of o=
ver 5 gigs!....but exits entirely on record 160,585 with the error from MAR=
C::Batch, "Can't call method "as_string" on an undefined value at ./marc_ba=
tch.pl".  Documentation on using MARC::Batch says that to tell it to contin=
ue processing even when errors are encountered one should use strict_off(),=
 then print/report warnings at the bottom of the script. I don't think my p=
articular error is being handled by the strict_off() setting. Doesn't anybo=
dy know what causes/how to fix "Can't call method as_string?" error? Full s=
cript below-it's 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_positi=
on_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...
       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


--_000_a743c6fab1484a43ae648e41ce579e9cBLUPR0601MB802namprd06p_
Content-Type: text/html; charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

<html xmlns:v=3D"urn:schemas-microsoft-com:vml" xmlns:o=3D"urn:schemas-micr=
osoft-com:office:office" xmlns:w=3D"urn:schemas-microsoft-com:office:word" =
xmlns:m=3D"http://schemas.microsoft.com/office/2004/12/omml" xmlns=3D"http:=
//www.w3.org/TR/REC-html40">
<head>
<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Dus-ascii"=
>
<meta name=3D"Generator" content=3D"Microsoft Word 14 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
	{font-family:Calibri;
	panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
	{margin:0in;
	margin-bottom:.0001pt;
	font-size:11.0pt;
	font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
	{mso-style-priority:99;
	color:blue;
	text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
	{mso-style-priority:99;
	color:purple;
	text-decoration:underline;}
p.MsoListParagraph, li.MsoListParagraph, div.MsoListParagraph
	{mso-style-priority:34;
	margin-top:0in;
	margin-right:0in;
	margin-bottom:0in;
	margin-left:.5in;
	margin-bottom:.0001pt;
	font-size:11.0pt;
	font-family:"Calibri","sans-serif";}
span.EmailStyle17
	{mso-style-type:personal-compose;
	font-family:"Calibri","sans-serif";
	color:windowtext;}
.MsoChpDefault
	{mso-style-type:export-only;
	font-family:"Calibri","sans-serif";}
@page WordSection1
	{size:8.5in 11.0in;
	margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
	{page:WordSection1;}
/* List Definitions */
@list l0
	{mso-list-id:1988121313;
	mso-list-type:hybrid;
	mso-list-template-ids:1292940692 67698703 67698713 67698715 67698703 67698=
713 67698715 67698703 67698713 67698715;}
@list l0:level1
	{mso-level-tab-stop:none;
	mso-level-number-position:left;
	text-indent:-.25in;}
@list l0:level2
	{mso-level-number-format:alpha-lower;
	mso-level-tab-stop:none;
	mso-level-number-position:left;
	text-indent:-.25in;}
@list l0:level3
	{mso-level-number-format:roman-lower;
	mso-level-tab-stop:none;
	mso-level-number-position:right;
	text-indent:-9.0pt;}
@list l0:level4
	{mso-level-tab-stop:none;
	mso-level-number-position:left;
	text-indent:-.25in;}
@list l0:level5
	{mso-level-number-format:alpha-lower;
	mso-level-tab-stop:none;
	mso-level-number-position:left;
	text-indent:-.25in;}
@list l0:level6
	{mso-level-number-format:roman-lower;
	mso-level-tab-stop:none;
	mso-level-number-position:right;
	text-indent:-9.0pt;}
@list l0:level7
	{mso-level-tab-stop:none;
	mso-level-number-position:left;
	text-indent:-.25in;}
@list l0:level8
	{mso-level-number-format:alpha-lower;
	mso-level-tab-stop:none;
	mso-level-number-position:left;
	text-indent:-.25in;}
@list l0:level9
	{mso-level-number-format:roman-lower;
	mso-level-tab-stop:none;
	mso-level-number-position:right;
	text-indent:-9.0pt;}
ol
	{margin-bottom:0in;}
ul
	{margin-bottom:0in;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext=3D"edit" spidmax=3D"1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext=3D"edit">
<o:idmap v:ext=3D"edit" data=3D"1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang=3D"EN-US" link=3D"blue" vlink=3D"purple">
<div class=3D"WordSection1">
<p class=3D"MsoNormal">Hello,<o:p></o:p></p>
<p class=3D"MsoNormal">Two questions please:<o:p></o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoListParagraph" style=3D"text-indent:-.25in;mso-list:l0 level=
1 lfo1"><![if !supportLists]><span style=3D"mso-list:Ignore">1.<span style=
=3D"font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span><![endif]>I&#8217;ve written a script that opens a marc file =
for reading using this syntax:<o:p></o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal">$file =3D $ARGV[0];<o:p></o:p></p>
<p class=3D"MsoNormal">$batch =3D MARC::Batch-&gt;new('USMARC',$file);<o:p>=
</o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal">It then loops thru the records using this syntax:<o:=
p></o:p></p>
<p class=3D"MsoNormal">while ( $record =3D $batch-&gt;next()) {<o:p></o:p><=
/p>
<p class=3D"MsoNormal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8=
230;..check position 6, 7 of leader and position 23 of 008 and make some ch=
anges<o:p></o:p></p>
<p class=3D"MsoNormal">}<o:p></o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></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. &nbsp;<o:p></o:p></p>
<p class=3D"MsoNormal">I understand that this can be done using this syntax=
:&nbsp;&nbsp;&nbsp; while ($line =3D&lt;STDIN&gt;){ &#8230;}, but I don&#82=
17;t understand how to use that STDIN with &#8220;MARC::Batch-&gt;new(&#821=
6;USMARC&#8217;,$file);&#8221;&nbsp;&nbsp;&nbsp; This does not work:&nbsp;&=
nbsp;&nbsp; $batch =3D MARC::Batch-&gt;new(&#8216;USMARC&#8217;,&lt;STDIN&g=
t;);<o:p></o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoListParagraph" style=3D"text-indent:-.25in;mso-list:l0 level=
1 lfo1"><![if !supportLists]><span style=3D"mso-list:Ignore">2.<span style=
=3D"font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span><![endif]>My current script successfully reads and processes =
a marc file of over 5 gigs!....but exits entirely on record 160,585 with th=
e error from MARC::Batch, &#8220;Can't call method &quot;as_string&quot; on=
 an undefined value at ./marc_batch.pl&#8221;.&nbsp; 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&#8217;t think my particular error i=
s being handled by the strict_off() setting.
 Doesn&#8217;t anybody know what causes/how to fix &#8220;Can&#8217;t call =
method as_string?&#8221; error? Full script below&#8212;it&#8217;s pretty s=
hort, thanks to MARC::Batch.
<o:p></o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal">Thanks for ensights!&nbsp; <o:p></o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal">use MARC::Batch;<o:p></o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal">$file =3D $ARGV[0];<o:p></o:p></p>
<p class=3D"MsoNormal">chomp($file);<o:p></o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal">$batch =3D MARC::Batch-&gt;new('USMARC',$file);<o:p>=
</o:p></p>
<p class=3D"MsoNormal">$batch-&gt;strict_off();&nbsp;&nbsp;&nbsp; # otherwi=
se script exits when encounters errors<o:p></o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal">open(OUT,'&gt;new_marc');<o:p></o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal">while ( $record =3D $batch-&gt;next()) {<o:p></o:p><=
/p>
<p class=3D"MsoNormal">&nbsp;&nbsp;&nbsp; $leader&nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =3D $recor=
d-&gt;leader();<o:p></o:p></p>
<p class=3D"MsoNormal">&nbsp;&nbsp;&nbsp; $leader_pos_6&nbsp;&nbsp;&nbsp;&n=
bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =3D substr($leader,6,1);<o:p></o:p></p>
<p class=3D"MsoNormal">&nbsp;&nbsp;&nbsp; $leader_pos_7&nbsp;&nbsp;&nbsp;&n=
bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =3D substr($leader,7,1);<o:p></o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal">&nbsp;&nbsp;&nbsp; $field&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =3D $=
record-&gt;field('008');<o:p></o:p></p>
<p class=3D"MsoNormal">&nbsp;&nbsp;&nbsp; $field_008&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =3D $field-&gt;as_string(=
);<o:p></o:p></p>
<p class=3D"MsoNormal">&nbsp;&nbsp;&nbsp; $field_008_position_23 =3D substr=
($field_008,23,1);<o:p></o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal">if ( ($leader_pos_6 eq &quot;a&quot;) &amp;&amp; ($l=
eader_pos_7 eq &quot;m&quot;) &amp;&amp; ($field_008_position_23 eq &quot;o=
&quot;) || ($field_008_position_23 eq &quot;s&quot;) ) {<o:p></o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $control_num&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =3D $record-&gt;field('001');<o:p><=
/o:p></p>
<p class=3D"MsoNormal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $control_num&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =3D $control_num-&gt;as_string();<o=
:p></o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print &quot;008=
 position 23: $field_008_position_23 \n&quot;;<o:p></o:p></p>
<p class=3D"MsoNormal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print &quot;OLD=
 leader: $leader \n&quot;;<o:p></o:p></p>
<p class=3D"MsoNormal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $old_leader =3D=
 $leader;<o:p></o:p></p>
<p class=3D"MsoNormal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; substr($leader,=
6,1) =3D 'm';<o:p></o:p></p>
<p class=3D"MsoNormal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print &quot;NEW=
 leader: $leader \n&quot;;<o:p></o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print OUT $reco=
rd-&gt;as_usmarc();<o:p></o:p></p>
<p class=3D"MsoNormal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print &quot;$con=
trol_num|$old_leader|$leader|$field_008\n&quot;;<o:p></o:p></p>
<p class=3D"MsoNormal">&nbsp;&nbsp; <o:p></o:p></p>
<p class=3D"MsoNormal">} else {&nbsp; # not a match so just print this one =
unchanged&#8230;<o:p></o:p></p>
<p class=3D"MsoNormal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print OUT $reco=
rd-&gt;as_usmarc();<o:p></o:p></p>
<p class=3D"MsoNormal">}<o:p></o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal">}<o:p></o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal"># handles errors:<o:p></o:p></p>
<p class=3D"MsoNormal">if (@warnings =3D $batch-&gt;warnings()) {<o:p></o:p=
></p>
<p class=3D"MsoNormal">&nbsp;&nbsp;&nbsp;&nbsp; print &quot;\n Warnings det=
ected: \n&quot;, @warnings;<o:p></o:p></p>
<p class=3D"MsoNormal">}<o:p></o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal">close(OUT);<o:p></o:p></p>
<p class=3D"MsoNormal">close(LOG);<o:p></o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal">John Guillory<o:p></o:p></p>
<p class=3D"MsoNormal">Louisiana Library Network<o:p></o:p></p>
<p class=3D"MsoNormal">225.578.3758<o:p></o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
</div>
</body>
</html>

--_000_a743c6fab1484a43ae648e41ce579e9cBLUPR0601MB802namprd06p_--