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::Model
  • Config::Model::BackendMgr
  • Config::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