/[webpac2]/trunk/lib/WebPAC/Common.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/Common.pm

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

revision 3 by dpavlin, Sat Jul 16 11:07:38 2005 UTC revision 4 by dpavlin, Sat Jul 16 12:37:18 2005 UTC
# Line 17  Version 0.01 Line 17  Version 0.01
17    
18  our $VERSION = '0.01';  our $VERSION = '0.01';
19    
20    #my $LOOKUP_REGEX = '\[[^\[\]]+\]';
21    #my $LOOKUP_REGEX_SAVE = '\[([^\[\]]+)\]';
22    my $LOOKUP_REGEX = 'lookup{[^\{\}]+}';
23    my $LOOKUP_REGEX_SAVE = 'lookup{([^\{\}]+)}';
24    
25    
26    =head1 FUNCTIONS
27    
28    =head2 fill_in
29    
30    Workhourse of all: takes record from in-memory structure of database and
31    strings with placeholders and returns string or array of with substituted
32    values from record.
33    
34     my $text = $webpac->fill_in($rec,'v250^a');
35    
36    Optional argument is ordinal number for repeatable fields. By default,
37    it's assume to be first repeatable field (fields are perl array, so first
38    element is 0).
39    Following example will read second value from repeatable field.
40    
41     my $text = $webpac->fill_in($rec,'Title: v250^a',1);
42    
43    This function B<does not> perform parsing of format to inteligenty skip
44    delimiters before fields which aren't used.
45    
46    This method will automatically decode UTF-8 string to local code page
47    if needed.
48    
49    =cut
50    
51    sub fill_in {
52            my $self = shift;
53    
54            my $log = $self->_get_logger();
55    
56            my $rec = shift || $log->logconfess("need data record");
57            my $format = shift || $log->logconfess("need format to parse");
58            # iteration (for repeatable fields)
59            my $i = shift || 0;
60    
61            $log->logdie("infitite loop in format $format") if ($i > ($self->{'max_mfn'} || 9999));
62    
63            # FIXME remove for speedup?
64            $log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o);
65    
66            if (utf8::is_utf8($format)) {
67                    $format = $self->_x($format);
68            }
69    
70            my $found = 0;
71    
72            my $eval_code;
73            # remove eval{...} from beginning
74            $eval_code = $1 if ($format =~ s/^eval{([^}]+)}//s);
75    
76            my $filter_name;
77            # remove filter{...} from beginning
78            $filter_name = $1 if ($format =~ s/^filter{([^}]+)}//s);
79    
80            # do actual replacement of placeholders
81            # repeatable fields
82            $format =~ s/v(\d+)(?:\^(\w))?/$self->get_data(\$rec,$1,$2,$i,\$found)/ges;
83            # non-repeatable fields
84            $format =~ s/s(\d+)(?:\^(\w))?/$self->get_data(\$rec,$1,$2,0,\$found)/ges;
85    
86            if ($found) {
87                    $log->debug("format: $format");
88                    if ($eval_code) {
89                            my $eval = $self->fill_in($rec,$eval_code,$i);
90                            return if (! $self->_eval($eval));
91                    }
92                    if ($filter_name && $self->{'filter'}->{$filter_name}) {
93                            $log->debug("filter '$filter_name' for $format");
94                            $format = $self->{'filter'}->{$filter_name}->($format);
95                            return unless(defined($format));
96                            $log->debug("filter result: $format");
97                    }
98                    # do we have lookups?
99                    if ($format =~ /$LOOKUP_REGEX/o) {
100                            $log->debug("format '$format' has lookup");
101                            return $self->lookup($format);
102                    } else {
103                            return $format;
104                    }
105            } else {
106                    return;
107            }
108    }
109    
110    
111    =head2 get_data
112    
113    Returns value from record.
114    
115     my $text = $self->get_data(\$rec,$f,$sf,$i,\$found);
116    
117    Arguments are:
118    record reference C<$rec>,
119    field C<$f>,
120    optional subfiled C<$sf>,
121    index for repeatable values C<$i>.
122    
123    Optinal variable C<$found> will be incremeted if there
124    is field.
125    
126    Returns value or empty string.
127    
128    =cut
129    
130    sub get_data {
131            my $self = shift;
132    
133            my ($rec,$f,$sf,$i,$found) = @_;
134    
135            if ($$rec->{$f}) {
136                    return '' if (! $$rec->{$f}->[$i]);
137                    no strict 'refs';
138                    if ($sf && $$rec->{$f}->[$i]->{$sf}) {
139                            $$found++ if (defined($$found));
140                            return $$rec->{$f}->[$i]->{$sf};
141                    } elsif ($$rec->{$f}->[$i]) {
142                            $$found++ if (defined($$found));
143                            # it still might have subfield, just
144                            # not specified, so we'll dump all
145                            if ($$rec->{$f}->[$i] =~ /HASH/o) {
146                                    my $out;
147                                    foreach my $k (keys %{$$rec->{$f}->[$i]}) {
148                                            $out .= $$rec->{$f}->[$i]->{$k}." ";
149                                    }
150                                    return $out;
151                            } else {
152                                    return $$rec->{$f}->[$i];
153                            }
154                    }
155            } else {
156                    return '';
157            }
158    }
159    
160    
161  =head1 INTERNAL METHODS  =head1 INTERNAL METHODS
162    
163  Here is a quick list of internal methods, mostly useful to turn debugging  Here is a quick list of internal methods, mostly useful to turn debugging

Legend:
Removed from v.3  
changed lines
  Added in v.4

  ViewVC Help
Powered by ViewVC 1.1.26