--- trunk2/lib/WebPAC.pm 2004/06/17 01:44:25 366 +++ trunk2/lib/WebPAC.pm 2004/06/17 12:05:01 367 @@ -1,5 +1,8 @@ package WebPAC; +use warnings; +use strict; + use Carp; use Text::Iconv; use Config::IniFiles; @@ -97,8 +100,6 @@ If optional parametar C is set, it will read just 500 records from database in example above. -Returns number of last record read into memory (size of database, really). - C argument is an array of lookups to create. Each lookup must have C and C. Optional parametar C is perl code to evaluate before storing value in index. @@ -110,6 +111,8 @@ 'val' => 'v900' }, ] +Returns number of last record read into memory (size of database, really). + =cut sub open_isis { @@ -237,6 +240,10 @@ Create lookup from record using lookup definition. + $self->create_lookup($rec, @lookups); + +Called internally by C methods. + =cut sub create_lookup { @@ -267,7 +274,7 @@ Returns value from record. - $self->get_data(\$rec,$f,$sf,$i,\$found); + my $text = $self->get_data(\$rec,$f,$sf,$i,\$found); Arguments are: record reference C<$rec>, @@ -275,7 +282,7 @@ optional subfiled C<$sf>, index for repeatable values C<$i>. -Optinal variable C<$found> will be incremeted if thre +Optinal variable C<$found> will be incremeted if there is field. Returns value or empty string. @@ -286,7 +293,9 @@ my $self = shift; my ($rec,$f,$sf,$i,$found) = @_; + if ($$rec->{$f}) { + return '' if (! $$rec->{$f}->[$i]); if ($sf && $$rec->{$f}->[$i]->{$sf}) { $$found++ if (defined($$found)); return $$rec->{$f}->[$i]->{$sf}; @@ -315,14 +324,14 @@ strings with placeholders and returns string or array of with substituted values from record. - $webpac->fill_in($rec,'v250^a'); + my $text = $webpac->fill_in($rec,'v250^a'); Optional argument is ordinal number for repeatable fields. By default, it's assume to be first repeatable field (fields are perl array, so first element is 0). Following example will read second value from repeatable field. - $webpac->fill_in($rec,'Title: v250^a',1); + my $text = $webpac->fill_in($rec,'Title: v250^a',1); This function B perform parsing of format to inteligenty skip delimiters before fields which aren't used. @@ -369,7 +378,7 @@ Perform lookups on format supplied to it. - my $txt = $self->lookup('[v900]'); + my $text = $self->lookup('[v900]'); Lookups can be nested (like C<[d:[a:[v900]]]>). @@ -382,25 +391,21 @@ if ($tmp =~ /\[[^\[\]]+\]/o) { my @in = ( $tmp ); -#print "##lookup $tmp\n"; my @out; while (my $f = shift @in) { if ($f =~ /\[([^\[\]]+)\]/) { my $k = $1; if ($self->{'lookup'}->{$k}) { -#print "## lookup key = $k\n"; foreach my $nv (@{$self->{'lookup'}->{$k}}) { my $tmp2 = $f; $tmp2 =~ s/\[$k\]/$nv/g; push @in, $tmp2; -#print "## lookup in => $tmp2\n"; } } else { undef $f; } } elsif ($f) { push @out, $f; -#print "## lookup out => $f\n"; } } return @out; @@ -415,7 +420,7 @@ defined. It can also eval code in format starting with C and return output or nothing depending on eval code. - $webpac->parse($rec,'eval{"v901^a" eq "Deskriptor"}descriptor: v250^a', $i); + my $text = $webpac->parse($rec,'eval{"v901^a" eq "Deskriptor"}descriptor: v250^a', $i); =cut @@ -442,9 +447,7 @@ my $prefix; my $all_found=0; -#print "## $format\n"; while ($format =~ s/^(.*?)v(\d+)(?:\^(\w))*//s) { -#print "## [ $1 | $2 | $3 ] $format\n"; my $del = $1 || ''; $prefix ||= $del if ($all_found == 0); @@ -465,7 +468,7 @@ # add prefix if not there $out = $prefix . $out if ($out !~ m/^\Q$prefix\E/); - + if ($eval_code) { my $eval = $self->fill_in($rec,$eval_code,$i); return if (! eval $eval); @@ -474,6 +477,32 @@ return $out; } +=head2 parse_to_arr + +Similar to C, but returns array of all repeatable fields + + my @arr = $webpac->parse_to_arr($rec,'v250^a'); + +=cut + +sub parse_to_arr { + my $self = shift; + + my ($rec, $format_utf8) = @_; + + confess("need HASH as first argument!") if ($rec !~ /HASH/o); + return if (! $format_utf8); + + my $i = 0; + my @arr; + + while (my $v = $self->parse($rec,$format_utf8,$i++)) { + push @arr, $v; + } + + return @arr; +} + =head2 data_structure Create in-memory data structure which represents layout from C. @@ -515,23 +544,20 @@ foreach my $field (@sorted_tags) { my $row; - my $i = 0; #print "field $field [",$self->{'tag'},"] = ",Dumper($self->{'import_xml'}->{'indexer'}->{$field}->{$self->{'tag'}}); foreach my $tag (@{$self->{'import_xml'}->{'indexer'}->{$field}->{$self->{'tag'}}}) { + my @v = $self->parse_to_arr($rec,$tag->{'content'}); - my $v = $self->parse($rec,$tag->{'content'},$i); -print "## $i:",$tag->{'content'}," = ",($v || 'null'),"\n"; - - next if (!$v || $v && $v eq ''); + next if (! @v); # does tag have type? if ($tag->{'type'}) { - push @{$row->{$tag->{'type'}}}, $v; + push @{$row->{$tag->{'type'}}}, @v; } else { - push @{$row->{'display'}}, $v; - push @{$row->{'swish'}}, $v; + push @{$row->{'display'}}, @v; + push @{$row->{'swish'}}, @v; } } @@ -539,7 +565,7 @@ } - print Dumper($ds); + print "data_structure => ",Dumper($ds); }