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

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

  ViewVC Help
Powered by ViewVC 1.1.26