Config::Model::Backend::DpkgSyntax(3) Role to read and write files with Dpkg syntax

SYNOPSIS


package MyParser ;
use Log::Log4perl qw(:easy);
Log::Log4perl->easy_init($WARN);

use Mouse ;
with 'Config::Model::Backend::DpkgSyntax';

package main ;
use IO::File ;
use Data::Dumper ;

my $data = [ [ qw/Name Foo Version 1.2/ ],
[ qw/Name Bar Version 1.3/ ,
Files => [qw/file1 file2/] ,
Description => "A very\n\nlong description"
]
] ;

my $fhw = IO::File->new ;
$fhw -> open ( 'dpkg_file' ,'>' ) ;
my $parser = MyParser->new() ;

$parser->write_dpkg_file($fhw,$data) ;

"dpkg_file" will contain:

 Name: Foo
 Version: 1.2
 Name: Bar
 Version: 1.3
 Files: file1,
        file2
 Description: A very
  .
  long description

DESCRIPTION

This module is a Moose role to read and write dpkg control files.

Debian control file are read and transformed in a list of list matching the control file. The top level list of a list of section. Each section is mapped to a list made of keywords and values. Since this explanation is probably too abstract, here's an example of a file written with Dpkg syntax:

 Name: Foo
 Version: 1.1
 Name: Bar
 # boy, new version
 Version: 1.2
  Description: A very
  . 
  long description

Once parsed, this file will be stored in the following list of list :

 (
   [ Name => 'Foo', Version => '1.1' ],
   [ Name => 'Bar', Version => [ '1.2' 'boy, new version' ], 
     Description => "A very\n\nlong description"
   ]
 )

Note: The description is changed into a paragraph without the Dpkg syntax idiosyncrasies. The leading white space is removed and the single dot is transformed in to a ``\n''. These characters will be restored when the file is written back.

Last not but not least, this module can be re-used outside of "Config::Model" with some small modifications in exception handing. Ask the author if you want this module shipped in its own distribution.

parse_dpkg_file ( file_handle, check, comment_allowed )

Read a control file from the file_handle and returns a nested list (or a list ref) containing data from the file.

The returned list is of the form :

 [
   # section 1
   [ keyword1 => value1, # for text or simple values
     keyword2 => value2, # etc 
   ],
   # section 2
   [ ... ]
   # etc ...
 ]

check is "yes", "skip" or "no". "comment_allowed" is boolean (default 0)

When comments are provided in the dpkg files, the returned list is of the form :

 [
   [ 
     keyword1 => [ value1, 'value1 comment'] 
     keyword2 => value2, # no comment 
   ],
   [ ... ]
 ]

parse_dpkg_lines (lines, check, comment_allowed )

Parse the dpkg date from lines (which is an array ref) and return a data structure like parse_dpkg_file.

write_dpkg_file ( io_handle, list_ref, list_sep )

Munge the passed list ref into a string compatible with control files and write it in the passed file handle.

The input is a list of list in a form similar to the one generated by parse_dpkg_file:

 [ section [ keyword => value | value_list ] ]

Except that the value may be a SCALAR or a list ref. In case, of a list ref, the list items will be joined with the value "list_sep" before being written. Values will be aligned in case of multi-line output of a list.

For instance the following code :

 my $ref = [ [ Foo => 'foo value' , Bar => [ qw/v1 v2/ ] ];
 write_dpkg_file ( $ioh, $ref, ', ' )

will yield:

 Foo: foo value
 Bar: v1, v2

AUTHOR

Dominique Dumont, (ddumont at cpan dot org)