1 package ExtUtils::Packlist;
8 $VERSION = eval $VERSION;
10 # Used for generating filehandle globs. IO::File might not be available!
17 Make a filehandle. Same kind of idea as Symbol::gensym().
26 my $fh = \*{$fhname++};
33 my ($class, $packfile) = @_;
34 $class = ref($class) || $class;
36 tie(%self, $class, $packfile);
37 return(bless(\%self, $class));
42 my ($class, $packfile) = @_;
43 my $self = { packfile => $packfile };
45 $self->read($packfile) if (defined($packfile) && -f $packfile);
51 $_[0]->{data}->{$_[1]} = $_[2];
56 return($_[0]->{data}->{$_[1]});
61 my $reset = scalar(keys(%{$_[0]->{data}}));
62 return(each(%{$_[0]->{data}}));
67 return(each(%{$_[0]->{data}}));
72 return(exists($_[0]->{data}->{$_[1]}));
77 return(delete($_[0]->{data}->{$_[1]}));
82 %{$_[0]->{data}} = ();
91 my ($self, $packfile) = @_;
92 $self = tied(%$self) || $self;
94 if (defined($packfile)) { $self->{packfile} = $packfile; }
95 else { $packfile = $self->{packfile}; }
96 Carp::croak("No packlist filename specified") if (! defined($packfile));
98 open($fh, "<$packfile") || Carp::croak("Can't open file $packfile: $!");
101 while (defined($line = <$fh>))
104 my ($key, @kvs) = $line;
105 if ($key =~ /^(.*?)( \w+=.*)$/)
108 @kvs = split(' ', $2);
110 $key =~ s!/\./!/!g; # Some .packlists have spurious '/./' bits in the paths
113 $self->{data}->{$key} = undef;
118 foreach my $kv (@kvs)
120 my ($k, $v) = split('=', $kv);
123 $self->{data}->{$key} = $data;
131 my ($self, $packfile) = @_;
132 $self = tied(%$self) || $self;
133 if (defined($packfile)) { $self->{packfile} = $packfile; }
134 else { $packfile = $self->{packfile}; }
135 Carp::croak("No packlist filename specified") if (! defined($packfile));
137 open($fh, ">$packfile") || Carp::croak("Can't open file $packfile: $!");
138 foreach my $key (sort(keys(%{$self->{data}})))
141 if (ref($self->{data}->{$key}))
143 my $data = $self->{data}->{$key};
144 foreach my $k (sort(keys(%$data)))
146 print $fh (" $k=$data->{$k}");
156 my ($self, $remove) = @_;
157 $self = tied(%$self) || $self;
159 foreach my $key (sort(keys(%{$self->{data}})))
163 push(@missing, $key);
164 delete($self->{data}{$key}) if ($remove);
173 $self = tied(%$self) || $self;
174 return($self->{packfile});
183 ExtUtils::Packlist - manage .packlist files
187 use ExtUtils::Packlist;
188 my ($pl) = ExtUtils::Packlist->new('.packlist');
189 $pl->read('/an/old/.packlist');
190 my @missing_files = $pl->validate();
191 $pl->write('/a/new/.packlist');
193 $pl->{'/some/file/name'}++;
195 $pl->{'/some/other/file/name'} = { type => 'file',
196 from => '/some/file' };
200 ExtUtils::Packlist provides a standard way to manage .packlist files.
201 Functions are provided to read and write .packlist files. The original
202 .packlist format is a simple list of absolute pathnames, one per line. In
203 addition, this package supports an extended format, where as well as a filename
204 each line may contain a list of attributes in the form of a space separated
205 list of key=value pairs. This is used by the installperl script to
206 differentiate between files and links, for example.
210 The hash reference returned by the new() function can be used to examine and
211 modify the contents of the .packlist. Items may be added/deleted from the
212 .packlist by modifying the hash. If the value associated with a hash key is a
213 scalar, the entry written to the .packlist by any subsequent write() will be a
214 simple filename. If the value is a hash, the entry written will be the
215 filename followed by the key=value pairs from the hash. Reading back the
216 .packlist will recreate the original entries.
224 This takes an optional parameter, the name of a .packlist. If the file exists,
225 it will be opened and the contents of the file will be read. The new() method
226 returns a reference to a hash. This hash holds an entry for each line in the
227 .packlist. In the case of old-style .packlists, the value associated with each
228 key is undef. In the case of new-style .packlists, the value associated with
229 each key is a hash containing the key=value pairs following the filename in the
234 This takes an optional parameter, the name of the .packlist to be read. If
235 no file is specified, the .packlist specified to new() will be read. If the
236 .packlist does not exist, Carp::croak will be called.
240 This takes an optional parameter, the name of the .packlist to be written. If
241 no file is specified, the .packlist specified to new() will be overwritten.
245 This checks that every file listed in the .packlist actually exists. If an
246 argument which evaluates to true is given, any missing files will be removed
247 from the internal hash. The return value is a list of the missing files, which
248 will be empty if they all exist.
250 =item packlist_file()
252 This returns the name of the associated .packlist file
258 Here's C<modrm>, a little utility to cleanly remove an installed module.
260 #!/usr/local/bin/perl -w
264 use ExtUtils::Packlist;
265 use ExtUtils::Installed;
269 my $dh = IO::Dir->new($dir) || return(0);
270 my @count = $dh->read();
272 return(@count == 2 ? 1 : 0);
275 # Find all the installed packages
276 print("Finding all installed modules...\n");
277 my $installed = ExtUtils::Installed->new();
279 foreach my $module (grep(!/^Perl$/, $installed->modules())) {
280 my $version = $installed->version($module) || "???";
281 print("Found module $module Version $version\n");
282 print("Do you want to delete $module? [n] ");
283 my $r = <STDIN>; chomp($r);
284 if ($r && $r =~ /^y/i) {
285 # Remove all the files
286 foreach my $file (sort($installed->files($module))) {
290 my $pf = $installed->packlist($module)->packlist_file();
293 foreach my $dir (sort($installed->directory_tree($module))) {
294 if (emptydir($dir)) {
295 print("rmdir $dir\n");
304 Alan Burlison <Alan.Burlison@uk.sun.com>