Config...Syntax(3pm)
β¨ NAME β¨
So bestie, this is Config::Model::Backend::DpkgSyntax (from the Config...Syntax(3pm) page, part of the User Contributed Perl Documentation) β and it's a role whose whole job is reading and writing files that speak Dpkg syntax. That's it, that's the gig π
πΈ SYNOPSIS πΈ
Okay picture this. You've got a dpkg file, right? And it looks like this:
Name: Foo
Version: 1.2
# section comment
Name: Bar
# data comment
Version: 1.3
Files: file1,
# inline comment
file2
Description: A very
.
long description
Now to parse that lil cutie, you do:
package MyParser ;
use strict;
use warnings;
use 5.20.1;
# DpkgSyntax uses Log4perl, so we must initialise this module
use Log::Log4perl qw(:easy);
Log::Log4perl->easy_init($WARN);
# load role
use Mouse ;
with 'Config::Model::Backend::DpkgSyntax';
package main ;
use Path::Tiny;
use YAML::PP;
# load control file
my $file = path('dpkg-test');
# create your parser
my $parser = MyParser->new() ;
# convert control file data in a Perl data structure
# documented in Synopsis
my $data = $parser->parse_dpkg_file($file, 'yes', 1);
And the $data you get back is giving this:
[
1, # section 1 found in line 1
[
'Name', # first parameter
[
'section comment',
[
'Foo', # first parameter data
1, # also found in line 1
'' # currently always empty
]
],
'Version', [ 'data comment', ['1.2', 2, '']]
], # end of section 1
4, # section 2 found in line 4
[
'Name', [['Bar', 5, '']],
'Version', [['1.3', 7, '']],
'Files', # param with 2 lines
[
['file1,', 8, ''],
[' file2', 10, '', 'inline comment'] # padding is kept
],
'Description', # param with 3 lines
[
['A very', 11, ''],
['', 12, ''], # empty line, note: dot was removed
['long description', 13, '']
]
] # end of section 2
]; # end of data
Obsessed with that structure ngl π now β to write a Dpkg file back out:
package MyParser ;
use strict;
use warnings;
use 5.20.1;
# DpkgSyntax uses Log4perl, so we must initialise this module
use Log::Log4perl qw(:easy);
Log::Log4perl->easy_init($WARN);
# load role
use Mouse ;
with 'Config::Model::Backend::DpkgSyntax';
package main ;
use Path::Tiny;
my $data = [
[
'# section comment', qw/Name Foo/,
'# data comment', qw/Version 1.2/
],
[
qw/Name Bar Version 1.3/ ,
Files => [qw/file1/, [ 'file2' , '# inline comment'] ] ,
Description => "A very\n\nlong description"
]
];
my $parser = MyParser->new() ;
# print control file content
say $parser->write_dpkg_file($data) ;
Periodt. Write it out, print it, done π
π¦ DESCRIPTION π¦
So this module is a Moose role β its whole personality is reading and writing dpkg control files β¨
Debian control files get read and transformed into a structure that matches the control file itself. The top level list is a list of sections. Lowkey elegant, ngl.
Every section maps to a structure holding the parameter names and values, the comments, and the line numbers. Peep the synopsis for a whole example, it's right there π₯°
Note bestie, this is important: the description gets changed into a paragraph without the Dpkg syntax idiosyncrasies. The leading white space gets removed, and the single dot gets transformed into a "\n". Those characters are restored when the file is written back. So don't panic, nothing is lost forever π
And last but very much not least β this module can be re-used outside of Config::Model with some small modifications in exception handling. Just ask the author if you want this module shipped in its own distribution π
π parse_dpkg_file π
Parameters: ( file_path, file_handle, [ check, [ comment_allowed ]] )
Reads a control file from file_handle and returns a nested list (or a list ref) stuffed with data from the file.
See the synopsis for the returned structure β it's all laid out there, no gatekeeping π
check is "yes", "skip" or "no" (default "yes"). comment_allowed is boolean (default 0).
π« parse_dpkg_lines π«
Parameters: ( file_path, lines, check, comment_allowed )
Parses the dpkg date from lines (which is an array ref) and returns a data structure just like parse_dpkg_file. Same energy, different entry point β¨
π«Ά write_dpkg_file π«Ά
Parameters ( list_ref, list_sep )
Munges the passed list ref into a string compatible with control files, and writes it in the passed file handle.
The input is a list of lists, in a form similar to the one generated by parse_dpkg_file. See the synopsis for an example π
List items (like the Depends field in debian/control) get joined with the value list_sep before being written. And the values are aligned in case of multi-line output of a list. The default value of list_sep is ",\n".
So for instance, after this code:
my $ref = [ [ Foo => 'foo value' , Bar => [ 'v1', 'v2' ] ];
my $res = write_dpkg_file ( $ref, ', ' )
$res contains:
Foo: foo value
Bar: v1, v2
And here's an example using the default $sep_list:
print write_dpkg_file ( $ref )
which yields:
Foo: foo value
Bar: v1,
v2
See how cute that alignment is?? The vibes are immaculate π
π AUTHOR π
Dominique Dumont, (ddumont at cpan dot org)
π SEE ALSO π
Config::ModelConfig::Model::BackendMgrConfig::Model::Backend::Any
And that's the whole page bestie β no cap, you understood all of it π§ This was perl v5.42.3, dated 2026-08-30, and remember: you can always re-read the docs if the vibe check fails ππΈπ
π¦ where this page comes from
- ubuntu stonking: libconfig-model-dpkg-perl 3.026 Β· LGPL-2.1-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms