Re: Return values from MARC::Record
On Thursday, November 6, 2003, at 08:45 PM, Leif Andersson wrote: Assume we have a record with two 035 fields 035 -- $91234567 035 -- $a(XX)12345678 Now, this code will get the 035 $9 subfield: $subfield = eval { $record-field('035')-subfield('9') }; @subfields = eval { $record-field('035')-subfield('9') }; But this it will fail getting 035 $a $subfield = eval { $record-field('035')-subfield('a') }; @subfields = eval { $record-field('035')-subfield('a') }; I don't see how this can be. Am I missing something? Paul. -Ursprungligt meddelande- Från: Paul Hoffman [mailto:[EMAIL PROTECTED] Skickat: den 6 november 2003 22:30 Till: Leif Andersson Kopia: [EMAIL PROTECTED] Ämne: Re: Return values from MARC::Record On Thursday, November 6, 2003, at 01:14 PM, Leif Andersson wrote: With the same BAD record we try $subfield = eval { $record-field($tag)-subfield($sub) } This is the only case where we have to put the code in eval. Should MARC:: take care of the eval for us? I am beginning to think so. No, it can't. Just add your own error checking, something like this for example: my $field = $record-field($tag) || die No tag '$tag' in record; $subfield = $field-subfield($sub); MARC::Record::field has no way of knowing that your code will invoke the method 'subfield' on the value it returns. You could also do it like this if you want to keep things concise: $subfield = ($record-field($tag) || die No tag '$tag' in record)-subfield($sub); But that's getting a wee bit obfuscated. Paul. -- Paul Hoffman :: Taubman Medical Library :: Univ. of Michigan [EMAIL PROTECTED] :: [EMAIL PROTECTED] :: http://www.nkuitse.com/
Re: Return values from MARC::Record
On Friday, November 7, 2003, at 08:23 AM, Paul Hoffman wrote: On Thursday, November 6, 2003, at 08:45 PM, Leif Andersson wrote: Assume we have a record with two 035 fields 035 -- $91234567 035 -- $a(XX)12345678 Now, this code will get the 035 $9 subfield: $subfield = eval { $record-field('035')-subfield('9') }; @subfields = eval { $record-field('035')-subfield('9') }; But this it will fail getting 035 $a $subfield = eval { $record-field('035')-subfield('a') }; @subfields = eval { $record-field('035')-subfield('a') }; I don't see how this can be. Am I missing something? D'oh! Of course it fails to find the 035 $a subfield -- the call to $record-field('035') in scalar context only returns the *first* 035 field. My bad. As for the problem at hand, map and grep are your friends: my @fields = $record-field('035'); # -- list of all 035 fields my @possible_subfields = map { $_-subfield('a') } @fields; # -- list of results of calling subfield('a') on all 035 fields my @subfields = grep { defined } @possible_subfields; # -- existing 035 $a subfields only Or, more succinctly: @subfields = grep { defined } map { $_-subfield('a') } @fields; Most Perl programmers are familiar with this idiom, so I wouldn't worry about it being hard to understand. Paul. -Ursprungligt meddelande- Från: Paul Hoffman [mailto:[EMAIL PROTECTED] Skickat: den 6 november 2003 22:30 Till: Leif Andersson Kopia: [EMAIL PROTECTED] Ämne: Re: Return values from MARC::Record On Thursday, November 6, 2003, at 01:14 PM, Leif Andersson wrote: With the same BAD record we try $subfield = eval { $record-field($tag)-subfield($sub) } This is the only case where we have to put the code in eval. Should MARC:: take care of the eval for us? I am beginning to think so. No, it can't. Just add your own error checking, something like this for example: my $field = $record-field($tag) || die No tag '$tag' in record; $subfield = $field-subfield($sub); MARC::Record::field has no way of knowing that your code will invoke the method 'subfield' on the value it returns. You could also do it like this if you want to keep things concise: $subfield = ($record-field($tag) || die No tag '$tag' in record)-subfield($sub); But that's getting a wee bit obfuscated. Paul. -- Paul Hoffman :: Taubman Medical Library :: Univ. of Michigan [EMAIL PROTECTED] :: [EMAIL PROTECTED] :: http://www.nkuitse.com/ -- Paul Hoffman :: Taubman Medical Library :: Univ. of Michigan [EMAIL PROTECTED] :: [EMAIL PROTECTED] :: http://www.nkuitse.com/
Return values from MARC::Record
I think the return values from various methods in the MARC::Record distribution could be more intuitive. And also more consistent. If we have a BAD record in $record and try to perform $record-field($tag) we get 0 in return. But if you try $record-subfield($sub) we get undef. I would rather prefer undef for both. With the same BAD record we try $subfield = eval { $record-field($tag)-subfield($sub) } This is the only case where we have to put the code in eval. Should MARC:: take care of the eval for us? I am beginning to think so. At the bottom of this we have the creation of the record. What would we expect to get back from these? my $record1 = MARC::Record-new_from_usmarc( ); my $record2 = MARC::Record-new_from_usmarc( undef ); my $record3 = MARC::Record-new_from_usmarc( '' ); my $record4 = MARC::Record-new_from_usmarc( 'not a valid record' ); Currently they all provide us with a broken record object. From the three first I myself would prefer to get undef in return. That is how MARC::Batch treats the records. The $record4 is a bit more complicated. We have to decide what is a valid record in this context? The answer would be, I'd guess, if we can perform other methods on the object it is a valid record (so far). Leif == Leif Andersson, Systems Librarian Stockholm University Library SE-106 91 Stockholm SWEDEN Phone : +46 8 162769 Mobile: +46 70 6904281