1 package ExtUtils::Packlist;
9 # Used for generating filehandle globs. IO::File might not be available!
15 my $fh = \*{$fhname++};
22 my ($class, $packfile) = @_;
23 $class = ref($class) || $class;
25 tie(%self, $class, $packfile);
26 return(bless(\%self, $class));
31 my ($class, $packfile) = @_;
32 my $self = { packfile => $packfile };
34 $self->read($packfile) if (defined($packfile) && -f $packfile);
40 $_[0]->{data}->{$_[1]} = $_[2];
45 return($_[0]->{data}->{$_[1]});
50 my $reset = scalar(keys(%{$_[0]->{data}}));
51 return(each(%{$_[0]->{data}}));
56 return(each(%{$_[0]->{data}}));
61 return(exists($_[0]->{data}->{$_[1]}));
66 return(delete($_[0]->{data}->{$_[1]}));
71 %{$_[0]->{data}} = ();
80 my ($self, $packfile) = @_;
81 $self = tied(%$self) || $self;
83 if (defined($packfile)) { $self->{packfile} = $packfile; }
84 else { $packfile = $self->{packfile}; }
85 Carp::croak("No packlist filename specified") if (! defined($packfile));
87 open($fh, "<$packfile") || Carp::croak("Can't open file $packfile: $!");
90 while (defined($line = <$fh>))
93 my ($key, @kvs) = $line;
94 if ($key =~ /^(.*?)( \w+=.*)$/)
97 @kvs = split(' ', $2);
99 $key =~ s!/\./!/!g; # Some .packlists have spurious '/./' bits in the paths
102 $self->{data}->{$key} = undef;
107 foreach my $kv (@kvs)
109 my ($k, $v) = split('=', $kv);
112 $self->{data}->{$key} = $data;
120 my ($self, $packfile) = @_;
121 $self = tied(%$self) || $self;
122 if (defined($packfile)) { $self->{packfile} = $packfile; }
123 else { $packfile = $self->{packfile}; }
124 Carp::croak("No packlist filename specified") if (! defined($packfile));
126 open($fh, ">$packfile") || Carp::croak("Can't open file $packfile: $!");
127 foreach my $key (sort(keys(%{$self->{data}})))
130 if (ref($self->{data}->{$key}))
132 my $data = $self->{data}->{$key};
133 foreach my $k (sort(keys(%$data)))
135 print $fh (" $k=$data->{$k}");
145 my ($self, $remove) = @_;
146 $self = tied(%$self) || $self;
148 foreach my $key (sort(keys(%{$self->{data}})))
152 push(@missing, $key);
153 delete($self->{data}{$key}) if ($remove);
162 $self = tied(%$self) || $self;
163 return($self->{packfile});
172 ExtUtils::Packlist - manage .packlist files
176 use ExtUtils::Packlist;
177 my ($pl) = ExtUtils::Packlist->new('.packlist');
178 $pl->read('/an/old/.packlist');
179 my @missing_files = $pl->validate();
180 $pl->write('/a/new/.packlist');
182 $pl->{'/some/file/name'}++;
184 $pl->{'/some/other/file/name'} = { type => 'file',
185 from => '/some/file' };
189 ExtUtils::Packlist provides a standard way to manage .packlist files.
190 Functions are provided to read and write .packlist files. The original
191 .packlist format is a simple list of absolute pathnames, one per line. In
192 addition, this package supports an extended format, where as well as a filename
193 each line may contain a list of attributes in the form of a space separated
194 list of key=value pairs. This is used by the installperl script to
195 differentiate between files and links, for example.
199 The hash reference returned by the new() function can be used to examine and
200 modify the contents of the .packlist. Items may be added/deleted from the
201 .packlist by modifying the hash. If the value associated with a hash key is a
202 scalar, the entry written to the .packlist by any subsequent write() will be a
203 simple filename. If the value is a hash, the entry written will be the
204 filename followed by the key=value pairs from the hash. Reading back the
205 .packlist will recreate the original entries.
213 This takes an optional parameter, the name of a .packlist. If the file exists,
214 it will be opened and the contents of the file will be read. The new() method
215 returns a reference to a hash. This hash holds an entry for each line in the
216 .packlist. In the case of old-style .packlists, the value associated with each
217 key is undef. In the case of new-style .packlists, the value associated with
218 each key is a hash containing the key=value pairs following the filename in the
223 This takes an optional parameter, the name of the .packlist to be read. If
224 no file is specified, the .packlist specified to new() will be read. If the
225 .packlist does not exist, Carp::croak will be called.
229 This takes an optional parameter, the name of the .packlist to be written. If
230 no file is specified, the .packlist specified to new() will be overwritten.
234 This checks that every file listed in the .packlist actually exists. If an
235 argument which evaluates to true is given, any missing files will be removed
236 from the internal hash. The return value is a list of the missing files, which
237 will be empty if they all exist.
239 =item packlist_file()
241 This returns the name of the associated .packlist file
247 Here's C<modrm>, a little utility to cleanly remove an installed module.
249 #!/usr/local/bin/perl -w
253 use ExtUtils::Packlist;
254 use ExtUtils::Installed;
258 my $dh = IO::Dir->new($dir) || return(0);
259 my @count = $dh->read();
261 return(@count == 2 ? 1 : 0);
264 # Find all the installed packages
265 print("Finding all installed modules...\n");
266 my $installed = ExtUtils::Installed->new();
268 foreach my $module (grep(!/^Perl$/, $installed->modules())) {
269 my $version = $installed->version($module) || "???";
270 print("Found module $module Version $version\n");
271 print("Do you want to delete $module? [n] ");
272 my $r = <STDIN>; chomp($r);
273 if ($r && $r =~ /^y/i) {
274 # Remove all the files
275 foreach my $file (sort($installed->files($module))) {
279 my $pf = $installed->packlist($module)->packlist_file();
282 foreach my $dir (sort($installed->directory_tree($module))) {
283 if (emptydir($dir)) {
284 print("rmdir $dir\n");
293 Alan Burlison <Alan.Burlison@uk.sun.com>