Linux-APT

 view release on metacpan or  search on metacpan

lib/Linux/APT.pm  view on Meta::CPAN


=cut

sub new
{
  my $class = shift;
  my $self = {};
  my %args = @_;

  $self->{debug} = $args{debug};

  $self->{aptget} = $args{aptget} || `which apt-get`;
  chomp($self->{aptget});
  die qq(apt-get doesn't appear to be available.\n) unless $self->{aptget};

  $self->{aptcache} = $args{aptcache} || `which apt-cache`;
  chomp($self->{aptcache});
  die qq(apt-cache doesn't appear to be available.\n) unless $self->{aptcache};

  return bless($self, $class);
}

=head2 update

  my $update = $apt->update;

  warn "There were errors...\n" if $update->{error};
  warn "There were warnings...\n" if $update->{warning};

Update apt cache.
Basically equivalent to C<apt-get update>.

Returns hashref containing these items:

=over

=item error

Arrayref of errors.

=item warning

Arrayref of warnings.

=item speed

Network transfer speed of update.

=item time

Wallclock time it took to update.

=item size

Amount of received transferred during update.

=back

=cut

sub update
{
  my $self = shift;
  my $update = {};

  if (open(APT, "$self->{aptget} -q update 2>&1 |"))
  {
    while (my $line = <APT>)
    {
      chomp($line);
      print qq($line\n) if $self->{debug};
      if ($line =~ m#Fetched (\d+\S+) in (.*?) \((\d+\S+?)\)#i)
      {
        $update->{size} = $1;
        $update->{time} = $2;
        $update->{speed} = $3;
      }
      elsif ($line =~ s#^W: ##) # warning
      {
        my $warning = {};
        $warning->{message} = $line;
        push(@{$update->{warning}}, $warning);
      }
      elsif ($line =~ s#^E: ##) # error
      {
        my $error = {};
        $error->{message} = $line;
        push(@{$update->{error}}, $error);
      }
    }
    close(APT);
  }
  else
  {
    die "Couldn't use APT: $!\n";
  }

  return $update;
}

=head2 toupgrade

  my $toupgrade = $apt->toupgrade;

Returns hashref of packages, errors, and warnings:

=over

=item warning

Warnings, if any.

=item error

Errors, if any.

=item packages

Contains a hashref of updateable packages.
Keys are package names.
Each update is a hashref containing these items:



( run in 0.506 second using v1.01-cache-2.11-cpan-39bf76dae61 )