MSDOS-Descript
view release on metacpan or search on metacpan
lib/MSDOS/Descript.pm view on Meta::CPAN
{
my ($self,$in) = @_;
$in = $self->{file} unless $in;
%{$self->{desc}} = ();
$self->read_add($in);
delete $self->{changed} if $in eq $self->{file};
} # end read
#---------------------------------------------------------------------
# Add descriptions from a file to the current database:
#
# Input:
# IN: The name of the file to read
sub read_add
{
my ($self,$in) = @_;
if (-r $in) {
open(DESCRIPT, $in) or croak "Unable to open $in";
while (<DESCRIPT>) {
m/^\"([^\"]+)\" (.+)$/ or m/^([^ ]+) (.+)$/ or die;
$self->{desc}{$1} = $2;
}
close DESCRIPT;
}
$self->{changed} = 1;
} # end read_add
#---------------------------------------------------------------------
# Write the 4DOS description file:
#
# Sets the CHANGED flag to 0 if writing to our FILE.
sub write
{
my ($self, $out) = @_;
$out = $self->{file} unless $out;
my ($file, $desc);
unlink $out;
if (keys %{$self->{desc}}) {
open(DESCRIPT,">$out") or croak "Unable to open $out for writing";
while (($file,$desc) = each %{$self->{desc}}) {
next unless $desc;
$file = '"' . $file . '"' if $file =~ /\s/;
print DESCRIPT $file,' ',$desc,"\n";
}
close DESCRIPT;
set_attribs('+h',$out) if $hide_descriptions;
}
$self->{changed} = 0 if $out eq $self->{file};
} # end write
#---------------------------------------------------------------------
# Save changes to descriptions:
sub update
{
$_[0]->write if $_[0]->changed;
} # end update
#=====================================================================
# Package Return Value:
1;
__END__
=head1 NAME
MSDOS::Descript - Manage 4DOS style DESCRIPT.ION files
=head1 VERSION
This document describes version 1.05 of
MSDOS::Descript, released September 20, 2014.
=head1 SYNOPSIS
use MSDOS::Descript;
$d = new MSDOS::Descript;
print $d->description('foo.txt');
$d->rename('foo.txt', 'bar.txt');
$d->description('baz.txt','This is Baz.txt');
$d->description('frotz.txt', ''); # Remove description for frotz.txt
$d->update;
=head1 DESCRIPTION
MSDOS::Descript provides access to 4DOS style DESCRIPT.ION files.
Remember that changes to the descriptions are B<not> saved unless you
call the C<update> or C<write> methods.
By default, MSDOS::Descript uses relative paths, so if you change
the current directory between C<new> and C<update>, you'll be writing
to a different file. To avoid this, you can pass an absolute path to
C<new>.
=head2 Methods
=over 4
=item C<< $d = MSDOS::Descript->new([$filename]) >>
Constructs a new C<MSDOS::Descript> object. C<$filename> may be a
directory or a 4DOS DESCRIPT.ION format file. If it's a directory,
looks for a DESCRIPT.ION file in that directory. If C<$filename> is
omitted, it defaults to the current directory.
=item C<< $d->description($file, [$desc]) >>
Gets or sets the description of C<$file>. If C<$desc> is omitted,
returns the description of C<$file> or C<undef> if it doesn't have
one. Otherwise, sets the description of C<$file> to C<$desc> and
returns the old description. (If C<$desc> is the null string or
C<undef>, the description is deleted.)
( run in 3.131 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )