/[webpac2]/trunk/lib/WebPAC/Normalize.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/lib/WebPAC/Normalize.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1037 by dpavlin, Mon Nov 12 11:17:19 2007 UTC revision 1210 by dpavlin, Sat May 30 15:26:25 2009 UTC
# Line 7  our @EXPORT = qw/ Line 7  our @EXPORT = qw/
7          _debug          _debug
8          _pack_subfields_hash          _pack_subfields_hash
9    
10            to
11          search_display search display sorted          search_display search display sorted
12    
13          rec1 rec2 rec          rec1 rec2 rec
# Line 20  our @EXPORT = qw/ Line 21  our @EXPORT = qw/
21          get set          get set
22          count          count
23    
24            row
25            rec_array
26    
27  /;  /;
28    
29  use warnings;  use warnings;
# Line 46  push @EXPORT, ( qw/ Line 50  push @EXPORT, ( qw/
50          marc_template          marc_template
51  /);  /);
52    
53    use Storable qw/dclone/;
54    
55  =head1 NAME  =head1 NAME
56    
57  WebPAC::Normalize - describe normalisaton rules using sets  WebPAC::Normalize - describe normalisaton rules using sets
58    
59  =cut  =cut
60    
61  our $VERSION = '0.35';  our $VERSION = '0.36';
62    
63  =head1 SYNOPSIS  =head1 SYNOPSIS
64    
# Line 147  sub _set_ds { Line 153  sub _set_ds {
153    
154  sub _get_rec { $rec };  sub _get_rec { $rec };
155    
156    sub rec_array {
157            my $d = $rec->{ $_[0] };
158            return @$d if ref($d) eq 'ARRAY';
159            die "field $_[0] not array: ",dump( $d );
160    }
161    
162  =head2 _set_config  =head2 _set_config
163    
164  Set current config hash  Set current config hash
# Line 263  sub _debug { Line 275  sub _debug {
275          warn "debug level $l",$/ if ($l > 0);          warn "debug level $l",$/ if ($l > 0);
276          $debug = $l;          $debug = $l;
277          $WebPAC::Normalize::MARC::debug = $debug;          $WebPAC::Normalize::MARC::debug = $debug;
 warn "#### MARC::debug = ",dump($WebPAC::Normalize::MARC::debug);  
278  }  }
279    
280  =head1 Functions to create C<data_structure>  =head1 Functions to create C<data_structure>
281    
282  Those functions generally have to first in your normalization file.  Those functions generally have to first in your normalization file.
283    
284    =head2 to
285    
286    Generic way to set values for some name
287    
288      to('field-name', 'name-value' => rec('200','a') );
289    
290    There are many helpers defined below which might be easier to use.
291    
292    =cut
293    
294    sub to {
295            my $type = shift or confess "need type -- BUG?";
296            my $name = shift or confess "needs name as first argument";
297            my @o = grep { defined($_) && $_ ne '' } @_;
298            return unless (@o);
299            $out->{$name}->{$type} = \@o;
300    }
301    
302  =head2 search_display  =head2 search_display
303    
304  Define output for L<search> and L<display> at the same time  Define output for L<search> and L<display> at the same time
305    
306    search_display('Title', rec('200','a') );    search_display('Title', rec('200','a') );
307    
   
308  =cut  =cut
309    
310  sub search_display {  sub search_display {
# Line 289  sub search_display { Line 317  sub search_display {
317    
318  =head2 tag  =head2 tag
319    
320  Old name for L<search_display>, but supported  Old name for L<search_display>, it will probably be removed at one point.
321    
322  =cut  =cut
323    
# Line 305  Define output just for I<display> Line 333  Define output just for I<display>
333    
334  =cut  =cut
335    
336  sub _field {  sub display { to( 'display', @_ ) }
         my $type = shift or confess "need type -- BUG?";  
         my $name = shift or confess "needs name as first argument";  
         my @o = grep { defined($_) && $_ ne '' } @_;  
         return unless (@o);  
         $out->{$name}->{$type} = \@o;  
 }  
   
 sub display { _field( 'display', @_ ) }  
337    
338  =head2 search  =head2 search
339    
# Line 323  Prepare values just for I<search> Line 343  Prepare values just for I<search>
343    
344  =cut  =cut
345    
346  sub search { _field( 'search', @_ ) }  sub search { to( 'search', @_ ) }
347    
348  =head2 sorted  =head2 sorted
349    
# Line 333  Insert into lists which will be automati Line 353  Insert into lists which will be automati
353    
354  =cut  =cut
355    
356  sub sorted { _field( 'sorted', @_ ) }  sub sorted { to( 'sorted', @_ ) }
357    
358    =head2 row
359    
360    Insert new row of data into output module
361    
362      row( column => 'foo', column2 => 'bar' );
363    
364    =cut
365    
366    use Data::Dump qw/dump/;
367    
368    sub row {
369            die "array doesn't have odd number of elements but $#_: ",dump( @_ ) if $#_ % 2 == 1;
370            my $table = shift @_;
371            push @{ $out->{'_rows'}->{$table} }, {@_};
372    }
373    
374    
375  =head1 Functions to extract data from input  =head1 Functions to extract data from input
# Line 356  sub _pack_subfields_hash { Line 391  sub _pack_subfields_hash {
391    
392          warn "## _pack_subfields_hash( ",dump(@_), " )\n" if ($debug > 1);          warn "## _pack_subfields_hash( ",dump(@_), " )\n" if ($debug > 1);
393    
394          my ($h,$include_subfields) = @_;          my ($hash,$include_subfields) = @_;
395    
396          # sanity and ease of use          # sanity and ease of use
397          return $h if (ref($h) ne 'HASH');          return $hash if (ref($hash) ne 'HASH');
398    
399            my $h = dclone( $hash );
400    
401          if ( defined($h->{subfields}) ) {          if ( defined($h->{subfields}) ) {
402                  my $sfs = delete $h->{subfields} || die "no subfields?";                  my $sfs = delete $h->{subfields} || die "no subfields?";
# Line 450  sub rec2 { Line 487  sub rec2 {
487                  } else {                  } else {
488                          $_->{$sf};                          $_->{$sf};
489                  }                  }
490          } grep { ref($_) eq 'HASH' && $_->{$sf} } @{ $rec->{$f} };          } grep { ref($_) eq 'HASH' && defined $_->{$sf} } @{ $rec->{$f} };
491  }  }
492    
493  =head2 rec  =head2 rec

Legend:
Removed from v.1037  
changed lines
  Added in v.1210

  ViewVC Help
Powered by ViewVC 1.1.26