<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">
=head1 NAME

Debian::Control - manage Debian source package control files

=head1 SYNOPSIS

    my $c = Debian::Control-&gt;new();         # construct a new
    $c-&gt;read($file);                        # parse debian/control file
    $c-&gt;write($file);                       # write to file
    print $c-&gt;source-&gt;Source;
    print $c-&gt;source-&gt;Build_Depends;        # Debian::Dependencies object
    $c-&gt;binary-&gt;{'libfoo-perl'}-&gt;Description(
        "Foo Perl module\n" .
        " Foo makes this and that"
    );

=head1 DESCRIPTION

Debian::Control can be used for representation and manipulation of Debian
source package control files in an object-oriented way. It provides easy
reading and writing of the F&lt;debian/control&gt; file found in Debian source
packages.

=head1 FIELDS

=over

=item source

An instance of L&lt;Debian::Control::Stanza::Source&gt; class. Contains the source
stanza of the Debian source package control file.

=item binary

A hash reference with keys being binary
package names and values instances of L&lt;Debian::Control::Stanza::Binary&gt; class.
Contains the information of the binary package stanzas of Debian source package
control file.

=item binary_tie

A L&lt;Tie::IxHash&gt; object tied to the B&lt;binary&gt; hash.

=back

=cut

package Debian::Control;

use base 'Class::Accessor';
use strict;
use warnings;

our $VERSION = '0.77';

__PACKAGE__-&gt;mk_accessors(qw( source binary binary_tie _parser ));

use Parse::DebControl;
use Debian::Control::Stanza::Source;
use Debian::Control::Stanza::Binary;

=head1 CONSTRUCTOR

=over

=item new

Constructs a new L&lt;Debian::Control&gt; instance.

The C&lt;source&gt; field is initialized with an empty instance of
L&lt;Debian::Control::Stanza::Source&gt; and C&lt;binary&gt; field is initialized with an
empty instance of L&lt;Tie::IxHash&gt;.

=back

=cut

sub new {
    my $class = shift;

    my $self = $class-&gt;SUPER::new();

    $self-&gt;_parser( Parse::DebControl-&gt;new );

    my %b;
    $self-&gt;binary_tie( tie %b, 'Tie::IxHash' );
    $self-&gt;binary( \%b );
    $self-&gt;source( Debian::Control::Stanza::Source-&gt;new );

    return $self;
}

=head1 METHODS

=over

=item read I&lt;file&gt;

Parse L&lt;debian/control&gt; and populate C&lt;source&gt; and C&lt;binary&gt; accessors.

I&lt;file&gt; can be either a file name, an opened file handle or a string scalar
reference.

=cut

sub read {
    my ( $self, $file ) = @_;

    my $parser_method = 'parse_file';

    if ( ref($file) ) {
        $file          = $$file;
        $parser_method = 'parse_mem';
    }

    my $stanzas = $self-&gt;_parser-&gt;$parser_method( $file,
        { useTieIxHash =&gt; 1, verbMultiLine =&gt; 1 } );

    for (@$stanzas) {
        if ( $_-&gt;{Source} ) {
            $self-&gt;source( Debian::Control::Stanza::Source-&gt;new($_) );
        }
        elsif ( $_-&gt;{Package} ) {
            $self-&gt;binary_tie-&gt;Push(
                $_-&gt;{Package} =&gt; Debian::Control::Stanza::Binary-&gt;new($_) );
        }
        else {
            die "Got control stanza with neither Source nor Package field\n";
        }
    }
}

=item write I&lt;file&gt;

Writes a debian/control-like file in I&lt;file&gt; with the contents defined in the
C&lt;source&gt; and C&lt;binary&gt; fields.

I&lt;file&gt; can be either a file name, an opened file handle or a string scalar
reference.

All dependency lists are sorted before writing.

=cut

sub write {
    my ( $self, $file ) = @_;

    for my $s ( $self-&gt;source, $self-&gt;binary_tie-&gt;Values ) {
        for ( $s-&gt;fields ) {
            $s-&gt;$_-&gt;sort if $s-&gt;is_dependency_list($_);
        }
    }

    if ( ref($file) and ref($file) eq 'SCALAR' ) {
        $$file = join( "\n", $self-&gt;source, $self-&gt;binary_tie-&gt;Values );
    }
    elsif ( ref($file) and ref($file) eq 'GLOB' ) {
        $file-&gt;print( join( "\n", $self-&gt;source, $self-&gt;binary_tie-&gt;Values ) );
    }
    else {
        my $fh;
        open $fh, '&gt;', $file or die "Unable to open '$file' for writing: $!";

        print $fh join( "\n", $self-&gt;source, $self-&gt;binary_tie-&gt;Values );
    }
}

=item is_arch_dep

Returns true if the package is architecture-dependent. This is determined by
the C&lt;Architecture&gt; field of the first binary package. If it equals to C&lt;all&gt;,
then the package is architecture-independent; otherwise it is
architecture-dependent.

Returns I&lt;undef&gt; if it is not possible to determine whether the package is
architecture-dependent or not. This is the case when there are no binary
package stanzas present or the first has no C&lt;Archiitecture&gt; field.

=cut

sub is_arch_dep {
    my $self = shift;

    my $bin = $self-&gt;binary_tie-&gt;Values(0);

    return undef unless $bin;

    my $arch = $bin-&gt;Architecture;

    return undef unless defined($arch);

    return ( $arch ne 'all' );
}

=back

=head1 SEE ALSO

L&lt;Debian::Control::Stanza::Source&gt;, L&lt;Debian::Control::Stanza::Binary&gt;,
L&lt;Debian::Control::FromCPAN&gt;

=head1 COPYRIGHT &amp; LICENSE

Copyright (C) 2009 Damyan Ivanov L&lt;dmn@debian.org&gt;

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License version 2 as published by the Free
Software Foundation.

This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.

=cut

1;
</pre></body></html>