RE: MARC blob to MARC::Record object
Hi Leif, > you can simply read from your database (i.e. your statement handle) like you > were reading from a file. Very interesting! Tack för att du tog dig tid att lägga ett tillägg! A bit on the bleeding edge for me, perhaps, but I may try it for the experience. -- Michael # Michael Doran, Systems Librarian # University of Texas at Arlington # 817-272-5326 office # 817-688-1926 mobile # do...@uta.edu # http://rocky.uta.edu/doran/ > -Original Message- > From: Leif Andersson [mailto:leif.anders...@sub.su.se] > Sent: Monday, January 10, 2011 8:35 AM > To: Doran, Michael D; perl4lib > Subject: Re: MARC blob to MARC::Record object > > I hope you will forgive me for a late addendum. > Not only do I have to apologize for the late arrival of this post, I also > should apologize for its (lack of) seriousness. > Actually - this is in every respect just a programming scherzo, so to speak. > (Even though the code below works, at least for me) > Now you are all warned. ;-) > > So: If you are used to letting MARC::Batch read the records from a file, then > you can simply read from your database (i.e. your statement handle) like you > were reading from a file. > Like this: > > > #!/usr/local/bin/perl -w > use DBI; > use MARC::Batch; > use strict; > #BEGIN { > #$ENV{NLS_LANG} = ...; > #} > my $dbh = DBI->connect(...) || die 1; > $dbh->{LongReadLen} = 9; > $dbh->{LongTruncOk} = 0; > my $sql = q( select GetBibBlob(bib_id) from bib_master where rownum <= 3 ); > my $sth = $dbh->prepare($sql) || die 2; > my $rv = $sth->execute() || die 3; > # add some magic: > tie(*MARC, 'dbfile', $sth); > # pass the virtual filehandle to MARC::Batch > my $batch = MARC::Batch->new('USMARC', *MARC ); > $batch->strict_off; > # read as usual > while ( my $marc = $batch->next ) { > print $marc->as_formatted(), "\n\n"; > } > > #--- > package dbfile; > use strict; > sub TIEHANDLE { > my ($class, $sth) = @_; > my $i = { 'sth' => $sth, > 'eof' => 0, }; > bless $i, $class; > } > sub READLINE { > my ($marc) = $_[0]->{sth}->fetchrow_array() ; > if (defined $marc) { > my $len = substr($marc,0,5); > return substr($marc,0,$len); > } > else { > $_[0]->{'eof'} = 1; > return undef; > } > } > sub EOF { > # eof() > $_[0]->{'eof'}; > } > sub FILENO {1} > sub BINMODE {1} > sub CLOSE {1} > sub DESTROY {1} > __END__ > > > That's all folks, > > /Leif > Leif Andersson, Systems Librarian > Stockholm University Library > > > Från: Doran, Michael D [do...@uta.edu] > Skickat: den 7 januari 2011 15:11 > Till: Leif Andersson; 'Jon Gorman'; perl4lib > Ämne: RE: MARC blob to MARC::Record object > > Hi Leif and Jon, > > > use MARC::Record; > > ... > > my $record = MARC::Record->new_from_usmarc( $blob ); > > This works! > > > From: Jon Gorman [mailto:jonathan.gor...@gmail.com] > > Sent: Friday, January 07, 2011 7:51 AM > > You'll probably think of this when you get up, but did you make sure > > to import the package? ie use MARC::FILE::USMARC;? > > This made the other way work, too! (I had only "use MARC::File") > > Much thanks to Leif and Jon. > > -- Michael > > # Michael Doran, Systems Librarian > # University of Texas at Arlington > # 817-272-5326 office > # 817-688-1926 mobile > # do...@uta.edu > # http://rocky.uta.edu/doran/ > > > -Original Message- > > From: Leif Andersson [mailto:leif.anders...@sub.su.se] > > Sent: Friday, January 07, 2011 7:50 AM > > To: Doran, Michael D; perl4lib > > Subject: Re: MARC blob to MARC::Record object > > > > Hi Michael, > > > > this is how I - in principle - usually do it: > > > > use MARC::Record; > > ... > > my $record = MARC::Record->new_from_usmarc( $blob ); > > > > /Leif > > > > Leif Andersson, Systems librarian > > Stockholm University Library > > > > Från: Doran, Michael D [do...@uta.edu] > > Skickat: den 7 januari 2011 00:18 > > Till: perl4lib > > Ämne: MARC blob to MARC::Record object > > > > I am working on a Perl script that retrieves data from our Voyager ILS via > an > > SQL query. Among other data, I have MARC records in blob form, and the > script > > processes one MARC record at a time. I want to be able to parse and > > modify/convert the MARC record (using MARC::Record) before writing/printing > > data to a file. > > > > How do I make the MARC blob into a MARC::Record object (without having to > > first save it a file and read it in with MARC::File/Batch)? The MARC blob > is > > already in a variable, so it doesn't make sense (to me) to write it out to a > > file just so I can read it back in. Unless I have to, natch. > > > > I apologize if I am missing something obvious. > > > > -- Michael > > > > # Michael Doran, Systems Librarian > > # University of Texas at Arlington > > # 817-272-5326 office > > # 817-688-1926 mobile > > # do...@uta.edu > > # http://rocky.uta.edu/doran/
Re: MARC blob to MARC::Record object
I hope you will forgive me for a late addendum. Not only do I have to apologize for the late arrival of this post, I also should apologize for its (lack of) seriousness. Actually - this is in every respect just a programming scherzo, so to speak. (Even though the code below works, at least for me) Now you are all warned. ;-) So: If you are used to letting MARC::Batch read the records from a file, then you can simply read from your database (i.e. your statement handle) like you were reading from a file. Like this: #!/usr/local/bin/perl -w use DBI; use MARC::Batch; use strict; #BEGIN { #$ENV{NLS_LANG} = ...; #} my $dbh = DBI->connect(...) || die 1; $dbh->{LongReadLen} = 9; $dbh->{LongTruncOk} = 0; my $sql = q( select GetBibBlob(bib_id) from bib_master where rownum <= 3 ); my $sth = $dbh->prepare($sql) || die 2; my $rv = $sth->execute() || die 3; # add some magic: tie(*MARC, 'dbfile', $sth); # pass the virtual filehandle to MARC::Batch my $batch = MARC::Batch->new('USMARC', *MARC ); $batch->strict_off; # read as usual while ( my $marc = $batch->next ) { print $marc->as_formatted(), "\n\n"; } #--- package dbfile; use strict; sub TIEHANDLE { my ($class, $sth) = @_; my $i = { 'sth' => $sth, 'eof' => 0, }; bless $i, $class; } sub READLINE { my ($marc) = $_[0]->{sth}->fetchrow_array() ; if (defined $marc) { my $len = substr($marc,0,5); return substr($marc,0,$len); } else { $_[0]->{'eof'} = 1; return undef; } } sub EOF { # eof() $_[0]->{'eof'}; } sub FILENO {1} sub BINMODE {1} sub CLOSE {1} sub DESTROY {1} __END__ That's all folks, /Leif Leif Andersson, Systems Librarian Stockholm University Library Från: Doran, Michael D [do...@uta.edu] Skickat: den 7 januari 2011 15:11 Till: Leif Andersson; 'Jon Gorman'; perl4lib Ämne: RE: MARC blob to MARC::Record object Hi Leif and Jon, > use MARC::Record; > ... > my $record = MARC::Record->new_from_usmarc( $blob ); This works! > From: Jon Gorman [mailto:jonathan.gor...@gmail.com] > Sent: Friday, January 07, 2011 7:51 AM > You'll probably think of this when you get up, but did you make sure > to import the package? ie use MARC::FILE::USMARC;? This made the other way work, too! (I had only "use MARC::File") Much thanks to Leif and Jon. -- Michael # Michael Doran, Systems Librarian # University of Texas at Arlington # 817-272-5326 office # 817-688-1926 mobile # do...@uta.edu # http://rocky.uta.edu/doran/ > -----Original Message----- > From: Leif Andersson [mailto:leif.anders...@sub.su.se] > Sent: Friday, January 07, 2011 7:50 AM > To: Doran, Michael D; perl4lib > Subject: Re: MARC blob to MARC::Record object > > Hi Michael, > > this is how I - in principle - usually do it: > > use MARC::Record; > ... > my $record = MARC::Record->new_from_usmarc( $blob ); > > /Leif > > Leif Andersson, Systems librarian > Stockholm University Library > > Från: Doran, Michael D [do...@uta.edu] > Skickat: den 7 januari 2011 00:18 > Till: perl4lib > Ämne: MARC blob to MARC::Record object > > I am working on a Perl script that retrieves data from our Voyager ILS via an > SQL query. Among other data, I have MARC records in blob form, and the script > processes one MARC record at a time. I want to be able to parse and > modify/convert the MARC record (using MARC::Record) before writing/printing > data to a file. > > How do I make the MARC blob into a MARC::Record object (without having to > first save it a file and read it in with MARC::File/Batch)? The MARC blob is > already in a variable, so it doesn't make sense (to me) to write it out to a > file just so I can read it back in. Unless I have to, natch. > > I apologize if I am missing something obvious. > > -- Michael > > # Michael Doran, Systems Librarian > # University of Texas at Arlington > # 817-272-5326 office > # 817-688-1926 mobile > # do...@uta.edu > # http://rocky.uta.edu/doran/
RE: MARC blob to MARC::Record object
Hi Leif and Jon, > use MARC::Record; > ... > my $record = MARC::Record->new_from_usmarc( $blob ); This works! > From: Jon Gorman [mailto:jonathan.gor...@gmail.com] > Sent: Friday, January 07, 2011 7:51 AM > You'll probably think of this when you get up, but did you make sure > to import the package? ie use MARC::FILE::USMARC;? This made the other way work, too! (I had only "use MARC::File") Much thanks to Leif and Jon. -- Michael # Michael Doran, Systems Librarian # University of Texas at Arlington # 817-272-5326 office # 817-688-1926 mobile # do...@uta.edu # http://rocky.uta.edu/doran/ > -Original Message- > From: Leif Andersson [mailto:leif.anders...@sub.su.se] > Sent: Friday, January 07, 2011 7:50 AM > To: Doran, Michael D; perl4lib > Subject: Re: MARC blob to MARC::Record object > > Hi Michael, > > this is how I - in principle - usually do it: > > use MARC::Record; > ... > my $record = MARC::Record->new_from_usmarc( $blob ); > > /Leif > > Leif Andersson, Systems librarian > Stockholm University Library > > Från: Doran, Michael D [do...@uta.edu] > Skickat: den 7 januari 2011 00:18 > Till: perl4lib > Ämne: MARC blob to MARC::Record object > > I am working on a Perl script that retrieves data from our Voyager ILS via an > SQL query. Among other data, I have MARC records in blob form, and the script > processes one MARC record at a time. I want to be able to parse and > modify/convert the MARC record (using MARC::Record) before writing/printing > data to a file. > > How do I make the MARC blob into a MARC::Record object (without having to > first save it a file and read it in with MARC::File/Batch)? The MARC blob is > already in a variable, so it doesn't make sense (to me) to write it out to a > file just so I can read it back in. Unless I have to, natch. > > I apologize if I am missing something obvious. > > -- Michael > > # Michael Doran, Systems Librarian > # University of Texas at Arlington > # 817-272-5326 office > # 817-688-1926 mobile > # do...@uta.edu > # http://rocky.uta.edu/doran/
Re: MARC blob to MARC::Record object
Hi Michael, this is how I - in principle - usually do it: use MARC::Record; ... my $record = MARC::Record->new_from_usmarc( $blob ); /Leif Leif Andersson, Systems librarian Stockholm University Library Från: Doran, Michael D [do...@uta.edu] Skickat: den 7 januari 2011 00:18 Till: perl4lib Ämne: MARC blob to MARC::Record object I am working on a Perl script that retrieves data from our Voyager ILS via an SQL query. Among other data, I have MARC records in blob form, and the script processes one MARC record at a time. I want to be able to parse and modify/convert the MARC record (using MARC::Record) before writing/printing data to a file. How do I make the MARC blob into a MARC::Record object (without having to first save it a file and read it in with MARC::File/Batch)? The MARC blob is already in a variable, so it doesn't make sense (to me) to write it out to a file just so I can read it back in. Unless I have to, natch. I apologize if I am missing something obvious. -- Michael # Michael Doran, Systems Librarian # University of Texas at Arlington # 817-272-5326 office # 817-688-1926 mobile # do...@uta.edu # http://rocky.uta.edu/doran/
RE: MARC blob to MARC::Record object
> How do I make the MARC blob into a MARC::Record object (without having > to first save it a file and read it in with MARC::File/Batch)? The > MARC blob is already in a variable, so it doesn't make sense (to me) to > write it out to a file just so I can read it back in. Unless I have > to, natch. > You should be able to create a MARC::Record object via MARC::File::USMARC from string (see decode). An example below http://search.cpan.org/dist/MARC-Record/lib/MARC/File/USMARC.pm So with per/Voyager I know I've done something like: Had a query ... SELECT BIB_ID, record_segment, seqnum FROM UIUDB.BIB_DATA WHERE BIB_ID = ? ORDER BY seqnum ASC; I prepare/execute that query with something like: $getBibRecordH->execute($row->{'BIBID'}) or $logger->logdie("Could not execute query to get Bib Record"); while (my ($rec_id, $recseg, $seqnum) = $getBibRecordH->fetchrow_array) { $rawMARC .= $recseg ; } my $MARC = MARC::File::USMARC->decode($rawMARC); There's also some nice ways if you're only using certain parts of the record to apply a filter, although I don't have any examples off-hand. If however you mean by the MARC blob you don't have a "complete" record but part, I'm less sure how to do that. I'd just pull in the entire file. Jon Gorman University of Illinois > > -- Michael > > # Michael Doran, Systems Librarian > # University of Texas at Arlington > # 817-272-5326 office > # 817-688-1926 mobile > # do...@uta.edu > # http://rocky.uta.edu/doran/
RE: MARC blob to MARC::Record object
Hi Jon, > You should be able to create a MARC::Record object via MARC::File::USMARC from > string (see decode). > my $MARC = MARC::File::USMARC->decode($rawMARC); This looks like what I need. However, (arggh) now I am getting this error message when I try to use that line of code: "Can't locate object method "decode" via package "MARC::File::USMARC" at spco-export.pl line 404." I'm getting tired and punchy, so will take it up again tomorrow. > If however you mean by the MARC blob you don't have a "complete" record but > part, I'm less sure how to do that. Yes, I the MARC blob is a complete record. Thanks for the help! -- Michael # Michael Doran, Systems Librarian # University of Texas at Arlington # 817-272-5326 office # 817-688-1926 mobile # do...@uta.edu # http://rocky.uta.edu/doran/ > -Original Message- > From: Gorman, Jon [mailto:jtgor...@illinois.edu] > Sent: Thursday, January 06, 2011 6:19 PM > To: Doran, Michael D; perl4lib > Subject: RE: MARC blob to MARC::Record object > > > > > How do I make the MARC blob into a MARC::Record object (without having > > to first save it a file and read it in with MARC::File/Batch)? The > > MARC blob is already in a variable, so it doesn't make sense (to me) to > > write it out to a file just so I can read it back in. Unless I have > > to, natch. > > > > You should be able to create a MARC::Record object via MARC::File::USMARC from > string (see decode). An example below > http://search.cpan.org/dist/MARC-Record/lib/MARC/File/USMARC.pm > > > So with per/Voyager I know I've done something like: > > Had a query ... > SELECT BIB_ID, record_segment, seqnum > FROM UIUDB.BIB_DATA > WHERE BIB_ID = ? > ORDER BY seqnum ASC; > > I prepare/execute that query with something like: > > $getBibRecordH->execute($row->{'BIBID'}) or $logger->logdie("Could not > execute query to get Bib Record"); > while (my ($rec_id, $recseg, $seqnum) = $getBibRecordH->fetchrow_array) { > $rawMARC .= $recseg ; > } > > > > my $MARC = MARC::File::USMARC->decode($rawMARC); > > > There's also some nice ways if you're only using certain parts of the record > to apply a filter, although I don't have any examples off-hand. > > If however you mean by the MARC blob you don't have a "complete" record but > part, I'm less sure how to do that. I'd just pull in the entire file. > > Jon Gorman > University of Illinois > > > > > > > -- Michael > > > > # Michael Doran, Systems Librarian > > # University of Texas at Arlington > > # 817-272-5326 office > > # 817-688-1926 mobile > > # do...@uta.edu > > # http://rocky.uta.edu/doran/