--- trunk/lib/WebPAC/Normalize.pm 2005/11/12 21:31:47 39 +++ trunk/lib/WebPAC/Normalize.pm 2006/06/29 23:19:26 547 @@ -1,679 +1,480 @@ package WebPAC::Normalize; +use Exporter 'import'; +@EXPORT = qw/ + _set_rec _set_lookup + _get_ds _clean_ds + + tag search display + marc marc_indicators marc_repeatable_subfield + + rec1 rec2 rec + regex prefix suffix surround + first lookup join_with +/; use warnings; use strict; -use base 'WebPAC::Common'; + +#use base qw/WebPAC::Common/; use Data::Dumper; +use Encode qw/from_to/; =head1 NAME -WebPAC::Normalize - data mungling for normalisation +WebPAC::Normalize - describe normalisaton rules using sets =head1 VERSION -Version 0.01 +Version 0.06 =cut -our $VERSION = '0.01'; +our $VERSION = '0.06'; =head1 SYNOPSIS -This package contains code that mungle data to produce normalized format. - -It contains several assumptions: - -=over - -=item * - -format of fields is defined using C notation for repeatable fields -or C for single (or first) value, where C<123> is field number and -C is subfield. - -=item * - -source data records (C<$rec>) have unique identifiers in field C<000> - -=item * - -optional C tag at B will be -perl code that is evaluated before producing output (value of field will be -interpolated before that) +This module uses C files to perform normalisation +from input records using perl functions which are specialized for set +processing. + +Sets are implemented as arrays, and normalisation file is valid perl, which +means that you check it's validity before running WebPAC using +C. + +Normalisation can generate multiple output normalized data. For now, supported output +types (on the left side of definition) are: C, C, C and +C. -=item * +=head1 FUNCTIONS -optional C at B will apply perl -code defined as code ref on format after field substitution to producing -output +Functions which start with C<_> are private and used by WebPAC internally. +All other functions are available for use within normalisation rules. -=item * +=head2 data_structure -optional C will be then performed. See C. +Return data structure -=item * + my $ds = WebPAC::Normalize::data_structure( + lookup => $lookup->lookup_hash, + row => $row, + rules => $normalize_pl_config, + marc_encoding => 'utf-8', + ); -at end, optional Cs rules are resolved. Format rules are similar to -C and can also contain C which is performed after -values are inserted in format. +Options C, C, C and C are mandatory while all +other are optional. -=back +This function will B if normalizastion can't be evaled. -This also describes order in which transformations are applied (eval, -filter, lookup, format) which is important to undestand when deciding how to -solve your data mungling and normalisation process. +Since this function isn't exported you have to call it with +C. +=cut +sub data_structure { + my $arg = {@_}; + die "need row argument" unless ($arg->{row}); + die "need normalisation argument" unless ($arg->{rules}); -=head1 FUNCTIONS + no strict 'subs'; + _set_lookup( $arg->{lookup} ); + _set_rec( $arg->{row} ); + _clean_ds( %{ $arg } ); + eval "$arg->{rules}"; + die "error evaling $arg->{rules}: $@\n" if ($@); -=head2 new + return _get_ds(); +} -Create new normalisation object +=head2 _set_rec - my $n = new WebPAC::Normalize::Something( - filter => { - 'filter_name_1' => sub { - # filter code - return length($_); - }, ... - }, - db => $db_obj, - lookup_regex => $lookup->regex, - lookup => $lookup_obj, - ); +Set current record hash -Parametar C defines user supplied snippets of perl code which can -be use with C notation. - -Recommended parametar C is used to enable parsing of lookups -in structures. If you pass this parametar, you must also pass C -which is C object. + _set_rec( $rec ); =cut -sub new { - my $class = shift; - my $self = {@_}; - bless($self, $class); - - my $r = $self->{'lookup_regex'} ? 1 : 0; - my $l = $self->{'lookup'} ? 1 : 0; - - my $log = $self->_get_logger(); - - # those two must be in pair - if ( ($r & $l) != ($r || $l) ) { - my $log = $self->_get_logger(); - $log->logdie("lookup_regex and lookup must be in pair"); - } - - $log->logdie("lookup must be WebPAC::Lookup object") if ($self->{'lookup'} && ! $self->{'lookup'}->isa('WebPAC::Lookup')); +my $rec; - $self ? return $self : return undef; +sub _set_rec { + $rec = shift or die "no record hash"; } +=head2 _get_ds -=head2 data_structure - -Create in-memory data structure which represents normalized layout from -C. - -This structures are used to produce output. - - my @ds = $webpac->data_structure($rec); +Return hash formatted as data structure -B - -This method will also set C<< $webpac->{'currnet_filename'} >> if there is -C<< >> tag and C<< $webpac->{'headline'} >> if there is -C<< >> tag. + my $ds = _get_ds(); =cut -sub data_structure { - my $self = shift; - - my $log = $self->_get_logger(); - - my $rec = shift; - $log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o); +my ($out,$marc_record, $marc_encoding, $marc_repeatable_subfield, $marc_indicators); - my $cache_file; - - if ($self->{'db'}) { - my @ds = $self->{'db'}->load_ds($rec); - $log->debug("load_ds( rec = ", sub { Dumper($rec) }, ") = ", sub { Dumper(@ds) }); - return @ds if ($#ds > 0); - $log->debug("cache miss, creating"); - } - - undef $self->{'currnet_filename'}; - undef $self->{'headline'}; +sub _get_ds { + return $out; +} - my @sorted_tags; - if ($self->{tags_by_order}) { - @sorted_tags = @{$self->{tags_by_order}}; - } else { - @sorted_tags = sort { $self->_sort_by_order } keys %{$self->{'import_xml'}->{'indexer'}}; - $self->{tags_by_order} = \@sorted_tags; - } +=head2 _clean_ds - my @ds; +Clean data structure hash for next record - $log->debug("tags: ",sub { join(", ",@sorted_tags) }); + _clean_ds(); - foreach my $field (@sorted_tags) { +=cut - my $row; +sub _clean_ds { + my $a = {@_}; + ($out,$marc_record, $marc_encoding, $marc_repeatable_subfield, $marc_indicators) = (undef); + $marc_encoding = $a->{marc_encoding}; +} -#print "field $field [",$self->{'tag'},"] = ",Dumper($self->{'import_xml'}->{'indexer'}->{$field}->{$self->{'tag'}}); +=head2 _set_lookup - foreach my $tag (@{$self->{'import_xml'}->{'indexer'}->{$field}->{$self->{'tag'}}}) { - my $format; +Set current lookup hash - $log->logdie("expected tag HASH and got $tag") unless (ref($tag) eq 'HASH'); - $format = $tag->{'value'} || $tag->{'content'}; + _set_lookup( $lookup ); - $log->debug("format: $format"); +=cut - my @v; - if ($self->{'lookup_regex'} && $format =~ $self->{'lookup_regex'}) { - @v = $self->fill_in_to_arr($rec,$format); - } else { - @v = $self->parse_to_arr($rec,$format); - } - next if (! @v); +my $lookup; - if ($tag->{'sort'}) { - @v = $self->sort_arr(@v); - } +sub _set_lookup { + $lookup = shift; +} - # use format? - if ($tag->{'format_name'}) { - @v = map { $self->apply_format($tag->{'format_name'},$tag->{'format_delimiter'},$_) } @v; - } +=head2 _get_marc_fields - if ($field eq 'filename') { - $self->{'current_filename'} = join('',@v); - $log->debug("filename: ",$self->{'current_filename'}); - } elsif ($field eq 'headline') { - $self->{'headline'} .= join('',@v); - $log->debug("headline: ",$self->{'headline'}); - next; # don't return headline in data_structure! - } +Get all fields defined by calls to C - # delimiter will join repeatable fields - if ($tag->{'delimiter'}) { - @v = ( join($tag->{'delimiter'}, @v) ); - } + $marc->add_fields( WebPAC::Normalize:_get_marc_fields() ); - # default types - my @types = qw(display swish); - # override by type attribute - @types = ( $tag->{'type'} ) if ($tag->{'type'}); - - foreach my $type (@types) { - # append to previous line? - $log->debug("type: $type ",sub { join(" ",@v) }, $row->{'append'} || 'no append'); - if ($tag->{'append'}) { - - # I will delimit appended part with - # delimiter (or ,) - my $d = $tag->{'delimiter'}; - # default delimiter - $d ||= " "; - - my $last = pop @{$row->{$type}}; - $d = "" if (! $last); - $last .= $d . join($d, @v); - push @{$row->{$type}}, $last; - - } else { - push @{$row->{$type}}, @v; - } - } - } +We are using I which detect repeatable fields only from +sequence of field/subfield data generated by normalization. - if ($row) { - $row->{'tag'} = $field; +Repeatable field is created if there is second occurence of same subfield or +if any of indicators are different. This is sane for most cases except for +non-repeatable fields with repeatable subfields. - # TODO: name_sigular, name_plural - my $name = $self->{'import_xml'}->{'indexer'}->{$field}->{'name'}; - $row->{'name'} = $name ? $self->_x($name) : $field; - - # post-sort all values in field - if ($self->{'import_xml'}->{'indexer'}->{$field}->{'sort'}) { - $log->warn("sort at field tag not implemented"); - } +You can change behaviour of that using C. - push @ds, $row; +=cut - $log->debug("row $field: ",sub { Dumper($row) }); +sub _get_marc_fields { + my @m; + my $last; + foreach my $row (@{ $marc_record }) { + if ($last && + $last->[0] eq $row->[0] && # check if field is same + $last->[1] eq $row->[1] && # check for i1 + $last->[2] eq $row->[2] && # and for i2 + ( $last->[3] ne $row->[3] || # and subfield is different + $last->[3] eq $row->[3] && # or subfield is same, + $marc_repeatable_subfield->{ $row->[3] } # but is repeatable + ) + ) { + push @$last, ( $row->[3] , $row->[4] ); + warn "## ++ added $row->[0] ^$row->[3] to $last->[0]\n"; + next; + } elsif ($last) { + push @m, $last; } + $last = $row; } - $log->logdie("there is no current_filename defined! Do you have filename tag in conf/normalize/?.xml") unless ($self->{'current_filename'}); - - $self->{'db'}->save_ds( - ds => \@ds, - current_filename => $self->{'current_filename'}, - headline => $self->{'headline'}, - ) if ($self->{'db'}); - - $log->debug("ds: ", sub { Dumper(@ds) }); - - return @ds; + push @m, $last if ($last); + return @m; } -=head2 parse - -Perform smart parsing of string, skipping delimiters for fields which aren't -defined. It can also eval code in format starting with C and -return output or nothing depending on eval code. - - my $text = $webpac->parse($rec,'eval{"v901^a" eq "Deskriptor"}descriptor: v250^a', $i); +=head1 Functions to create C -=cut +Those functions generally have to first in your normalization file. -sub parse { - my $self = shift; +=head2 tag - my ($rec, $format_utf8, $i) = @_; +Define new tag for I and I. - return if (! $format_utf8); + tag('Title', rec('200','a') ); - my $log = $self->_get_logger(); - $log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o); +=cut - $i = 0 if (! $i); +sub tag { + my $name = shift or die "tag needs name as first argument"; + my @o = grep { defined($_) && $_ ne '' } @_; + return unless (@o); + $out->{$name}->{tag} = $name; + $out->{$name}->{search} = \@o; + $out->{$name}->{display} = \@o; +} - my $format = $self->_x($format_utf8) || $log->logconfess("can't convert '$format_utf8' from UTF-8 to ",$self->{'code_page'}); +=head2 display - my @out; +Define tag just for I - $log->debug("format: $format"); + @v = display('Title', rec('200','a') ); - my $eval_code; - # remove eval{...} from beginning - $eval_code = $1 if ($format =~ s/^eval{([^}]+)}//s); +=cut - my $filter_name; - # remove filter{...} from beginning - $filter_name = $1 if ($format =~ s/^filter{([^}]+)}//s); +sub display { + my $name = shift or die "display needs name as first argument"; + my @o = grep { defined($_) && $_ ne '' } @_; + return unless (@o); + $out->{$name}->{tag} = $name; + $out->{$name}->{display} = \@o; +} - my $prefix; - my $all_found=0; +=head2 search - while ($format =~ s/^(.*?)(v|s)(\d+)(?:\^(\w))?//s) { +Prepare values just for I - my $del = $1 || ''; - $prefix ||= $del if ($all_found == 0); + @v = search('Title', rec('200','a') ); - # repeatable index - my $r = $i; - $r = 0 if (lc("$2") eq 's'); +=cut - my $found = 0; - my $tmp = $self->get_data(\$rec,$3,$4,$r,\$found); +sub search { + my $name = shift or die "search needs name as first argument"; + my @o = grep { defined($_) && $_ ne '' } @_; + return unless (@o); + $out->{$name}->{tag} = $name; + $out->{$name}->{search} = \@o; +} - if ($found) { - push @out, $del; - push @out, $tmp; - $all_found += $found; - } - } +=head2 marc - return if (! $all_found); +Save value for MARC field - my $out = join('',@out); + marc('900','a', rec('200','a') ); - if ($out) { - # add rest of format (suffix) - $out .= $format; +=cut - # add prefix if not there - $out = $prefix . $out if ($out !~ m/^\Q$prefix\E/); +sub marc { + my $f = shift or die "marc needs field"; + die "marc field must be numer" unless ($f =~ /^\d+$/); - $log->debug("result: $out"); - } + my $sf = shift or die "marc needs subfield"; - if ($eval_code) { - my $eval = $self->fill_in($rec,$eval_code,$i) || return; - $log->debug("about to eval{$eval} format: $out"); - return if (! $self->_eval($eval)); - } - - if ($filter_name && $self->{'filter'}->{$filter_name}) { - $log->debug("about to filter{$filter_name} format: $out"); - $out = $self->{'filter'}->{$filter_name}->($out); - return unless(defined($out)); - $log->debug("filter result: $out"); + foreach (@_) { + my $v = $_; # make var read-write for Encode + next unless (defined($v) && $v !~ /^\s*$/); + from_to($v, 'iso-8859-2', $marc_encoding) if ($marc_encoding); + push @{ $marc_record }, [ + $f, + $marc_indicators->{$f}->{i1} || ' ', + $marc_indicators->{$f}->{i2} || ' ', + $sf => $v + ]; } - - return $out; } -=head2 parse_to_arr +=head2 marc_repeatable_subfield -Similar to C, but returns array of all repeatable fields +Save values for MARC repetable subfield - my @arr = $webpac->parse_to_arr($rec,'v250^a'); + marc_repeatable_subfield('910', 'z', rec('909') ); =cut -sub parse_to_arr { - my $self = shift; +sub marc_repeatable_subfield { + die "marc_repeatable_subfield need subfield!\n" unless (defined($_[1])); + $marc_repeatable_subfield->{ $_[1] }++; + marc(@_); +} - my ($rec, $format_utf8) = @_; +=head2 marc_indicators - my $log = $self->_get_logger(); +Set both indicators for MARC field - $log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o); - return if (! $format_utf8); + marc_indicators('900', ' ', 1); - my $i = 0; - my @arr; +Any indicator value other than C<0-9> will be treated as undefined. - while (my $v = $self->parse($rec,$format_utf8,$i++)) { - push @arr, $v; - } +=cut - $log->debug("format '$format_utf8' returned ",--$i," elements: ", sub { join(" | ",@arr) }) if (@arr); +sub marc_indicators { + my $f = shift || die "marc_indicators need field!\n"; + my ($i1,$i2) = @_; + die "marc_indicators($f, ...) need i1!\n" unless(defined($i1)); + die "marc_indicators($f, $i1, ...) need i2!\n" unless(defined($i2)); - return @arr; + $i1 = ' ' if ($i1 !~ /^\d$/); + $i2 = ' ' if ($i2 !~ /^\d$/); + $marc_indicators->{$f}->{i1} = $i1; + $marc_indicators->{$f}->{i2} = $i2; } -=head2 fill_in - -Workhourse of all: takes record from in-memory structure of database and -strings with placeholders and returns string or array of with substituted -values from record. +=head1 Functions to extract data from input - my $text = $webpac->fill_in($rec,'v250^a'); +This function should be used inside functions to create C described +above. -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. +=head2 rec1 - my $text = $webpac->fill_in($rec,'Title: v250^a',1); +Return all values in some field -This function B perform parsing of format to inteligenty skip -delimiters before fields which aren't used. + @v = rec1('200') -This method will automatically decode UTF-8 string to local code page -if needed. +TODO: order of values is probably same as in source data, need to investigate that =cut -sub fill_in { - my $self = shift; - - my $log = $self->_get_logger(); - - my $rec = shift || $log->logconfess("need data record"); - my $format = shift || $log->logconfess("need format to parse"); - # iteration (for repeatable fields) - my $i = shift || 0; - - $log->logdie("infitite loop in format $format") if ($i > ($self->{'max_mfn'} || 9999)); - - # FIXME remove for speedup? - $log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o); - - if (utf8::is_utf8($format)) { - $format = $self->_x($format); - } - - my $found = 0; - - my $eval_code; - # remove eval{...} from beginning - $eval_code = $1 if ($format =~ s/^eval{([^}]+)}//s); - - my $filter_name; - # remove filter{...} from beginning - $filter_name = $1 if ($format =~ s/^filter{([^}]+)}//s); - - # do actual replacement of placeholders - # repeatable fields - $format =~ s/v(\d+)(?:\^(\w))?/$self->get_data(\$rec,$1,$2,$i,\$found)/ges; - # non-repeatable fields - $format =~ s/s(\d+)(?:\^(\w))?/$self->get_data(\$rec,$1,$2,0,\$found)/ges; - - if ($found) { - $log->debug("format: $format"); - if ($eval_code) { - my $eval = $self->fill_in($rec,$eval_code,$i); - return if (! $self->_eval($eval)); - } - if ($filter_name && $self->{'filter'}->{$filter_name}) { - $log->debug("filter '$filter_name' for $format"); - $format = $self->{'filter'}->{$filter_name}->($format); - return unless(defined($format)); - $log->debug("filter result: $format"); - } - # do we have lookups? - if ($self->{'lookup'}) { - if ($self->{'lookup'}->can('lookup')) { - return $self->{'lookup'}->lookup($format); +sub rec1 { + my $f = shift; + return unless (defined($rec) && defined($rec->{$f})); + if (ref($rec->{$f}) eq 'ARRAY') { + return map { + if (ref($_) eq 'HASH') { + values %{$_}; } else { - $log->warn("Have lookup object but can't invoke lookup method"); + $_; } - } else { - return $format; - } - } else { - return; + } @{ $rec->{$f} }; + } elsif( defined($rec->{$f}) ) { + return $rec->{$f}; } } +=head2 rec2 -=head2 fill_in_to_arr +Return all values in specific field and subfield -Similar to C, but returns array of all repeatable fields. Usable -for fields which have lookups, so they shouldn't be parsed but rather -Ced. - - my @arr = $webpac->fill_in_to_arr($rec,'[v900];;[v250^a]'); + @v = rec2('200','a') =cut -sub fill_in_to_arr { - my $self = shift; +sub rec2 { + my $f = shift; + return unless (defined($rec && $rec->{$f})); + my $sf = shift; + return map { $_->{$sf} } grep { ref($_) eq 'HASH' && $_->{$sf} } @{ $rec->{$f} }; +} - my ($rec, $format_utf8) = @_; +=head2 rec - my $log = $self->_get_logger(); +syntaxtic sugar for - $log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o); - return if (! $format_utf8); + @v = rec('200') + @v = rec('200','a') - my $i = 0; - my @arr; +=cut - while (my @v = $self->fill_in($rec,$format_utf8,$i++)) { - push @arr, @v; +sub rec { + if ($#_ == 0) { + return rec1(@_); + } elsif ($#_ == 1) { + return rec2(@_); } - - $log->debug("format '$format_utf8' returned ",--$i," elements: ", sub { join(" | ",@arr) }) if (@arr); - - return @arr; } +=head2 regex -=head2 get_data - -Returns value from record. +Apply regex to some or all values - my $text = $self->get_data(\$rec,$f,$sf,$i,\$found); - -Arguments are: -record reference C<$rec>, -field C<$f>, -optional subfiled C<$sf>, -index for repeatable values C<$i>. - -Optinal variable C<$found> will be incremeted if there -is field. - -Returns value or empty string. + @v = regex( 's/foo/bar/g', @v ); =cut -sub get_data { - my $self = shift; - - my ($rec,$f,$sf,$i,$found) = @_; - - if ($$rec->{$f}) { - return '' if (! $$rec->{$f}->[$i]); - no strict 'refs'; - if ($sf && $$rec->{$f}->[$i]->{$sf}) { - $$found++ if (defined($$found)); - return $$rec->{$f}->[$i]->{$sf}; - } elsif ($$rec->{$f}->[$i]) { - $$found++ if (defined($$found)); - # it still might have subfield, just - # not specified, so we'll dump all - if ($$rec->{$f}->[$i] =~ /HASH/o) { - my $out; - foreach my $k (keys %{$$rec->{$f}->[$i]}) { - $out .= $$rec->{$f}->[$i]->{$k}." "; - } - return $out; - } else { - return $$rec->{$f}->[$i]; - } - } - } else { - return ''; +sub regex { + my $r = shift; + my @out; + #warn "r: $r\n",Dumper(\@_); + foreach my $t (@_) { + next unless ($t); + eval "\$t =~ $r"; + push @out, $t if ($t && $t ne ''); } + return @out; } +=head2 prefix -=head2 apply_format - -Apply format specified in tag with C and -C. - - my $text = $webpac->apply_format($format_name,$format_delimiter,$data); +Prefix all values with a string -Formats can contain C if you need them. + @v = prefix( 'my_', @v ); =cut -sub apply_format { - my $self = shift; - - my ($name,$delimiter,$data) = @_; - - my $log = $self->_get_logger(); - - if (! $self->{'import_xml'}->{'format'}->{$name}) { - $log->warn(" is not defined in ",$self->{'import_xml_file'}); - return $data; - } - - $log->warn("no delimiter for format $name") if (! $delimiter); +sub prefix { + my $p = shift or die "prefix needs string as first argument"; + return map { $p . $_ } grep { defined($_) } @_; +} - my $format = $self->_x($self->{'import_xml'}->{'format'}->{$name}->{'content'}) || $log->logdie("can't find format '$name'"); +=head2 suffix - my @data = split(/\Q$delimiter\E/, $data); +suffix all values with a string - my $out = sprintf($format, @data); - $log->debug("using format $name [$format] on $data to produce: $out"); + @v = suffix( '_my', @v ); - if ($self->{'lookup_regex'} && $out =~ $self->{'lookup_regex'}) { - return $self->{'lookup'}->lookup($out); - } else { - return $out; - } +=cut +sub suffix { + my $s = shift or die "suffix needs string as first argument"; + return map { $_ . $s } grep { defined($_) } @_; } -=head2 sort_arr +=head2 surround -Sort array ignoring case and html in data +surround all values with a two strings - my @sorted = $webpac->sort_arr(@unsorted); + @v = surround( 'prefix_', '_suffix', @v ); =cut -sub sort_arr { - my $self = shift; - - my $log = $self->_get_logger(); - - # FIXME add Schwartzian Transformation? - - my @sorted = sort { - $a =~ s#<[^>]+/*>##; - $b =~ s#<[^>]+/*>##; - lc($b) cmp lc($a) - } @_; - $log->debug("sorted values: ",sub { join(", ",@sorted) }); - - return @sorted; +sub surround { + my $p = shift or die "surround need prefix as first argument"; + my $s = shift or die "surround needs suffix as second argument"; + return map { $p . $_ . $s } grep { defined($_) } @_; } +=head2 first -=head1 INTERNAL METHODS - -=head2 _sort_by_order +Return first element -Sort xml tags data structure accoding to C attribute. + $v = first( @v ); =cut -sub _sort_by_order { - my $self = shift; - - my $va = $self->{'import_xml'}->{'indexer'}->{$a}->{'order'} || - $self->{'import_xml'}->{'indexer'}->{$a}; - my $vb = $self->{'import_xml'}->{'indexer'}->{$b}->{'order'} || - $self->{'import_xml'}->{'indexer'}->{$b}; - - return $va <=> $vb; +sub first { + my $r = shift; + return $r; } -=head2 _x +=head2 lookup -Convert strings from C encoding into application -specific encoding (optinally specified using C to C -constructor). +Consult lookup hashes for some value - my $text = $n->_x('normalize text string'); - -This is a stub so that other modules doesn't have to implement it. + @v = lookup( $v ); + @v = lookup( @v ); =cut -sub _x { - my $self = shift; - return shift; +sub lookup { + my $k = shift or return; + return unless (defined($lookup->{$k})); + if (ref($lookup->{$k}) eq 'ARRAY') { + return @{ $lookup->{$k} }; + } else { + return $lookup->{$k}; + } } +=head2 join_with -=head1 AUTHOR - -Dobrica Pavlinusic, C<< >> +Joins walues with some delimiter -=head1 COPYRIGHT & LICENSE - -Copyright 2005 Dobrica Pavlinusic, All Rights Reserved. - -This program is free software; you can redistribute it and/or modify it -under the same terms as Perl itself. + $v = join_with(", ", @v); =cut -1; # End of WebPAC::DB +sub join_with { + my $d = shift; + return join($d, grep { defined($_) && $_ ne '' } @_); +} + +# END +1;