1 package Parse::DebControl;
3 ###########################################################
4 # Parse::DebControl - Parse debian-style control
5 # files (and other colon key-value fields)
7 # Copyright 2003 - Jay Bonci <jaybonci@cpan.org>
8 # Licensed under the same terms as perl itself
10 ###########################################################
15 use vars qw($VERSION);
19 my ($class, $debug) = @_;
22 my $obj = bless $this, $class;
31 my ($this, $filename, $options) = @_;
34 $this->_dowarn("parse_file failed because no filename parameter was given");
39 unless(open($fh,"$filename"))
41 $this->_dowarn("parse_file failed because $filename could not be opened for reading");
45 return $this->_parseDataHandle($fh, $options);
49 my ($this, $data, $options) = @_;
53 $this->_dowarn("parse_mem failed because no data was given");
57 my $IOS = new IO::Scalar \$data;
61 $this->_dowarn("parse_mem failed because IO::Scalar creation failed.");
65 return $this->_parseDataHandle($IOS, $options);
70 my ($this, $filenameorhandle, $dataorarrayref, $options) = @_;
72 unless($filenameorhandle)
74 $this->_dowarn("write_file failed because no filename or filehandle was given");
78 unless($dataorarrayref)
80 $this->_dowarn("write_file failed because no data was given");
84 my $handle = $this->_getValidHandle($filenameorhandle, $options);
88 $this->_dowarn("write_file failed because we couldn't negotiate a valid handle");
92 my $arrayref = $this->_makeArrayref($dataorarrayref);
94 my $string = $this->_makeControl($arrayref);
97 print $handle $string;
100 return length($string);
104 my ($this, $dataorarrayref, $options) = @_;
106 unless($dataorarrayref)
108 $this->_dowarn("write_mem failed because no data was given");
112 my $arrayref = $this->_makeArrayref($dataorarrayref);
114 my $string = $this->_makeControl($arrayref);
121 my($this, $verbose) = @_;
122 $verbose = 1 unless(defined($verbose) and int($verbose) == 0);
123 $this->{_verbose} = $verbose;
128 sub _getValidHandle {
129 my($this, $filenameorhandle, $options) = @_;
131 if(ref $filenameorhandle eq "GLOB")
133 unless($filenameorhandle->opened())
135 $this->_dowarn("Can't get a valid filehandle to write to, because that is closed");
139 return $filenameorhandle;
143 $openmode=">" if $options->{clobberFile};
144 $openmode=">>" if $options->{appendFile};
148 unless(open $handle,"$openmode$filenameorhandle")
150 $this->_dowarn("Couldn't open file: $openmode$filenameorhandle for writing");
159 my ($this, $dataorarrayref) = @_;
161 if(ref $dataorarrayref eq "ARRAY")
163 return $dataorarrayref;
165 return [$dataorarrayref];
171 my ($this, $dataorarrayref) = @_;
175 foreach my $stanza(@$dataorarrayref)
177 foreach my $key(keys %$stanza)
179 $stanza->{$key} ||= "";
181 my @lines = split("\n", $stanza->{$key});
183 $str.="$key\: ".(shift @lines)."\n";
212 my ($this, $handle, $options) = @_;
218 $this->_dowarn("_parseDataHandle failed because no handle was given. This is likely a bug in the module");
222 my $data = $this->_getReadyHash($options);
227 foreach my $line (<$handle>)
229 #Sometimes with IO::Scalar, lines may have a newline at the end
232 if($options->{stripComments}){
233 next if $line =~ /^\s*\#/;
238 if($line =~ /^[^\t\s]/)
240 #we have a valid key-value pair
241 if($line =~ /(.*?)\s*\:\s*(.*)$/)
246 if($options->{discardCase})
251 unless($options->{verbMultiLine})
253 $value =~ s/[\s\t]+$//;
256 $data->{$key} = $value;
259 if ($options->{verbMultiLine}
260 && (($data->{$lastfield} || "") =~ /\n/o)){
261 $data->{$lastfield} .= "\n";
266 $this->_dowarn("Parse error on line $linenum of data; invalid key/value stanza");
270 }elsif($line =~ /^([\t\s])(.*)/)
272 #appends to previous line
276 $this->_dowarn("Parse error on line $linenum of data; indented entry without previous line");
279 if($options->{verbMultiLine}){
280 $data->{$lastfield}.="\n$1$2";
282 $data->{$lastfield}.="\n";
285 $val =~ s/[\s\t]+$//;
286 $data->{$lastfield}.="\n$val";
289 }elsif($line =~ /^[\s\t]*$/){
290 if ($options->{verbMultiLine}
291 && ($data->{$lastfield} =~ /\n/o)) {
292 $data->{$lastfield} .= "\n";
295 push @$structs, $data;
297 $data = $this->_getReadyHash($options);
300 $this->_dowarn("Parse error on line $linenum of data; unidentified line structure");
308 push @$structs, $data;
316 my ($this, $options) = @_;
319 if($options->{useTieIxHash})
321 eval("use Tie::IxHash");
324 $this->_dowarn("Can't use Tie::IxHash. You need to install it to have this functionality");
327 tie(%$data, "Tie::IxHash");
336 my ($this, $warning) = @_;
338 if($this->{_verbose})
340 warn "DEBUG: $warning";
353 Parse::DebControl - Easy OO parsing of debian control-like files
357 use Parse::DebControl
359 $parser = new Parse::DebControl;
361 $data = $parser->parse_mem($control_data, %options);
362 $data = $parser->parse_file('./debian/control', %options);
364 $writer = new Parse::DebControl;
366 $string = $writer->write_mem($singlestanza);
367 $string = $writer->write_mem([$stanza1, $stanza2]);
369 $writer->write_file($filename, $singlestanza, %options);
370 $writer->write_file($filename, [$stanza1, $stanza2], %options);
372 $writer->write_file($handle, $singlestanza, %options);
373 $writer->write_file($handle, [$stanza1, $stanza2], %options);
379 Parse::DebControl is an easy OO way to parse debian control files and
380 other colon separated key-value pairs. It's specifically designed
381 to handle the format used in Debian control files, template files, and
382 the cache files used by dpkg.
384 For basic format information see:
385 http://www.debian.org/doc/debian-policy/ch-controlfields.html#s-controlsyntax
387 This module does not actually do any intelligence with the file content
388 (because there are a lot of files in this format), but merely handles
389 the format. It can handle simple control files, or files hundreds of lines
390 long efficiently and easily.
398 =item * C<new(I<$debug>)>
400 Returns a new Parse::DebControl object. If a true parameter I<$debug> is
401 passed in, it turns on debugging, similar to a call to C<DEBUG()> (see below);
407 =item * C<parse_file($control_filename,I<%options>)>
409 Takes a filename as a scalar. Will parse as much as it can,
410 warning (if C<DEBUG>ing is turned on) on parsing errors.
412 Returns an array of hashes, containing the data in the control file, split up
413 by stanza. Stanzas are deliniated by newlines, and multi-line fields are
414 expressed as such post-parsing. Single periods are treated as special extra
415 newline deliniators, per convention. Whitespace is also stripped off of lines
416 as to make it less-easy to make mistakes with hand-written conf files).
418 The options hash can take parameters as follows. Setting the string to true
421 useTieIxHash - Instead of an array of regular hashes, uses Tie::IxHash-
423 discardCase - Remove all case items from keys (not values)
424 stripComments - Remove all commented lines in standard #comment format
425 verbMultiLine - Keep the description AS IS, and no not collapse leading
426 spaces or dots as newlines. This also keeps whitespace from being
427 stripped off the end of lines.
433 =item * C<parse_mem($control_data, I<%options>)>
435 Similar to C<parse_file>, except takes data as a scalar. Returns the same
436 array of hashrefs as C<parse_file>. The options hash is the same as
437 C<parse_file> as well; see above.
443 =item * C<write_file($filename, $data, I<%options>)>
445 =item * C<write_file($handle, $data>
447 =item * C<write_file($filename, [$data1, $data2, $data3], I<%options>)>
449 =item * C<write_file($handle, [$data, $data2, $data3])>
451 This function takes a filename or a handle and writes the data out. The
452 data can be given as a single hash(ref) or as an arrayref of hash(ref)s. It
453 will then write it out in a format that it can parse. The order is dependant
454 on your hash sorting order. If you care, use Tie::IxHash. Remember for
455 reading back in, the module doesn't care.
457 The I<%options> hash can contain one of the following two items:
459 appendFile - (default) Write to the end of the file
460 clobberFile - Overwrite the file given.
462 Since you determine the mode of your filehandle, passing it an options hash
463 obviously won't do anything; rather, it is ignored.
465 This function returns the number of bytes written to the file, undef
472 =item * C<write_mem($data)>
474 =item * C<write_mem([$data1,$data2,$data3])>;
476 This function works similarly to the C<write_file> method, except it returns
477 the control structure as a scalar, instead of writing it to a file. There
478 is no I<%options> for this file (yet);
486 Turns on debugging. Calling it with no paramater or a true parameter turns
487 on verbose C<warn()>ings. Calling it with a false parameter turns it off.
488 It is useful for nailing down any format or internal problems.
494 B<Version 1.7> - July 11th, 2003
498 =item * By default, we now strip off whitespace unless verbMultiLine is in place. This makes sense for things like conf files where trailing whitespace has no meaning. Thanks to pudge for reporting this.
502 B<Version 1.7> - June 25th, 2003
506 =item * POD documentation error noticed again by Frank Lichtenheld
508 =item * Also by Frank, applied a patch to add a "verbMultiLine" option so that we can hand multiline fields back unparsed.
510 =item * Slightly expanded test suite to cover new features
514 B<Version 1.6.1> - June 9th, 2003
518 =item * POD cleanups noticed by Frank Lichtenheld. Thank you, Frank.
522 B<Version 1.6> - June 2nd, 2003
526 =item * Cleaned up some warnings when you pass in empty hashrefs or arrayrefs
528 =item * Added stripComments setting
530 =item * Cleaned up POD errors
534 B<Version 1.5> - May 8th, 2003
538 =item * Added a line to quash errors with undef hashkeys and writing
540 =item * Fixed the Makefile.PL to straighten up DebControl.pm being in the wrong dir
544 B<Version 1.4> - April 30th, 2003
548 =item * Removed exports as they were unnecessary. Many thanks to pudge, who pointed this out.
552 B<Version 1.3> - April 28th, 2003
556 =item * Fixed a bug where writing blank stanzas would throw a warning. Fix found and supplied by Nate Oostendorp.
560 B<Version 1.2b> - April 25th, 2003
566 =item * A bug in the test suite where IxHash was not disabled in 40write.t. Thanks to Jeroen Latour from cpan-testers for the report.
570 B<Version 1.2> - April 24th, 2003
576 =item * A bug in IxHash support where multiple stanzas might be out of order
580 B<Version 1.1> - April 23rd, 2003
586 =item * Writing support
588 =item * Tie::IxHash support
590 =item * Case insensitive reading support
594 * B<Version 1.0> - April 23rd, 2003
598 =item * This is the initial public release for CPAN, so everything is new.
604 The module will let you parse otherwise illegal key-value pairs and pairs with spaces. Badly formed stanzas will do things like overwrite duplicate keys, etc. This is your problem.
608 Change the name over to the Debian:: namespace, probably as Debian::ControlFormat. This will happen as soon as the project that uses this module reaches stability, and we can do some minor tweaks.
612 Parse::DebControl is copyright 2003 Jay Bonci E<lt>jaybonci@cpan.orgE<gt>.
613 This program is free software; you can redistribute it and/or modify it under
614 the same terms as Perl itself.