1 package ExtUtils
::Packlist
;
8 # Used for generating filehandle globs. IO::File might not be available!
14 my $fh = \
*{$fhname++};
21 my ($class, $packfile) = @_;
22 $class = ref($class) || $class;
24 tie
(%self, $class, $packfile);
25 return(bless(\
%self, $class));
30 my ($class, $packfile) = @_;
31 my $self = { packfile
=> $packfile };
33 $self->read($packfile) if (defined($packfile) && -f
$packfile);
39 $_[0]->{data
}->{$_[1]} = $_[2];
44 return($_[0]->{data
}->{$_[1]});
49 my $reset = scalar(keys(%{$_[0]->{data
}}));
50 return(each(%{$_[0]->{data
}}));
55 return(each(%{$_[0]->{data
}}));
60 return(exists($_[0]->{data
}->{$_[1]}));
65 return(delete($_[0]->{data
}->{$_[1]}));
70 %{$_[0]->{data
}} = ();
79 my ($self, $packfile) = @_;
80 $self = tied(%$self) || $self;
82 if (defined($packfile)) { $self->{packfile
} = $packfile; }
83 else { $packfile = $self->{packfile
}; }
84 Carp
::croak
("No packlist filename specified") if (! defined($packfile));
86 open($fh, "<$packfile") || Carp
::croak
("Can't open file $packfile: $!");
89 while (defined($line = <$fh>))
92 my ($key, @kvs) = split(' ', $line);
93 $key =~ s!/\./!/!g; # Some .packlists have spurious '/./' bits in the paths
96 $self->{data
}->{$key} = undef;
101 foreach my $kv (@kvs)
103 my ($k, $v) = split('=', $kv);
106 $self->{data
}->{$key} = $data;
114 my ($self, $packfile) = @_;
115 $self = tied(%$self) || $self;
116 if (defined($packfile)) { $self->{packfile
} = $packfile; }
117 else { $packfile = $self->{packfile
}; }
118 Carp
::croak
("No packlist filename specified") if (! defined($packfile));
120 open($fh, ">$packfile") || Carp
::croak
("Can't open file $packfile: $!");
121 foreach my $key (sort(keys(%{$self->{data
}})))
124 if (ref($self->{data
}->{$key}))
126 my $data = $self->{data
}->{$key};
127 foreach my $k (sort(keys(%$data)))
129 print $fh (" $k=$data->{$k}");
139 my ($self, $remove) = @_;
140 $self = tied(%$self) || $self;
142 foreach my $key (sort(keys(%{$self->{data
}})))
146 push(@missing, $key);
147 delete($self->{data
}{$key}) if ($remove);
156 $self = tied(%$self) || $self;
157 return($self->{packfile
});
166 ExtUtils::Packlist - manage .packlist files
170 use ExtUtils::Packlist;
171 my ($pl) = ExtUtils::Packlist->new('.packlist');
172 $pl->read('/an/old/.packlist');
173 my @missing_files = $pl->validate();
174 $pl->write('/a/new/.packlist');
176 $pl->{'/some/file/name'}++;
178 $pl->{'/some/other/file/name'} = { type => 'file',
179 from => '/some/file' };
183 ExtUtils::Packlist provides a standard way to manage .packlist files.
184 Functions are provided to read and write .packlist files. The original
185 .packlist format is a simple list of absolute pathnames, one per line. In
186 addition, this package supports an extended format, where as well as a filename
187 each line may contain a list of attributes in the form of a space separated
188 list of key=value pairs. This is used by the installperl script to
189 differentiate between files and links, for example.
193 The hash reference returned by the new() function can be used to examine and
194 modify the contents of the .packlist. Items may be added/deleted from the
195 .packlist by modifying the hash. If the value associated with a hash key is a
196 scalar, the entry written to the .packlist by any subsequent write() will be a
197 simple filename. If the value is a hash, the entry written will be the
198 filename followed by the key=value pairs from the hash. Reading back the
199 .packlist will recreate the original entries.
207 This takes an optional parameter, the name of a .packlist. If the file exists,
208 it will be opened and the contents of the file will be read. The new() method
209 returns a reference to a hash. This hash holds an entry for each line in the
210 .packlist. In the case of old-style .packlists, the value associated with each
211 key is undef. In the case of new-style .packlists, the value associated with
212 each key is a hash containing the key=value pairs following the filename in the
217 This takes an optional parameter, the name of the .packlist to be read. If
218 no file is specified, the .packlist specified to new() will be read. If the
219 .packlist does not exist, Carp::croak will be called.
223 This takes an optional parameter, the name of the .packlist to be written. If
224 no file is specified, the .packlist specified to new() will be overwritten.
228 This checks that every file listed in the .packlist actually exists. If an
229 argument which evaluates to true is given, any missing files will be removed
230 from the internal hash. The return value is a list of the missing files, which
231 will be empty if they all exist.
233 =item packlist_file()
235 This returns the name of the associated .packlist file
241 Here's C<modrm>, a little utility to cleanly remove an installed module.
243 #!/usr/local/bin/perl -w
247 use ExtUtils::Packlist;
248 use ExtUtils::Installed;
252 my $dh = IO::Dir->new($dir) || return(0);
253 my @count = $dh->read();
255 return(@count == 2 ? 1 : 0);
258 # Find all the installed packages
259 print("Finding all installed modules...\n");
260 my $installed = ExtUtils::Installed->new();
262 foreach my $module (grep(!/^Perl$/, $installed->modules())) {
263 my $version = $installed->version($module) || "???";
264 print("Found module $module Version $version\n");
265 print("Do you want to delete $module? [n] ");
266 my $r = <STDIN>; chomp($r);
267 if ($r && $r =~ /^y/i) {
268 # Remove all the files
269 foreach my $file (sort($installed->files($module))) {
273 my $pf = $installed->packlist($module)->packlist_file();
276 foreach my $dir (sort($installed->directory_tree($module))) {
277 if (emptydir($dir)) {
278 print("rmdir $dir\n");
287 Alan Burlison <Alan.Burlison@uk.sun.com>