in reply to Re^8: manipulating cpio archive
in thread manipulating cpio archive

As I already wrote, this code is over my head. I would like to fix the old code temporarily
OLD BUGGED CODE: sub remove { my ($cpio, @filenames) = @_; $cpio->{list} or die "can't remove from nothing\n"; my %filenames = map { $_ => 1 } @filenames; @{$cpio->{list}} = grep { !$filenames{$_} } @{$cpio->{list}}; }
with something like this
WRONG NEW CODE sub remove { my ($cpio, @filenames) = @_; $cpio->{list} or die "can't remove from nothing\n"; foreach $item (@{$cpio->{list}}) { foreach $file (@filesnames) { if ($item->{name} eq $file){ delete @{$cpio2->{list}->{$item}}; } } } return; }
How should this be done?

Replies are listed 'Best First'.
Re^10: manipulating cpio archive
by Corion (Patriarch) on Jan 18, 2011 at 08:30 UTC

    The only flaw of the old code is that it directly compares elements from @{ $cpio->{list} } against the filenames. That is wrong because the elements in @{ $cpio->{list} } are Archive::Cpio::File objects. Instead, it should compare the elements against the ->name property. Most likely (but untested) like so:

    sub remove { my ($cpio, @filenames) = @_; $cpio->{list} or die "can't remove from nothing\n"; # Create a lookup table of names to be removed my %filenames = map { $_ => 1 } @filenames; # This was the old, buggy code # @{$cpio->{list}} = grep { !$filenames{$_} } @{$cpio->{list}}; # Remove the elements according to their name: @{$cpio->{list}} = grep { !$filenames{$_->name} } @{$cpio->{list}} +; }
      This seems to work (the first output was correct)
      Thank you!

        The author has just released Archive::Cpio v0.08, which contains the fix.