[Bio] / FigKernelPackages / Tracer.pm Repository:
ViewVC logotype

Annotation of /FigKernelPackages/Tracer.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.40 - (view) (download) (as text)

1 : olson 1.30 #
2 :     # Copyright (c) 2003-2006 University of Chicago and Fellowship
3 :     # for Interpretations of Genomes. All Rights Reserved.
4 :     #
5 :     # This file is part of the SEED Toolkit.
6 :     #
7 :     # The SEED Toolkit is free software. You can redistribute
8 :     # it and/or modify it under the terms of the SEED Toolkit
9 :     # Public License.
10 :     #
11 :     # You should have received a copy of the SEED Toolkit Public License
12 :     # along with this program; if not write to the University of Chicago
13 :     # at info@ci.uchicago.edu or the Fellowship for Interpretation of
14 :     # Genomes at veronika@thefig.info or download a copy from
15 :     # http://www.theseed.org/LICENSE.TXT.
16 :     #
17 :    
18 : olson 1.1 package Tracer;
19 :    
20 : parrello 1.12 require Exporter;
21 :     @ISA = ('Exporter');
22 : parrello 1.37 @EXPORT = qw(Trace T TSetup QTrace Confess Cluck Min Max Assert Open OpenDir TICK StandardSetup ScriptSetup ScriptFinish Insure);
23 : parrello 1.12 @EXPORT_OK = qw(GetFile GetOptions Merge MergeOptions ParseCommand ParseRecord UnEscape Escape);
24 :     use strict;
25 :     use Carp qw(longmess croak);
26 :     use CGI;
27 :     use FIG_Config;
28 : parrello 1.9 use PageBuilder;
29 : parrello 1.21 use Digest::MD5;
30 : parrello 1.36 use File::Basename;
31 : parrello 1.37 use File::Path;
32 : olson 1.1
33 :     =head1 Tracing and Debugging Helpers
34 :    
35 :     =head2 Introduction
36 :    
37 :     This package provides simple tracing for debugging and reporting purposes. To use it simply call the
38 :     L</TSetup> method to set the options and call L</Trace> to write out trace messages. Each trace
39 : parrello 1.2 message has a I<trace level> and I<category> associated with it. In addition, the tracing package itself
40 :     has a list of categories and a single trace level set by the B<TSetup> method. Only messages whose trace
41 : olson 1.1 level is less than or equal to this package's trace level and whose category is activated will
42 : parrello 1.2 be written. Thus, a higher trace level on a message indicates that the message
43 : parrello 1.10 is less likely to be seen. A higher trace level passed to B<TSetup> means more trace messages will
44 : olson 1.1 appear. To generate a trace message, use the following syntax.
45 :    
46 :     C<< Trace($message) if T(errors => 4); >>
47 :    
48 : parrello 1.2 This statement will produce a trace message if the trace level is 4 or more and the C<errors>
49 : parrello 1.3 category is active. Note that the special category C<main> is always active, so
50 : olson 1.1
51 : parrello 1.3 C<< Trace($message) if T(main => 4); >>
52 : olson 1.1
53 :     will trace if the trace level is 4 or more.
54 :    
55 :     If the category name is the same as the package name, all you need is the number. So, if the
56 :     following call is made in the B<Sprout> package, it will appear if the C<Sprout> category is
57 :     active and the trace level is 2 or more.
58 :    
59 :     C<< Trace($message) if T(2); >>
60 :    
61 : parrello 1.10 To set up tracing, you call the L</TSetup> method. The method takes as input a trace level, a list
62 : olson 1.1 of category names, and a set of options. The trace level and list of category names are
63 :     specified as a space-delimited string. Thus
64 :    
65 :     C<< TSetup('3 errors Sprout ERDB', 'HTML'); >>
66 :    
67 : parrello 1.7 sets the trace level to 3, activates the C<errors>, C<Sprout>, and C<ERDB> categories, and
68 : parrello 1.12 specifies that messages should be output as HTML paragraphs.
69 :    
70 :     To turn on tracing for ALL categories, use an asterisk. The call below sets every category to
71 :     level 3 and writes the output to the standard error output. This sort of thing might be
72 :     useful in a CGI environment.
73 :    
74 :     C<< TSetup('3 *', 'WARN'); >>
75 : olson 1.1
76 :     In addition to HTML and file output for trace messages, you can specify that the trace messages
77 :     be queued. The messages can then be retrieved by calling the L</QTrace> method. This approach
78 :     is useful if you are building a web page. Instead of having the trace messages interspersed with
79 :     the page output, they can be gathered together and displayed at the end of the page. This makes
80 :     it easier to debug page formatting problems.
81 :    
82 : parrello 1.4 Finally, you can specify that all trace messages be emitted as warnings.
83 :    
84 : olson 1.1 The flexibility of tracing makes it superior to simple use of directives like C<die> and C<warn>.
85 :     Tracer calls can be left in the code with minimal overhead and then turned on only when needed.
86 :     Thus, debugging information is available and easily retrieved even when the application is
87 :     being used out in the field.
88 :    
89 : parrello 1.10 There is no hard and fast rule on how to use trace levels. The following is therefore only
90 :     a suggestion.
91 :    
92 :     =over 4
93 :    
94 : parrello 1.32 =item Error 0
95 : parrello 1.10
96 :     Message indicates an error that may lead to incorrect results or that has stopped the
97 :     application entirely.
98 :    
99 : parrello 1.32 =item Warning 1
100 : parrello 1.10
101 :     Message indicates something that is unexpected but that probably did not interfere
102 :     with program execution.
103 :    
104 : parrello 1.32 =item Notice 2
105 : parrello 1.10
106 :     Message indicates the beginning or end of a major task.
107 :    
108 : parrello 1.32 =item Information 3
109 : parrello 1.10
110 :     Message indicates a subtask. In the FIG system, a subtask generally relates to a single
111 :     genome. This would be a big loop that is not expected to execute more than 500 times or so.
112 :    
113 : parrello 1.32 =item Detail 4
114 : parrello 1.10
115 :     Message indicates a low-level loop iteration.
116 :    
117 :     =back
118 :    
119 : olson 1.1 =cut
120 : parrello 1.2
121 : olson 1.1 # Declare the configuration variables.
122 :    
123 : parrello 1.12 my $Destination = "NONE"; # Description of where to send the trace output.
124 :     my $TeeFlag = 0; # TRUE if output is going to a file and to the
125 :     # standard output
126 : parrello 1.3 my %Categories = ( main => 1 );
127 : parrello 1.12 # hash of active category names
128 :     my $TraceLevel = 0; # trace level; a higher trace level produces more
129 :     # messages
130 :     my @Queue = (); # queued list of trace messages.
131 : parrello 1.7 my $LastCategory = "main"; # name of the last category interrogated
132 : parrello 1.11 my $SetupCount = 0; # number of times TSetup called
133 : parrello 1.12 my $AllTrace = 0; # TRUE if we are tracing all categories.
134 : olson 1.1
135 :     =head2 Public Methods
136 :    
137 :     =head3 TSetup
138 :    
139 :     C<< TSetup($categoryList, $target); >>
140 :    
141 :     This method is used to specify the trace options. The options are stored as package data
142 :     and interrogated by the L</Trace> and L</T> methods.
143 :    
144 :     =over 4
145 :    
146 :     =item categoryList
147 :    
148 :     A string specifying the trace level and the categories to be traced, separated by spaces.
149 :     The trace level must come first.
150 :    
151 :     =item target
152 :    
153 :     The destination for the trace output. To send the trace output to a file, specify the file
154 :     name preceded by a ">" symbol. If a double symbol is used (">>"), then the data is appended
155 : parrello 1.10 to the file. Otherwise the file is cleared before tracing begins. Precede the first ">"
156 :     symbol with a C<+> to echo output to a file AND to the standard output. In addition to
157 :     sending the trace messages to a file, you can specify a special destination. C<HTML> will
158 :     cause tracing to the standard output with each line formatted as an HTML paragraph. C<TEXT>
159 : parrello 1.5 will cause tracing to the standard output as ordinary text. C<ERROR> will cause trace
160 : parrello 1.9 messages to be sent to the standard error output as ordinary text. C<QUEUE> will cause trace
161 : parrello 1.6 messages to be stored in a queue for later retrieval by the L</QTrace> method. C<WARN> will
162 : parrello 1.9 cause trace messages to be emitted as warnings using the B<warn> directive. C<NONE> will
163 : parrello 1.6 cause tracing to be suppressed.
164 : olson 1.1
165 :     =back
166 :    
167 :     =cut
168 :    
169 :     sub TSetup {
170 : parrello 1.12 # Get the parameters.
171 :     my ($categoryList, $target) = @_;
172 :     # Parse the category list.
173 :     my @categoryData = split /\s+/, $categoryList;
174 :     # Extract the trace level.
175 :     $TraceLevel = shift @categoryData;
176 :     # Presume category-based tracing until we learn otherwise.
177 :     $AllTrace = 0;
178 :     # Build the category hash. Note that if we find a "*", we turn on non-category
179 : parrello 1.33 # tracing. We must also clear away any pre-existing data.
180 : parrello 1.34 %Categories = ( main => 1 );
181 : parrello 1.12 for my $category (@categoryData) {
182 :     if ($category eq '*') {
183 :     $AllTrace = 1;
184 :     } else {
185 : parrello 1.13 $Categories{lc $category} = 1;
186 : parrello 1.12 }
187 :     }
188 :     # Now we need to process the destination information. The most important special
189 :     # cases are the single ">", which requires we clear the file first, and the
190 :     # "+" prefix which indicates a double echo.
191 :     if ($target =~ m/^\+?>>?/) {
192 :     if ($target =~ m/^\+/) {
193 :     $TeeFlag = 1;
194 :     $target = substr($target, 1);
195 :     }
196 :     if ($target =~ m/^>[^>]/) {
197 :     open TRACEFILE, $target;
198 :     print TRACEFILE Now() . " Tracing initialized.\n";
199 :     close TRACEFILE;
200 :     $Destination = ">$target";
201 :     } else {
202 :     $Destination = $target;
203 :     }
204 :     } else {
205 :     $Destination = uc($target);
206 :     }
207 :     # Increment the setup counter.
208 :     $SetupCount++;
209 : parrello 1.11 }
210 :    
211 : parrello 1.31 =head3 StandardSetup
212 :    
213 : parrello 1.36 C<< my ($options, @parameters) = StandardSetup(\@categories, \%options, $parmHelp, @ARGV); >>
214 : parrello 1.31
215 :     This method performs standard command-line parsing and tracing setup. The return
216 :     values are a hash of the command-line options and a list of the positional
217 :     parameters. Tracing is automatically set up and the command-line options are
218 :     validated.
219 :    
220 :     This is a complex method that does a lot of grunt work. The parameters can
221 :     be more easily understood, however, once they are examined individually.
222 :    
223 :     The I<categories> parameter is the most obtuse. It is a reference to a list of
224 :     special-purpose tracing categories. Most tracing categories are PERL package
225 :     names. So, for example, if you wanted to turn on tracing inside the B<Sprout>,
226 :     B<ERDB>, and B<SproutLoad> packages, you would specify the categories
227 :    
228 :     ["Sprout", "SproutLoad", "ERDB"]
229 :    
230 :     This would cause trace messages in the specified three packages to appear in
231 :     the output. There are threer special tracing categories that are automatically
232 :     handled by this method. In other words, if you used L</TSetup> you would need
233 :     to include these categories manually, but if you use this method they are turned
234 :     on automatically.
235 :    
236 :     =over 4
237 :    
238 :     =item FIG
239 :    
240 :     Turns on trace messages inside the B<FIG> package.
241 :    
242 :     =item SQL
243 :    
244 :     Traces SQL commands and activity.
245 :    
246 :     =item Tracer
247 :    
248 :     Traces error messages and call stacks.
249 :    
250 :     =back
251 :    
252 :     C<SQL> is only turned on if the C<-sql> option is specified in the command line.
253 :     The trace level is specified using the C<-trace> command-line option. For example,
254 :     the following command line for C<TransactFeatures> turns on SQL tracing and runs
255 :     all tracing at level 3.
256 :    
257 :     TransactFeatures -trace=3 -sql register ../xacts IDs.tbl
258 :    
259 :     Standard tracing is output to the standard output and echoed to the file
260 : parrello 1.38 C<trace>I<$$>C<.log> in the FIG temporary directory, where I<$$> is the
261 :     process ID. You can also specify the C<user> parameter to put a user ID
262 :     instead of a process ID in the trace file name. So, for example
263 : parrello 1.31
264 : parrello 1.35 The default trace level is 2. To get all messages, specify a trace level of 4.
265 :     For a genome-by-genome update, use 3.
266 : parrello 1.31
267 : parrello 1.38 TransactFeatures -trace=3 -sql -user=Bruce register ../xacts IDs.tbl
268 :    
269 :     would send the trace output to C<traceBruce.log> in the temporary directory.
270 :    
271 : parrello 1.31 The I<options> parameter is a reference to a hash containing the command-line
272 : parrello 1.36 options, their default values, and an explanation of what they mean. Command-line
273 :     options may be in the form of switches or keywords. In the case of a switch, the
274 :     option value is 1 if it is specified and 0 if it is not specified. In the case
275 :     of a keyword, the value is separated from the option name by an equal sign. You
276 :     can see this last in the command-line example above.
277 : parrello 1.31
278 :     An example at this point would help. Consider, for example, the command-line utility
279 :     C<TransactFeatures>. It accepts a list of positional parameters plus the options
280 :     C<safe>, C<noAlias>, C<start>, and C<tblFiles>. To start up this command, we execute
281 :     the following code.
282 :    
283 :     my ($options, @parameters) = Tracer::StandardSetup(["DocUtils"],
284 : parrello 1.36 { safe => [0, "use database transactions"],
285 :     noAlias => [0, "do not expect aliases in CHANGE transactions"],
286 :     start => [' ', "start with this genome"],
287 :     tblFiles => [0, "output TBL files containing the corrected IDs"] },
288 :     "command transactionDirectory IDfile",
289 :     @ARGV);
290 : parrello 1.31
291 :    
292 :     The call to C<ParseCommand> specifies the default values for the options and
293 :     stores the actual options in a hash that is returned as C<$options>. The
294 :     positional parameters are returned in C<@parameters>.
295 :    
296 :     The following is a sample command line for C<TransactFeatures>.
297 :    
298 :     TransactFeatures -trace=2 -noAlias register ../xacts IDs.tbl
299 :    
300 :     In this case, C<register>, C<../xacts>, and C<IDs.tbl> are the positional
301 :     parameters, and would find themselves in I<@parameters> after executing the
302 :     above code fragment. The tracing would be set to level 2, and the categories
303 :     would be C<FIG>, C<Tracer>, and <DocUtils>. C<FIG> and C<Tracer> are standard,
304 :     and C<DocUtils> was included because it came in within the first parameter
305 :     to this method. The I<$options> hash would be
306 :    
307 :     { trace => 2, sql => 0, safe => 0,
308 :     noAlias => 1, start => ' ', tblFiles => 0 }
309 :    
310 :     Use of C<StandardSetup> in this way provides a simple way of performing
311 :     standard tracing setup and command-line parsing. Note that the caller is
312 :     not even aware of the command-line switches C<-trace> and C<-sql>, which
313 :     are used by this method to control the tracing. If additional tracing features
314 :     need to be added in the future, they can be processed by this method without
315 :     upsetting the command-line utilities.
316 :    
317 : parrello 1.36 Finally, if the special option C<-h> is specified, the option names will
318 :     be traced at level 0 and the program will exit without processing.
319 :     This provides a limited help capability. For example, if the user enters
320 :    
321 :     TransactFeatures -h
322 :    
323 :     he would see the following output.
324 :    
325 :     TransactFeatures [options] command transactionDirectory IDfile
326 :     -trace tracing level (default 2)
327 :     -sql trace SQL commands
328 :     -safe use database transactions
329 :     -noAlias do not expect aliases in CHANGE transactions
330 :     -start start with this genome
331 :     -tblFiles output TBL files containing the corrected IDs
332 :    
333 : parrello 1.31 The parameters to this method are as follows.
334 :    
335 :     =over 4
336 :    
337 :     =item categories
338 :    
339 :     Reference to a list of tracing category names. These should be names of
340 :     packages whose internal workings will need to be debugged to get the
341 :     command working.
342 :    
343 :     =item options
344 :    
345 :     Reference to a hash containing the legal options for the current command mapped
346 : parrello 1.36 to their default values and descriptions. The user can override the defaults
347 :     by specifying the options as command-line switches prefixed by a hyphen.
348 :     Tracing-related options may be added to this hash. If the C<-h> option is
349 :     specified on the command line, the option descriptions will be used to
350 :     explain the options.
351 :    
352 :     =item parmHelp
353 :    
354 :     A string that vaguely describes the positional parameters. This is used
355 :     if the user specifies the C<-h> option.
356 : parrello 1.31
357 :     =item ARGV
358 :    
359 :     List of command line parameters, including the option switches, which must
360 :     precede the positional parameters and be prefixed by a hyphen.
361 :    
362 :     =item RETURN
363 :    
364 :     Returns a list. The first element of the list is the reference to a hash that
365 :     maps the command-line option switches to their values. These will either be the
366 :     default values or overrides specified on the command line. The remaining
367 :     elements of the list are the position parameters, in order.
368 :    
369 :     =back
370 :    
371 :     =cut
372 :    
373 :     sub StandardSetup {
374 :     # Get the parameters.
375 : parrello 1.36 my ($categories, $options, $parmHelp, @argv) = @_;
376 : parrello 1.31 # Add the tracing options.
377 : parrello 1.36 $options->{trace} = [2, "tracing level"];
378 :     $options->{sql} = [0, "turn on SQL tracing"];
379 :     $options->{h} = [0, "display command-line options"];
380 : parrello 1.38 $options->{user} = [$$, "trace log file name suffix"];
381 : parrello 1.36 # Create a parsing hash from the options hash. The parsing hash
382 :     # contains the default values rather than the default value
383 :     # and the description. While we're at it, we'll memorize the
384 :     # length of the longest option name.
385 :     my $longestName = 0;
386 :     my %parseOptions = ();
387 :     for my $key (keys %{$options}) {
388 :     if (length $key > $longestName) {
389 :     $longestName = length $key;
390 :     }
391 :     $parseOptions{$key} = $options->{$key}->[0];
392 :     }
393 : parrello 1.31 # Parse the command line.
394 : parrello 1.36 my ($retOptions, @retParameters) = ParseCommand(\%parseOptions, @argv);
395 : parrello 1.31 # Now we want to set up tracing. First, we need to know if SQL is to
396 :     # be traced.
397 :     my @cats = @{$categories};
398 :     if ($retOptions->{sql}) {
399 :     push @cats, "SQL";
400 :     }
401 :     # Add the default categories.
402 :     push @cats, "Tracer", "FIG";
403 :     # Next, we create the category string by prefixing the trace level
404 :     # and joining the categories.
405 : parrello 1.36 my $cats = join(" ", $parseOptions{trace}, @cats);
406 : parrello 1.40 # Verify that we can open a file in the temporary directory.
407 :     my $traceMode = "TEXT";
408 :     my $suffix = $retOptions->{user};
409 :     my $traceFileName = "$FIG_Config::temp/trace$suffix.log";
410 :     if (open TESTTRACE, ">$traceFileName") {
411 :     $traceMode = "+>$traceFileName";
412 :     close TESTTRACE;
413 :     }
414 : parrello 1.31 # Now set up the tracing.
415 : parrello 1.40 TSetup($cats, $traceMode);
416 : parrello 1.36 # Check for the "h" option. If it is specified, dump the command-line
417 :     # options and exit the program.
418 :     if ($retOptions->{h}) {
419 :     $0 =~ m#[/\\](\w+)(\.pl)?$#i;
420 :     Trace("$1 [options] $parmHelp") if T(0);
421 :     for my $key (sort keys %{$options}) {
422 :     my $name = Pad($key, $longestName, 0, ' ');
423 :     my $desc = $options->{$key}->[1];
424 :     if ($options->{$key}->[0]) {
425 :     $desc .= " (default " . $options->{$key}->[0] . ")";
426 :     }
427 :     Trace(" $name $desc") if T(0);
428 :     }
429 :     exit(0);
430 :     }
431 : parrello 1.31 # Return the parsed parameters.
432 :     return ($retOptions, @retParameters);
433 :     }
434 :    
435 : parrello 1.11 =head3 Setups
436 :    
437 :     C<< my $count = Tracer::Setups(); >>
438 :    
439 :     Return the number of times L</TSetup> has been called.
440 :    
441 :     This method allows for the creation of conditional tracing setups where, for example, we
442 :     may want to set up tracing if nobody else has done it before us.
443 :    
444 :     =cut
445 :    
446 :     sub Setups {
447 : parrello 1.12 return $SetupCount;
448 : olson 1.1 }
449 :    
450 : parrello 1.10 =head3 Open
451 :    
452 :     C<< my $handle = Open($fileHandle, $fileSpec, $message); >>
453 :    
454 : parrello 1.11 Open a file.
455 : parrello 1.10
456 :     The I<$fileSpec> is essentially the second argument of the PERL C<open>
457 :     function. The mode is specified using Unix-like shell information. So, for
458 :     example,
459 :    
460 : parrello 1.12 Open(\*LOGFILE, '>>/usr/spool/news/twitlog', "Could not open twit log.");
461 : parrello 1.10
462 :     would open for output appended to the specified file, and
463 :    
464 : parrello 1.12 Open(\*DATASTREAM, "| sort -u >$outputFile", "Could not open $outputFile.");
465 : parrello 1.10
466 :     would open a pipe that sorts the records written and removes duplicates. Note
467 : parrello 1.11 the use of file handle syntax in the Open call. To use anonymous file handles,
468 :     code as follows.
469 : parrello 1.10
470 : parrello 1.12 my $logFile = Open(undef, '>>/usr/spool/news/twitlog', "Could not open twit log.");
471 : parrello 1.10
472 : parrello 1.11 The I<$message> parameter is used if the open fails. If it is set to C<0>, then
473 :     the open returns TRUE if successful and FALSE if an error occurred. Otherwise, a
474 :     failed open will throw an exception and the third parameter will be used to construct
475 :     an error message. If the parameter is omitted, a standard message is constructed
476 :     using the file spec.
477 : parrello 1.10
478 : parrello 1.12 Could not open "/usr/spool/news/twitlog"
479 : parrello 1.10
480 :     Note that the mode characters are automatically cleaned from the file name.
481 :     The actual error message from the file system will be captured and appended to the
482 :     message in any case.
483 :    
484 : parrello 1.12 Could not open "/usr/spool/news/twitlog": file not found.
485 : parrello 1.10
486 :     In some versions of PERL the only error message we get is a number, which
487 :     corresponds to the C++ C<errno> value.
488 :    
489 : parrello 1.12 Could not open "/usr/spool/news/twitlog": 6.
490 : parrello 1.10
491 :     =over 4
492 :    
493 :     =item fileHandle
494 :    
495 :     File handle. If this parameter is C<undef>, a file handle will be generated
496 :     and returned as the value of this method.
497 :    
498 :     =item fileSpec
499 :    
500 :     File name and mode, as per the PERL C<open> function.
501 :    
502 :     =item message (optional)
503 :    
504 :     Error message to use if the open fails. If omitted, a standard error message
505 :     will be generated. In either case, the error information from the file system
506 : parrello 1.11 is appended to the message. To specify a conditional open that does not throw
507 :     an error if it fails, use C<0>.
508 : parrello 1.10
509 :     =item RETURN
510 :    
511 : parrello 1.11 Returns the name of the file handle assigned to the file, or C<undef> if the
512 :     open failed.
513 : parrello 1.10
514 :     =back
515 :    
516 :     =cut
517 :    
518 :     sub Open {
519 : parrello 1.12 # Get the parameters.
520 :     my ($fileHandle, $fileSpec, $message) = @_;
521 :     # Attempt to open the file.
522 :     my $rv = open $fileHandle, $fileSpec;
523 :     # If the open failed, generate an error message.
524 :     if (! $rv) {
525 :     # Save the system error message.
526 :     my $sysMessage = $!;
527 :     # See if we need a default message.
528 :     if (!$message) {
529 :     # Clean any obvious mode characters and leading spaces from the
530 :     # filename.
531 :     my ($fileName) = FindNamePart($fileSpec);
532 :     $message = "Could not open \"$fileName\"";
533 :     }
534 :     # Terminate with an error using the supplied message and the
535 :     # error message from the file system.
536 :     Confess("$message: $!");
537 :     }
538 :     # Return the file handle.
539 :     return $fileHandle;
540 : parrello 1.10 }
541 :    
542 : parrello 1.11 =head3 FindNamePart
543 :    
544 :     C<< my ($fileName, $start, $len) = Tracer::FindNamePart($fileSpec); >>
545 :    
546 :     Extract the portion of a file specification that contains the file name.
547 :    
548 :     A file specification is the string passed to an C<open> call. It specifies the file
549 :     mode and name. In a truly complex situation, it can specify a pipe sequence. This
550 :     method assumes that the file name is whatever follows the first angle bracket
551 :     sequence. So, for example, in the following strings the file name is
552 :     C</usr/fig/myfile.txt>.
553 :    
554 :     >>/usr/fig/myfile.txt
555 :     </usr/fig/myfile.txt
556 :     | sort -u > /usr/fig/myfile.txt
557 :    
558 :     If the method cannot find a file name using its normal methods, it will return the
559 :     whole incoming string.
560 :    
561 :     =over 4
562 :    
563 :     =item fileSpec
564 :    
565 :     File specification string from which the file name is to be extracted.
566 :    
567 :     =item RETURN
568 :    
569 :     Returns a three-element list. The first element contains the file name portion of
570 :     the specified string, or the whole string if a file name cannot be found via normal
571 :     methods. The second element contains the start position of the file name portion and
572 :     the third element contains the length.
573 :    
574 :     =back
575 :    
576 :     =cut
577 :     #: Return Type $;
578 :     sub FindNamePart {
579 :     # Get the parameters.
580 :     my ($fileSpec) = @_;
581 : parrello 1.12 # Default to the whole input string.
582 :     my ($retVal, $pos, $len) = ($fileSpec, 0, length $fileSpec);
583 : parrello 1.11 # Parse out the file name if we can.
584 : parrello 1.12 if ($fileSpec =~ m/(<|>>?)(.+?)(\s*)$/) {
585 :     $retVal = $2;
586 :     $len = length $retVal;
587 :     $pos = (length $fileSpec) - (length $3) - $len;
588 :     }
589 : parrello 1.11 # Return the result.
590 :     return ($retVal, $pos, $len);
591 :     }
592 :    
593 :     =head3 OpenDir
594 :    
595 : parrello 1.31 C<< my @files = OpenDir($dirName, $filtered, $flag); >>
596 : parrello 1.11
597 :     Open a directory and return all the file names. This function essentially performs
598 :     the functions of an C<opendir> and C<readdir>. If the I<$filtered> parameter is
599 : parrello 1.31 set to TRUE, all filenames beginning with a period (C<.>), dollar sign (C<$>),
600 : parrello 1.33 or pound sign (C<#>) and all filenames ending with a tilde C<~>) will be
601 :     filtered out of the return list. If the directory does not open and I<$flag> is not
602 :     set, an exception is thrown. So, for example,
603 : parrello 1.11
604 : parrello 1.12 my @files = OpenDir("/Volumes/fig/contigs", 1);
605 : parrello 1.29
606 : parrello 1.11 is effectively the same as
607 :    
608 : parrello 1.12 opendir(TMP, "/Volumes/fig/contigs") || Confess("Could not open /Volumes/fig/contigs.");
609 : parrello 1.33 my @files = grep { $_ !~ /^[\.\$\#]/ && $_ !~ /~$/ } readdir(TMP);
610 : parrello 1.11
611 :     Similarly, the following code
612 :    
613 : parrello 1.31 my @files = grep { $_ =~ /^\d/ } OpenDir("/Volumes/fig/orgs", 0, 1);
614 : parrello 1.29
615 : parrello 1.11 Returns the names of all files in C</Volumes/fig/orgs> that begin with digits and
616 : parrello 1.31 automatically returns an empty list if the directory fails to open.
617 : parrello 1.11
618 :     =over 4
619 :    
620 :     =item dirName
621 :    
622 :     Name of the directory to open.
623 :    
624 :     =item filtered
625 :    
626 :     TRUE if files whose names begin with a period (C<.>) should be automatically removed
627 :     from the list, else FALSE.
628 :    
629 : parrello 1.31 =item flag
630 :    
631 :     TRUE if a failure to open is okay, else FALSE
632 :    
633 : parrello 1.11 =back
634 :    
635 :     =cut
636 :     #: Return Type @;
637 :     sub OpenDir {
638 :     # Get the parameters.
639 : parrello 1.31 my ($dirName, $filtered, $flag) = @_;
640 : parrello 1.11 # Declare the return variable.
641 : parrello 1.31 my @retVal = ();
642 : parrello 1.12 # Open the directory.
643 :     if (opendir(my $dirHandle, $dirName)) {
644 :     # The directory opened successfully. Get the appropriate list according to the
645 :     # strictures of the filter parameter.
646 :     if ($filtered) {
647 : parrello 1.33 @retVal = grep { $_ !~ /^[\.\$\#]/ && $_ !~ /~$/ } readdir $dirHandle;
648 : parrello 1.12 } else {
649 :     @retVal = readdir $dirHandle;
650 :     }
651 : parrello 1.31 } elsif (! $flag) {
652 :     # Here the directory would not open and it's considered an error.
653 : parrello 1.12 Confess("Could not open directory $dirName.");
654 :     }
655 : parrello 1.11 # Return the result.
656 :     return @retVal;
657 :     }
658 :    
659 : parrello 1.6 =head3 SetLevel
660 :    
661 :     C<< Tracer::SetLevel($newLevel); >>
662 :    
663 :     Modify the trace level. A higher trace level will cause more messages to appear.
664 :    
665 :     =over 4
666 :    
667 :     =item newLevel
668 :    
669 :     Proposed new trace level.
670 :    
671 :     =back
672 :    
673 :     =cut
674 :    
675 :     sub SetLevel {
676 :     $TraceLevel = $_[0];
677 :     }
678 :    
679 : olson 1.1 =head3 Now
680 :    
681 :     C<< my $string = Tracer::Now(); >>
682 :    
683 :     Return a displayable time stamp containing the local time.
684 :    
685 :     =cut
686 :    
687 :     sub Now {
688 : parrello 1.12 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
689 :     my $retVal = _p2($mon+1) . "/" . _p2($mday) . "/" . ($year + 1900) . " " .
690 :     _p2($hour) . ":" . _p2($min) . ":" . _p2($sec);
691 :     return $retVal;
692 : olson 1.1 }
693 :    
694 :     # Pad a number to 2 digits.
695 :     sub _p2 {
696 : parrello 1.12 my ($value) = @_;
697 :     $value = "0$value" if ($value < 10);
698 :     return $value;
699 : olson 1.1 }
700 :    
701 :     =head3 LogErrors
702 :    
703 :     C<< Tracer::LogErrors($fileName); >>
704 :    
705 :     Route the standard error output to a log file.
706 :    
707 :     =over 4
708 :    
709 :     =item fileName
710 :    
711 :     Name of the file to receive the error output.
712 :    
713 :     =back
714 :    
715 :     =cut
716 :    
717 :     sub LogErrors {
718 : parrello 1.12 # Get the file name.
719 :     my ($fileName) = @_;
720 :     # Open the file as the standard error output.
721 :     open STDERR, '>', $fileName;
722 : olson 1.1 }
723 :    
724 : parrello 1.5 =head3 ReadOptions
725 :    
726 :     C<< my %options = Tracer::ReadOptions($fileName); >>
727 :    
728 :     Read a set of options from a file. Each option is encoded in a line of text that has the
729 :     format
730 :    
731 :     I<optionName>C<=>I<optionValue>C<; >I<comment>
732 :    
733 :     The option name must consist entirely of letters, digits, and the punctuation characters
734 : parrello 1.9 C<.> and C<_>, and is case sensitive. Blank lines and lines in which the first nonblank
735 :     character is a semi-colon will be ignored. The return hash will map each option name to
736 : parrello 1.5 the corresponding option value.
737 :    
738 :     =over 4
739 :    
740 :     =item fileName
741 :    
742 :     Name of the file containing the option data.
743 :    
744 :     =item RETURN
745 :    
746 :     Returns a hash mapping the option names specified in the file to their corresponding option
747 :     value.
748 :    
749 :     =back
750 :    
751 :     =cut
752 :    
753 :     sub ReadOptions {
754 : parrello 1.12 # Get the parameters.
755 :     my ($fileName) = @_;
756 :     # Open the file.
757 :     (open CONFIGFILE, "<$fileName") || Confess("Could not open option file $fileName.");
758 :     # Count the number of records read.
759 :     my ($records, $comments) = 0;
760 :     # Create the return hash.
761 :     my %retVal = ();
762 :     # Loop through the file, accumulating key-value pairs.
763 :     while (my $line = <CONFIGFILE>) {
764 :     # Denote we've read a line.
765 :     $records++;
766 :     # Determine the line type.
767 :     if ($line =~ /^\s*[\n\r]/) {
768 :     # A blank line is a comment.
769 :     $comments++;
770 :     } elsif ($line =~ /^\s*([A-Za-z0-9_\.]+)=([^;]*);/) {
771 :     # Here we have an option assignment.
772 :     retVal{$1} = $2;
773 :     } elsif ($line =~ /^\s*;/) {
774 :     # Here we have a text comment.
775 :     $comments++;
776 :     } else {
777 :     # Here we have an invalid line.
778 :     Trace("Invalid option statement in record $records.") if T(0);
779 :     }
780 :     }
781 :     # Return the hash created.
782 :     return %retVal;
783 : parrello 1.5 }
784 :    
785 : olson 1.1 =head3 GetOptions
786 :    
787 :     C<< Tracer::GetOptions(\%defaults, \%options); >>
788 :    
789 :     Merge a specified set of options into a table of defaults. This method takes two hash references
790 :     as input and uses the data from the second to update the first. If the second does not exist,
791 :     there will be no effect. An error will be thrown if one of the entries in the second hash does not
792 :     exist in the first.
793 :    
794 :     Consider the following example.
795 :    
796 :     C<< my $optionTable = GetOptions({ dbType => 'mySQL', trace => 0 }, $options); >>
797 :    
798 :     In this example, the variable B<$options> is expected to contain at most two options-- B<dbType> and
799 :     B<trace>. The default database type is C<mySQL> and the default trace level is C<0>. If the value of
800 :     B<$options> is C<< {dbType => 'Oracle'} >>, then the database type will be changed to C<Oracle> and
801 :     the trace level will remain at 0. If B<$options> is undefined, then the database type and trace level
802 :     will remain C<mySQL> and C<0>. If, on the other hand, B<$options> is defined as
803 :    
804 :     C<< {databaseType => 'Oracle'} >>
805 :    
806 :     an error will occur because the B<databaseType> option does not exist.
807 :    
808 :     =over 4
809 :    
810 :     =item defaults
811 :    
812 :     Table of default option values.
813 :    
814 :     =item options
815 :    
816 :     Table of overrides, if any.
817 :    
818 :     =item RETURN
819 :    
820 :     Returns a reference to the default table passed in as the first parameter.
821 :    
822 :     =back
823 :    
824 :     =cut
825 :    
826 :     sub GetOptions {
827 : parrello 1.12 # Get the parameters.
828 :     my ($defaults, $options) = @_;
829 :     # Check for overrides.
830 :     if ($options) {
831 :     # Loop through the overrides.
832 :     while (my ($option, $setting) = each %{$options}) {
833 :     # Insure this override exists.
834 :     if (!exists $defaults->{$option}) {
835 :     croak "Unrecognized option $option encountered.";
836 :     } else {
837 :     # Apply the override.
838 :     $defaults->{$option} = $setting;
839 :     }
840 :     }
841 :     }
842 :     # Return the merged table.
843 :     return $defaults;
844 : olson 1.1 }
845 :    
846 :     =head3 MergeOptions
847 :    
848 :     C<< Tracer::MergeOptions(\%table, \%defaults); >>
849 :    
850 :     Merge default values into a hash table. This method looks at the key-value pairs in the
851 :     second (default) hash, and if a matching key is not found in the first hash, the default
852 :     pair is copied in. The process is similar to L</GetOptions>, but there is no error-
853 :     checking and no return value.
854 :    
855 :     =over 4
856 :    
857 :     =item table
858 :    
859 :     Hash table to be updated with the default values.
860 :    
861 :     =item defaults
862 :    
863 :     Default values to be merged into the first hash table if they are not already present.
864 :    
865 :     =back
866 :    
867 :     =cut
868 :    
869 :     sub MergeOptions {
870 : parrello 1.12 # Get the parameters.
871 :     my ($table, $defaults) = @_;
872 :     # Loop through the defaults.
873 :     while (my ($key, $value) = each %{$defaults}) {
874 :     if (!exists $table->{$key}) {
875 :     $table->{$key} = $value;
876 :     }
877 :     }
878 : olson 1.1 }
879 :    
880 :     =head3 Trace
881 :    
882 :     C<< Trace($message); >>
883 :    
884 :     Write a trace message to the target location specified in L</TSetup>. If there has not been
885 :     any prior call to B<TSetup>.
886 :    
887 :     =over 4
888 :    
889 :     =item message
890 :    
891 :     Message to write.
892 :    
893 :     =back
894 :    
895 :     =cut
896 :    
897 :     sub Trace {
898 : parrello 1.12 # Get the parameters.
899 :     my ($message) = @_;
900 :     # Get the timestamp.
901 :     my $timeStamp = Now();
902 :     # Format the message. Note we strip off any line terminators at the end.
903 :     my $formatted = "$timeStamp <$LastCategory>: " . Strip($message);
904 :     # Process according to the destination.
905 :     if ($Destination eq "TEXT") {
906 :     # Write the message to the standard output.
907 :     print "$formatted\n";
908 :     } elsif ($Destination eq "ERROR") {
909 :     # Write the message to the error output.
910 :     print STDERR "$formatted\n";
911 :     } elsif ($Destination eq "QUEUE") {
912 :     # Push the message into the queue.
913 :     push @Queue, "$formatted";
914 :     } elsif ($Destination eq "HTML") {
915 :     # Convert the message to HTML and write it to the standard output.
916 :     my $escapedMessage = CGI::escapeHTML($message);
917 :     print "<p>$formatted</p>\n";
918 : parrello 1.4 } elsif ($Destination eq "WARN") {
919 :     # Emit the message as a warning.
920 :     warn $message;
921 : parrello 1.12 } elsif ($Destination =~ m/^>>/) {
922 :     # Write the trace message to an output file.
923 : parrello 1.14 (open TRACING, $Destination) || die "Tracing open for \"$Destination\" failed: $!";
924 : parrello 1.12 print TRACING "$formatted\n";
925 :     close TRACING;
926 :     # If the Tee flag is on, echo it to the standard output.
927 :     if ($TeeFlag) {
928 :     print "$formatted\n";
929 :     }
930 :     }
931 : olson 1.1 }
932 :    
933 :     =head3 T
934 :    
935 : parrello 1.2 C<< my $switch = T($category, $traceLevel); >>
936 : olson 1.1
937 : parrello 1.12 or
938 : parrello 1.2
939 : olson 1.1 C<< my $switch = T($traceLevel); >>
940 :    
941 :     Return TRUE if the trace level is at or above a specified value and the specified category
942 :     is active, else FALSE. If no category is specified, the caller's package name is used.
943 :    
944 :     =over 4
945 :    
946 :     =item category
947 :    
948 :     Category to which the message belongs. If not specified, the caller's package name is
949 :     used.
950 :    
951 :     =item traceLevel
952 :    
953 :     Relevant tracing level.
954 :    
955 :     =item RETURN
956 :    
957 :     TRUE if a message at the specified trace level would appear in the trace, else FALSE.
958 :    
959 :     =back
960 :    
961 :     =cut
962 :    
963 :     sub T {
964 : parrello 1.12 # Declare the return variable.
965 :     my $retVal = 0;
966 :     # Only proceed if tracing is turned on.
967 :     if ($Destination ne "NONE") {
968 :     # Get the parameters.
969 :     my ($category, $traceLevel) = @_;
970 :     if (!defined $traceLevel) {
971 :     # Here we have no category, so we need to get the calling package.
972 :     # The calling package is normally the first parameter. If it is
973 :     # omitted, the first parameter will be the tracelevel. So, the
974 :     # first thing we do is shift the so-called category into the
975 :     # $traceLevel variable where it belongs.
976 :     $traceLevel = $category;
977 :     my ($package, $fileName, $line) = caller;
978 : parrello 1.3 # If there is no calling package, we default to "main".
979 : parrello 1.12 if (!$package) {
980 : parrello 1.3 $category = "main";
981 : parrello 1.12 } else {
982 :     $category = $package;
983 :     }
984 :     }
985 : parrello 1.7 # Save the category name.
986 :     $LastCategory = $category;
987 : parrello 1.13 # Convert it to lower case before we hash it.
988 :     $category = lc $category;
989 : parrello 1.12 # Use the category and tracelevel to compute the result.
990 : parrello 1.36 if (ref $traceLevel) {
991 :     Confess("Bad trace level.");
992 :     } elsif (ref $TraceLevel) {
993 :     Confess("Bad trace config.");
994 :     }
995 : parrello 1.12 $retVal = ($traceLevel <= $TraceLevel && ($AllTrace || exists $Categories{$category}));
996 : parrello 1.3 }
997 : parrello 1.12 # Return the computed result.
998 : parrello 1.3 return $retVal;
999 : olson 1.1 }
1000 :    
1001 :     =head3 ParseCommand
1002 :    
1003 :     C<< my ($options, @arguments) = Tracer::ParseCommand(\%optionTable, @inputList); >>
1004 :    
1005 :     Parse a command line consisting of a list of parameters. The initial parameters may be option
1006 : parrello 1.2 specifiers of the form C<->I<option> or C<->I<option>C<=>I<value>. The options are stripped
1007 :     off and merged into a table of default options. The remainder of the command line is
1008 : olson 1.1 returned as a list of positional arguments. For example, consider the following invocation.
1009 :    
1010 :     C<< my ($options, @arguments) = ParseCommand({ errors => 0, logFile => 'trace.log'}, @words); >>
1011 :    
1012 :     In this case, the list @words will be treated as a command line. There are two options available,
1013 :     B<errors> and B<logFile>. If @words has the following format
1014 :    
1015 :     C<< -logFile=error.log apple orange rutabaga >>
1016 :    
1017 :     then at the end of the invocation, C<$options> will be
1018 :    
1019 :     C<< { errors => 0, logFile => 'error.log' } >>
1020 :    
1021 :     and C<@arguments> will contain
1022 :    
1023 :     C<< apple orange rutabaga >>
1024 :    
1025 : parrello 1.2 The parser allows for some escape sequences. See L</UnEscape> for a description. There is no
1026 : olson 1.1 support for quote characters.
1027 :    
1028 :     =over 4
1029 :    
1030 :     =item optionTable
1031 :    
1032 :     Table of default options.
1033 :    
1034 :     =item inputList
1035 :    
1036 :     List of words on the command line.
1037 :    
1038 :     =item RETURN
1039 :    
1040 :     Returns a reference to the option table and a list of the positional arguments.
1041 :    
1042 :     =back
1043 :    
1044 :     =cut
1045 :    
1046 :     sub ParseCommand {
1047 : parrello 1.12 # Get the parameters.
1048 :     my ($optionTable, @inputList) = @_;
1049 :     # Process any options in the input list.
1050 :     my %overrides = ();
1051 :     while ((@inputList > 0) && ($inputList[0] =~ /^-/)) {
1052 :     # Get the current option.
1053 :     my $arg = shift @inputList;
1054 :     # Pull out the option name.
1055 :     $arg =~ /^-([^=]*)/g;
1056 :     my $name = $1;
1057 :     # Check for an option value.
1058 :     if ($arg =~ /\G=(.*)$/g) {
1059 :     # Here we have a value for the option.
1060 :     $overrides{$name} = UnEscape($1);
1061 :     } else {
1062 :     # Here there is no value, so we use 1.
1063 :     $overrides{$name} = 1;
1064 :     }
1065 :     }
1066 :     # Merge the options into the defaults.
1067 :     GetOptions($optionTable, \%overrides);
1068 :     # Translate the remaining parameters.
1069 :     my @retVal = ();
1070 :     for my $inputParm (@inputList) {
1071 :     push @retVal, UnEscape($inputParm);
1072 :     }
1073 :     # Return the results.
1074 :     return ($optionTable, @retVal);
1075 : olson 1.1 }
1076 :    
1077 : parrello 1.9 =head3 Escape
1078 :    
1079 :     C<< my $codedString = Tracer::Escape($realString); >>
1080 :    
1081 : parrello 1.25 Escape a string for use in a command length. Tabs will be replaced by C<\t>, new-lines
1082 : parrello 1.28 replaced by C<\n>, carriage returns will be deleted, and backslashes will be doubled. The
1083 :     result is to reverse the effect of L</UnEscape>.
1084 : parrello 1.9
1085 :     =over 4
1086 :    
1087 :     =item realString
1088 :    
1089 :     String to escape.
1090 :    
1091 :     =item RETURN
1092 :    
1093 :     Escaped equivalent of the real string.
1094 :    
1095 :     =back
1096 :    
1097 :     =cut
1098 :    
1099 :     sub Escape {
1100 : parrello 1.12 # Get the parameter.
1101 :     my ($realString) = @_;
1102 :     # Initialize the return variable.
1103 :     my $retVal = "";
1104 :     # Loop through the parameter string, looking for sequences to escape.
1105 :     while (length $realString > 0) {
1106 :     # Look for the first sequence to escape.
1107 : parrello 1.27 if ($realString =~ /^(.*?)([\n\t\r\\])/) {
1108 : parrello 1.12 # Here we found it. The text preceding the sequence is in $1. The sequence
1109 :     # itself is in $2. First, move the clear text to the return variable.
1110 :     $retVal .= $1;
1111 : parrello 1.14 # Strip the processed section off the real string.
1112 :     $realString = substr $realString, (length $2) + (length $1);
1113 : parrello 1.28 # Get the matched character.
1114 : parrello 1.12 my $char = $2;
1115 : parrello 1.28 # If we have a CR, we are done.
1116 :     if ($char ne "\r") {
1117 :     # It's not a CR, so encode the escape sequence.
1118 :     $char =~ tr/\t\n/tn/;
1119 :     $retVal .= "\\" . $char;
1120 :     }
1121 : parrello 1.12 } else {
1122 :     # Here there are no more escape sequences. The rest of the string is
1123 :     # transferred unmodified.
1124 :     $retVal .= $realString;
1125 :     $realString = "";
1126 :     }
1127 :     }
1128 :     # Return the result.
1129 :     return $retVal;
1130 : parrello 1.9 }
1131 :    
1132 : olson 1.1 =head3 UnEscape
1133 :    
1134 :     C<< my $realString = Tracer::UnEscape($codedString); >>
1135 :    
1136 : parrello 1.25 Replace escape sequences with their actual equivalents. C<\t> will be replaced by
1137 : parrello 1.28 a tab, C<\n> by a new-line character, and C<\\> by a backslash. C<\r> codes will
1138 :     be deleted.
1139 : olson 1.1
1140 :     =over 4
1141 :    
1142 :     =item codedString
1143 :    
1144 :     String to un-escape.
1145 :    
1146 :     =item RETURN
1147 :    
1148 :     Returns a copy of the original string with the escape sequences converted to their actual
1149 :     values.
1150 :    
1151 :     =back
1152 :    
1153 :     =cut
1154 :    
1155 :     sub UnEscape {
1156 : parrello 1.12 # Get the parameter.
1157 :     my ($codedString) = @_;
1158 :     # Initialize the return variable.
1159 :     my $retVal = "";
1160 :     # Only proceed if the incoming string is nonempty.
1161 :     if (defined $codedString) {
1162 :     # Loop through the parameter string, looking for escape sequences. We can't do
1163 : parrello 1.25 # translating because it causes problems with the escaped slash. ("\\t" becomes
1164 :     # "\<tab>" no matter what we do.)
1165 : parrello 1.12 while (length $codedString > 0) {
1166 :     # Look for the first escape sequence.
1167 : parrello 1.27 if ($codedString =~ /^(.*?)\\(\\|n|t|r)/) {
1168 : parrello 1.12 # Here we found it. The text preceding the sequence is in $1. The sequence
1169 :     # itself is in $2. First, move the clear text to the return variable.
1170 :     $retVal .= $1;
1171 :     $codedString = substr $codedString, (2 + length $1);
1172 : parrello 1.28 # Get the escape value.
1173 : parrello 1.12 my $char = $2;
1174 : parrello 1.28 # If we have a "\r", we are done.
1175 :     if ($char ne 'r') {
1176 :     # Here it's not an 'r', so we convert it.
1177 :     $char =~ tr/\\tn/\\\t\n/;
1178 :     $retVal .= $char;
1179 :     }
1180 : parrello 1.12 } else {
1181 :     # Here there are no more escape sequences. The rest of the string is
1182 :     # transferred unmodified.
1183 :     $retVal .= $codedString;
1184 :     $codedString = "";
1185 :     }
1186 :     }
1187 :     }
1188 :     # Return the result.
1189 :     return $retVal;
1190 : olson 1.1 }
1191 :    
1192 :     =head3 ParseRecord
1193 :    
1194 :     C<< my @fields = Tracer::ParseRecord($line); >>
1195 :    
1196 :     Parse a tab-delimited data line. The data line is split into field values. Embedded tab
1197 :     and new-line characters in the data line must be represented as C<\t> and C<\n>, respectively.
1198 :     These will automatically be converted.
1199 :    
1200 :     =over 4
1201 :    
1202 :     =item line
1203 :    
1204 :     Line of data containing the tab-delimited fields.
1205 :    
1206 :     =item RETURN
1207 :    
1208 :     Returns a list of the fields found in the data line.
1209 :    
1210 :     =back
1211 :    
1212 :     =cut
1213 :    
1214 :     sub ParseRecord {
1215 : parrello 1.12 # Get the parameter.
1216 :     my ($line) = @_;
1217 :     # Remove the trailing new-line, if any.
1218 :     chomp $line;
1219 :     # Split the line read into pieces using the tab character.
1220 :     my @retVal = split /\t/, $line;
1221 :     # Trim and fix the escapes in each piece.
1222 :     for my $value (@retVal) {
1223 :     # Trim leading whitespace.
1224 :     $value =~ s/^\s+//;
1225 :     # Trim trailing whitespace.
1226 :     $value =~ s/\s+$//;
1227 :     # Delete the carriage returns.
1228 :     $value =~ s/\r//g;
1229 :     # Convert the escapes into their real values.
1230 :     $value =~ s/\\t/"\t"/ge;
1231 :     $value =~ s/\\n/"\n"/ge;
1232 :     }
1233 :     # Return the result.
1234 :     return @retVal;
1235 : olson 1.1 }
1236 :    
1237 :     =head3 Merge
1238 :    
1239 :     C<< my @mergedList = Tracer::Merge(@inputList); >>
1240 :    
1241 :     Sort a list of strings and remove duplicates.
1242 :    
1243 :     =over 4
1244 :    
1245 :     =item inputList
1246 :    
1247 :     List of scalars to sort and merge.
1248 :    
1249 :     =item RETURN
1250 :    
1251 :     Returns a list containing the same elements sorted in ascending order with duplicates
1252 :     removed.
1253 :    
1254 :     =back
1255 :    
1256 :     =cut
1257 :    
1258 :     sub Merge {
1259 : parrello 1.12 # Get the input list in sort order.
1260 :     my @inputList = sort @_;
1261 :     # Only proceed if the list has at least two elements.
1262 :     if (@inputList > 1) {
1263 :     # Now we want to move through the list splicing out duplicates.
1264 :     my $i = 0;
1265 :     while ($i < @inputList) {
1266 :     # Get the current entry.
1267 :     my $thisEntry = $inputList[$i];
1268 :     # Find out how many elements duplicate the current entry.
1269 :     my $j = $i + 1;
1270 :     my $dup1 = $i + 1;
1271 :     while ($j < @inputList && $inputList[$j] eq $thisEntry) { $j++; };
1272 :     # If the number is nonzero, splice out the duplicates found.
1273 :     if ($j > $dup1) {
1274 :     splice @inputList, $dup1, $j - $dup1;
1275 :     }
1276 :     # Now the element at position $dup1 is different from the element before it
1277 :     # at position $i. We push $i forward one position and start again.
1278 :     $i++;
1279 :     }
1280 :     }
1281 :     # Return the merged list.
1282 :     return @inputList;
1283 : olson 1.1 }
1284 :    
1285 :     =head3 GetFile
1286 :    
1287 : parrello 1.6 C<< my @fileContents = Tracer::GetFile($fileName); >>
1288 : olson 1.1
1289 : parrello 1.35 or
1290 :    
1291 :     C<< my $fileContents = Tracer::GetFile($fileName); >>
1292 :    
1293 :     Return the entire contents of a file. In list context, line-ends are removed and
1294 :     each line is a list element. In scalar context, line-ends are replaced by C<\n>.
1295 : olson 1.1
1296 :     =over 4
1297 :    
1298 :     =item fileName
1299 :    
1300 :     Name of the file to read.
1301 :    
1302 :     =item RETURN
1303 :    
1304 : parrello 1.6 In a list context, returns the entire file as a list with the line terminators removed.
1305 : parrello 1.39 In a scalar context, returns the entire file as a string. If an error occurs opening
1306 :     the file, an empty list will be returned.
1307 : olson 1.1
1308 :     =back
1309 :    
1310 :     =cut
1311 :    
1312 :     sub GetFile {
1313 : parrello 1.12 # Get the parameters.
1314 :     my ($fileName) = @_;
1315 :     # Declare the return variable.
1316 :     my @retVal = ();
1317 :     # Open the file for input.
1318 :     my $ok = open INPUTFILE, "<$fileName";
1319 :     if (!$ok) {
1320 :     # If we had an error, trace it. We will automatically return a null value.
1321 : parrello 1.16 Trace("Could not open \"$fileName\" for input: $!") if T(0);
1322 : parrello 1.12 } else {
1323 :     # Read the whole file into the return variable, stripping off any terminator
1324 : parrello 1.6 # characters.
1325 :     my $lineCount = 0;
1326 : parrello 1.12 while (my $line = <INPUTFILE>) {
1327 : parrello 1.6 $lineCount++;
1328 : parrello 1.9 $line = Strip($line);
1329 : parrello 1.12 push @retVal, $line;
1330 :     }
1331 :     # Close it.
1332 :     close INPUTFILE;
1333 : parrello 1.6 my $actualLines = @retVal;
1334 : parrello 1.12 }
1335 :     # Return the file's contents in the desired format.
1336 : parrello 1.9 if (wantarray) {
1337 : parrello 1.12 return @retVal;
1338 : parrello 1.6 } else {
1339 :     return join "\n", @retVal;
1340 :     }
1341 : olson 1.1 }
1342 :    
1343 :     =head3 QTrace
1344 :    
1345 :     C<< my $data = QTrace($format); >>
1346 :    
1347 :     Return the queued trace data in the specified format.
1348 :    
1349 :     =over 4
1350 :    
1351 :     =item format
1352 :    
1353 :     C<html> to format the data as an HTML list, C<text> to format it as straight text.
1354 :    
1355 :     =back
1356 :    
1357 :     =cut
1358 :    
1359 :     sub QTrace {
1360 : parrello 1.12 # Get the parameter.
1361 :     my ($format) = @_;
1362 :     # Create the return variable.
1363 :     my $retVal = "";
1364 : parrello 1.14 # Only proceed if there is an actual queue.
1365 :     if (@Queue) {
1366 :     # Process according to the format.
1367 :     if ($format =~ m/^HTML$/i) {
1368 :     # Convert the queue into an HTML list.
1369 :     $retVal = "<ul>\n";
1370 :     for my $line (@Queue) {
1371 :     my $escapedLine = CGI::escapeHTML($line);
1372 :     $retVal .= "<li>$escapedLine</li>\n";
1373 :     }
1374 :     $retVal .= "</ul>\n";
1375 :     } elsif ($format =~ m/^TEXT$/i) {
1376 :     # Convert the queue into a list of text lines.
1377 :     $retVal = join("\n", @Queue) . "\n";
1378 :     }
1379 :     # Clear the queue.
1380 :     @Queue = ();
1381 : parrello 1.12 }
1382 :     # Return the formatted list.
1383 :     return $retVal;
1384 : olson 1.1 }
1385 :    
1386 :     =head3 Confess
1387 :    
1388 :     C<< Confess($message); >>
1389 :    
1390 : parrello 1.22 Trace the call stack and abort the program with the specified message. When used with
1391 : parrello 1.9 the OR operator and the L</Assert> method, B<Confess> can function as a debugging assert.
1392 : parrello 1.6 So, for example
1393 : olson 1.1
1394 : parrello 1.6 C<< Assert($recNum >= 0) || Confess("Invalid record number $recNum."); >>
1395 : olson 1.1
1396 :     Will abort the program with a stack trace if the value of C<$recNum> is negative.
1397 :    
1398 :     =over 4
1399 :    
1400 :     =item message
1401 :    
1402 :     Message to include in the trace.
1403 :    
1404 :     =back
1405 :    
1406 :     =cut
1407 :    
1408 :     sub Confess {
1409 : parrello 1.12 # Get the parameters.
1410 :     my ($message) = @_;
1411 :     # Trace the call stack.
1412 : parrello 1.22 Cluck($message);
1413 : parrello 1.12 # Abort the program.
1414 :     croak(">>> $message");
1415 : olson 1.1 }
1416 :    
1417 : parrello 1.6 =head3 Assert
1418 :    
1419 :     C<< Assert($condition1, $condition2, ... $conditionN); >>
1420 :    
1421 :     Return TRUE if all the conditions are true. This method can be used in conjunction with
1422 : parrello 1.29 the OR operator and the L</Confess> method as a debugging assert.
1423 : parrello 1.6 So, for example
1424 :    
1425 :     C<< Assert($recNum >= 0) || Confess("Invalid record number $recNum."); >>
1426 :    
1427 :     Will abort the program with a stack trace if the value of C<$recNum> is negative.
1428 :    
1429 :     =cut
1430 :     sub Assert {
1431 :     my $retVal = 1;
1432 :     LOOP: for my $condition (@_) {
1433 :     if (! $condition) {
1434 :     $retVal = 0;
1435 :     last LOOP;
1436 :     }
1437 :     }
1438 :     return $retVal;
1439 :     }
1440 :    
1441 : olson 1.1 =head3 Cluck
1442 :    
1443 :     C<< Cluck($message); >>
1444 :    
1445 :     Trace the call stack. Note that for best results, you should qualify the call with a
1446 :     trace condition. For example,
1447 :    
1448 :     C<< Cluck("Starting record parse.") if T(3); >>
1449 :    
1450 :     will only trace the stack if the trace level for the package is 3 or more.
1451 :    
1452 :     =over 4
1453 :    
1454 :     =item message
1455 :    
1456 :     Message to include in the trace.
1457 :    
1458 :     =back
1459 :    
1460 :     =cut
1461 :    
1462 :     sub Cluck {
1463 : parrello 1.12 # Get the parameters.
1464 :     my ($message) = @_;
1465 : parrello 1.5 # Trace what's happening.
1466 :     Trace("Stack trace for event: $message");
1467 : parrello 1.12 my $confession = longmess($message);
1468 :     # Convert the confession to a series of trace messages. Note we skip any
1469 : parrello 1.5 # messages relating to calls into Tracer.
1470 : parrello 1.12 for my $line (split /\s*\n/, $confession) {
1471 :     Trace($line) if ($line !~ /Tracer\.pm/);
1472 :     }
1473 : olson 1.1 }
1474 :    
1475 : parrello 1.5 =head3 Min
1476 :    
1477 :     C<< my $min = Min($value1, $value2, ... $valueN); >>
1478 :    
1479 :     Return the minimum argument. The arguments are treated as numbers.
1480 :    
1481 :     =over 4
1482 :    
1483 :     =item $value1, $value2, ... $valueN
1484 :    
1485 :     List of numbers to compare.
1486 :    
1487 :     =item RETURN
1488 :    
1489 :     Returns the lowest number in the list.
1490 :    
1491 :     =back
1492 :    
1493 :     =cut
1494 :    
1495 :     sub Min {
1496 : parrello 1.12 # Get the parameters. Note that we prime the return value with the first parameter.
1497 :     my ($retVal, @values) = @_;
1498 :     # Loop through the remaining parameters, looking for the lowest.
1499 :     for my $value (@values) {
1500 :     if ($value < $retVal) {
1501 :     $retVal = $value;
1502 :     }
1503 :     }
1504 :     # Return the minimum found.
1505 :     return $retVal;
1506 : parrello 1.5 }
1507 :    
1508 :     =head3 Max
1509 :    
1510 :     C<< my $max = Max($value1, $value2, ... $valueN); >>
1511 :    
1512 :     Return the maximum argument. The arguments are treated as numbers.
1513 :    
1514 :     =over 4
1515 :    
1516 :     =item $value1, $value2, ... $valueN
1517 :    
1518 :     List of numbers to compare.
1519 :    
1520 :     =item RETURN
1521 :    
1522 :     Returns the highest number in the list.
1523 :    
1524 :     =back
1525 :    
1526 :     =cut
1527 :    
1528 :     sub Max {
1529 : parrello 1.12 # Get the parameters. Note that we prime the return value with the first parameter.
1530 :     my ($retVal, @values) = @_;
1531 :     # Loop through the remaining parameters, looking for the highest.
1532 :     for my $value (@values) {
1533 :     if ($value > $retVal) {
1534 :     $retVal = $value;
1535 :     }
1536 :     }
1537 :     # Return the maximum found.
1538 :     return $retVal;
1539 : parrello 1.5 }
1540 :    
1541 :     =head3 AddToListMap
1542 :    
1543 :     C<< Tracer::AddToListMap(\%hash, $key, $value); >>
1544 :    
1545 :     Add a key-value pair to a hash of lists. If no value exists for the key, a singleton list
1546 :     is created for the key. Otherwise, the new value is pushed onto the list.
1547 :    
1548 :     =over 4
1549 :    
1550 :     =item hash
1551 :    
1552 :     Reference to the target hash.
1553 :    
1554 :     =item key
1555 :    
1556 :     Key for which the value is to be added.
1557 :    
1558 :     =item value
1559 :    
1560 :     Value to add to the key's value list.
1561 :    
1562 :     =back
1563 :    
1564 :     =cut
1565 :    
1566 :     sub AddToListMap {
1567 :     # Get the parameters.
1568 :     my ($hash, $key, $value) = @_;
1569 :     # Process according to whether or not the key already has a value.
1570 :     if (! exists $hash->{$key}) {
1571 :     $hash->{$key} = [$value];
1572 :     } else {
1573 :     push @{$hash->{$key}}, $value;
1574 :     }
1575 :     }
1576 : olson 1.1
1577 : parrello 1.7 =head3 DebugMode
1578 :    
1579 :     C<< if (Tracer::DebugMode) { ...code... } >>
1580 :    
1581 : parrello 1.22 Return TRUE if debug mode has been turned on, else output an error
1582 :     page and return FALSE.
1583 : parrello 1.7
1584 :     Certain CGI scripts are too dangerous to exist in the production
1585 :     environment. This method provides a simple way to prevent them
1586 : parrello 1.21 from working unless they are explicitly turned on by creating a password
1587 :     cookie via the B<SetPassword> script. If debugging mode
1588 : parrello 1.22 is not turned on, an error web page will be output directing the
1589 :     user to enter in the correct password.
1590 : parrello 1.7
1591 :     =cut
1592 :    
1593 :     sub DebugMode {
1594 : parrello 1.12 # Declare the return variable.
1595 : parrello 1.21 my $retVal = 0;
1596 : parrello 1.12 # Check the debug configuration.
1597 : parrello 1.21 my $password = CGI::cookie("DebugMode");
1598 :     my $encrypted = Digest::MD5::md5_hex($password);
1599 :     if ($encrypted eq "252dec43280e0c0d6a75ffcec486e61d") {
1600 : parrello 1.12 $retVal = 1;
1601 :     } else {
1602 :     # Here debug mode is off, so we generate an error page.
1603 : parrello 1.9 my $pageString = PageBuilder::Build("<Html/ErrorPage.html", {}, "Html");
1604 : parrello 1.12 print $pageString;
1605 :     }
1606 :     # Return the determination indicator.
1607 : parrello 1.18 return $retVal;
1608 : parrello 1.9 }
1609 :    
1610 :     =head3 Strip
1611 :    
1612 :     C<< my $string = Tracer::Strip($line); >>
1613 :    
1614 :     Strip all line terminators off a string. This is necessary when dealing with files
1615 :     that may have been transferred back and forth several times among different
1616 :     operating environments.
1617 :    
1618 :     =over 4
1619 :    
1620 :     =item line
1621 :    
1622 :     Line of text to be stripped.
1623 :    
1624 :     =item RETURN
1625 :    
1626 :     The same line of text with all the line-ending characters chopped from the end.
1627 :    
1628 :     =back
1629 :    
1630 :     =cut
1631 :    
1632 :     sub Strip {
1633 : parrello 1.12 # Get a copy of the parameter string.
1634 :     my ($string) = @_;
1635 : parrello 1.29 my $retVal = (defined $string ? $string : "");
1636 : parrello 1.9 # Strip the line terminator characters.
1637 :     $retVal =~ s/(\r|\n)+$//g;
1638 : parrello 1.12 # Return the result.
1639 :     return $retVal;
1640 : parrello 1.9 }
1641 :    
1642 :     =head3 Pad
1643 :    
1644 :     C<< my $paddedString = Tracer::Pad($string, $len, $left, $padChar); >>
1645 :    
1646 :     Pad a string to a specified length. The pad character will be a
1647 :     space, and the padding will be on the right side unless specified
1648 :     in the third parameter.
1649 :    
1650 :     =over 4
1651 :    
1652 :     =item string
1653 :    
1654 :     String to be padded.
1655 :    
1656 :     =item len
1657 :    
1658 :     Desired length of the padded string.
1659 :    
1660 :     =item left (optional)
1661 :    
1662 :     TRUE if the string is to be left-padded; otherwise it will be padded on the right.
1663 :    
1664 :     =item padChar (optional)
1665 :    
1666 : parrello 1.22 Character to use for padding. The default is a space.
1667 :    
1668 : parrello 1.9 =item RETURN
1669 :    
1670 : parrello 1.22 Returns a copy of the original string with the pad character added to the
1671 :     specified end so that it achieves the desired length.
1672 : parrello 1.9
1673 :     =back
1674 :    
1675 :     =cut
1676 :    
1677 :     sub Pad {
1678 : parrello 1.12 # Get the parameters.
1679 :     my ($string, $len, $left, $padChar) = @_;
1680 :     # Compute the padding character.
1681 :     if (! defined $padChar) {
1682 :     $padChar = " ";
1683 :     }
1684 :     # Compute the number of spaces needed.
1685 :     my $needed = $len - length $string;
1686 :     # Copy the string into the return variable.
1687 :     my $retVal = $string;
1688 :     # Only proceed if padding is needed.
1689 :     if ($needed > 0) {
1690 :     # Create the pad string.
1691 :     my $pad = $padChar x $needed;
1692 :     # Affix it to the return value.
1693 :     if ($left) {
1694 :     $retVal = $pad . $retVal;
1695 :     } else {
1696 :     $retVal .= $pad;
1697 :     }
1698 :     }
1699 :     # Return the result.
1700 :     return $retVal;
1701 : parrello 1.7 }
1702 :    
1703 : parrello 1.29 =head3 EOF
1704 :    
1705 :     This is a constant that is lexically greater than any useful string.
1706 :    
1707 :     =cut
1708 :    
1709 :     sub EOF {
1710 :     return "\xFF\xFF\xFF\xFF\xFF";
1711 :     }
1712 :    
1713 : parrello 1.15 =head3 TICK
1714 :    
1715 :     C<< my @results = TICK($commandString); >>
1716 :    
1717 :     Perform a back-tick operation on a command. If this is a Windows environment, any leading
1718 :     dot-slash (C<./> will be removed. So, for example, if you were doing
1719 :    
1720 :     `./protein.cgi`
1721 :    
1722 :     from inside a CGI script, it would work fine in Unix, but would issue an error message
1723 :     in Windows complaining that C<'.'> is not a valid command. If instead you code
1724 :    
1725 :     TICK("./protein.cgi")
1726 :    
1727 :     it will work correctly in both environments.
1728 :    
1729 :     =over 4
1730 :    
1731 :     =item commandString
1732 :    
1733 :     The command string to pass to the system.
1734 :    
1735 :     =item RETURN
1736 :    
1737 :     Returns the standard output from the specified command, as a list.
1738 :    
1739 :     =back
1740 :    
1741 :     =cut
1742 :     #: Return Type @;
1743 :     sub TICK {
1744 :     # Get the parameters.
1745 :     my ($commandString) = @_;
1746 :     # Chop off the dot-slash if this is Windows.
1747 :     if ($FIG_Config::win_mode) {
1748 :     $commandString =~ s!^\./!!;
1749 :     }
1750 :     # Activate the command and return the result.
1751 :     return `$commandString`;
1752 :     }
1753 :    
1754 : parrello 1.35 =head3 ScriptSetup
1755 :    
1756 :     C<< my ($query, $varHash) = ScriptSetup(); >>
1757 :    
1758 :     Perform standard tracing and debugging setup for scripts. The value returned is
1759 :     the CGI object followed by a pre-built variable hash.
1760 :    
1761 :     The C<Trace> query parameter is used to determine whether or not tracing is active and
1762 :     which trace modules (other than C<Tracer> and C<FIG>) should be turned on. Specifying
1763 :     the C<CGI> trace module will trace parameter and environment information. Parameters are
1764 :     traced at level 3 and environment variables at level 4. At the end of the script, the
1765 :     client should call L</ScriptFinish> to output the web page.
1766 :    
1767 :     =cut
1768 :    
1769 :     sub ScriptSetup {
1770 :     # Get the CGI query object.
1771 :     my $query = CGI->new();
1772 :     # Check for tracing. Set it up if the user asked for it.
1773 :     if ($query->param('Trace')) {
1774 :     # Set up tracing to be queued for display at the bottom of the web page.
1775 :     TSetup($query->param('Trace') . " FIG Tracer", "QUEUE");
1776 :     # Trace the parameter and environment data.
1777 :     if (T(CGI => 3)) {
1778 :     # Here we want to trace the parameter data.
1779 :     my @names = $query->param;
1780 :     for my $parmName (sort @names) {
1781 :     # Note we skip "Trace", which is for our use only.
1782 :     if ($parmName ne 'Trace') {
1783 :     my @values = $query->param($parmName);
1784 :     Trace("CGI: $parmName = " . join(", ", @values));
1785 :     }
1786 :     }
1787 :     }
1788 :     if (T(CGI => 4)) {
1789 :     # Here we want the environment data too.
1790 :     for my $envName (sort keys %ENV) {
1791 :     Trace("ENV: $envName = $ENV{$envName}");
1792 :     }
1793 :     }
1794 :     } else {
1795 :     # Here tracing is to be turned off. All we allow is errors traced into the
1796 :     # error log.
1797 :     TSetup("0", "WARN");
1798 :     }
1799 :     # Create the variable hash.
1800 :     my $varHash = { DebugData => '' };
1801 :     # If we're in DEBUG mode, set up the debug mode data for forms.
1802 :     if (Tracer::DebugMode) {
1803 :     $varHash->{DebugData} = GetFile("Html/DebugFragment.html");
1804 :     }
1805 :     # Return the query object and variable hash.
1806 :     return ($query, $varHash);
1807 :     }
1808 :    
1809 :     =head3 ScriptFinish
1810 :    
1811 :     C<< ScriptFinish($webData, $varHash); >>
1812 :    
1813 :     Output a web page at the end of a script. Either the string to be output or the
1814 :     name of a template file can be specified. If the second parameter is omitted,
1815 :     it is assumed we have a string to be output; otherwise, it is assumed we have the
1816 :     name of a template file. The template should have the variable C<DebugData>
1817 :     specified in any form that invokes a standard script. If debugging mode is turned
1818 :     on, a form field will be put in that allows the user to enter tracing data.
1819 :     Trace messages will be placed immediately before the terminal C<BODY> tag in
1820 :     the output, formatted as a list.
1821 :    
1822 :     A typical standard script would loook like the following.
1823 :    
1824 :     BEGIN {
1825 :     # Print the HTML header.
1826 :     print "CONTENT-TYPE: text/html\n\n";
1827 :     }
1828 :     use Tracer;
1829 :     use CGI;
1830 :     use FIG;
1831 :     # ... more uses ...
1832 :    
1833 :     my ($query, $varHash) = ScriptSetup();
1834 :     eval {
1835 :     # ... get data from $query, put it in $varHash ...
1836 :     };
1837 :     if ($@) {
1838 :     Trace("Script Error: $@") if T(0);
1839 :     }
1840 :     ScriptFinish("Html/MyTemplate.html", $varHash);
1841 :    
1842 :     The idea here is that even if the script fails, you'll see trace messages and
1843 :     useful output.
1844 :    
1845 :     =over 4
1846 :    
1847 :     =item webData
1848 :    
1849 :     A string containing either the full web page to be written to the output or the
1850 :     name of a template file from which the page is to be constructed. If the name
1851 :     of a template file is specified, then the second parameter must be present;
1852 :     otherwise, it must be absent.
1853 :    
1854 :     =item varHash (optional)
1855 :    
1856 :     If specified, then a reference to a hash mapping variable names for a template
1857 :     to their values. The template file will be read into memory, and variable markers
1858 :     will be replaced by data in this hash reference.
1859 :    
1860 : parrello 1.37 =back
1861 :    
1862 : parrello 1.35 =cut
1863 :    
1864 :     sub ScriptFinish {
1865 :     # Get the parameters.
1866 :     my ($webData, $varHash) = @_;
1867 :     # Check for a template file situation.
1868 :     my $outputString;
1869 :     if (defined $varHash) {
1870 :     # Here we have a template file. We need to apply the variables to the template.
1871 :     $outputString = PageBuilder::Build("<$webData", $varHash, "Html");
1872 :     } else {
1873 :     # Here the user gave us a raw string.
1874 :     $outputString = $webData;
1875 :     }
1876 :     # Check for trace messages.
1877 :     if ($Destination eq "QUEUE") {
1878 :     # We have trace messages, so we want to put them at the end of the body. This
1879 :     # is either at the end of the whole string or at the beginning of the BODY
1880 :     # end-tag.
1881 :     my $pos = length $outputString;
1882 :     if ($outputString =~ m#</body>#gi) {
1883 :     $pos = (pos $outputString) - 7;
1884 :     }
1885 :     substr $outputString, $pos, 0, QTrace('Html');
1886 :     }
1887 :     # Write the output string.
1888 :     print $outputString;
1889 :     }
1890 :    
1891 : parrello 1.37 =head3 Insure
1892 :    
1893 :     C<< Insure($dirName); >>
1894 :    
1895 :     Insure a directory is present.
1896 :    
1897 :     =over 4
1898 :    
1899 :     =item dirName
1900 :    
1901 :     Name of the directory to check. If it does not exist, it will be created.
1902 :    
1903 :     =back
1904 :    
1905 :     =cut
1906 :    
1907 :     sub Insure {
1908 :     my ($dirName) = @_;
1909 :     if (! -d $dirName) {
1910 :     Trace("Creating $dirName directory.") if T(2);
1911 :     mkpath $dirName;
1912 :     }
1913 :     }
1914 :    
1915 : redwards 1.8 1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3