File-Append-TempFile
view release on metacpan or search on metacpan
lib/File/Append/TempFile.pm view on Meta::CPAN
$self->{stat} = [ @stat ];
return 1;
}
=item add_line (DATA)
Append data to the temporary file. This does not affect the original in
any way until C<commit()> is invoked.
=cut
sub add_line($ $)
{
my ($self, $line) = @_;
my $f = $self->{f};
if (!defined $f) {
$self->err("Cannot add_line() to an unopened tempfile");
return undef;
}
$self->debug("RDBG about to add a line to $f for $self->{fname}\n");
if (!(print $f $line)) {
$self->err("Could not add to the tempfile: $!");
return undef;
}
return 1;
}
=item commit ()
Replace the original file with the temporary copy, to which data may have
been added using C<add_line()>.
B<NOTE:> This method uninitializes the C<File::Append::TempFile> object,
that is, removes B<any> association between it and the original file and
even file name! The next method invoked on this C<File::Append::TempFile>
object should be C<begin_work()>.
=cut
sub commit($)
{
my ($self) = @_;
my $f = $self->{f};
if (!defined $f || !defined $self->{fname}) {
$self->err("Cannot commit an unopened tempfile");
return undef;
}
$self->debug("RDBG about to commit $f to $self->{fname}\n");
# Fix stuff up
if (defined $self->{stat}) {
# Mode
if (!chmod $self->{stat}->[2], $f) {
$self->err("Could not chmod $self->{stat}->[2] ".
"$f: $!");
return undef;
}
# Owner & group
if (!chown $self->{stat}->[4], $self->{stat}->[5], $f) {
$self->err("Could not chown $self->{stat}->[4], ".
"$self->{stat}->[5], $f: $!");
return undef;
}
}
if (!rename $f, $self->{fname}) {
$self->err("Renaming $f to $self->{fname}: $!");
return undef;
}
$f->unlink_on_destroy(0);
close $f;
$self->debug("RDBG successfully committed $f to $self->{fname}\n");
$self->{fname} = $self->{f} = undef;
return 1;
}
=item rollback ()
Discard all the changes made to the temporary copy and remove it. This
does not affect the original file in any way.
B<NOTE:> This method uninitializes the C<File::Append::TempFile> object,
that is, removes B<any> association between it and the original file and
even file name! The next method invoked on this C<File::Append::TempFile>
object should be C<begin_work()>.
=cut
sub rollback($)
{
my ($self) = @_;
$self->debug(ref($self)."->rollback() for $self->{fname}\n");
if (defined $self->{f}) {
my $f = $self->{f};
$self->debug("RDBG closing and removing $f\n");
$f->unlink_on_destroy(1);
close $f;
undef $self->{f};
}
undef $self->{fname};
$self->debug("RDBG rollback seems complete\n");
return 1;
}
=back
There are also several methods used internally by the
C<File::Append::TempFile> routines:
=over 4
=item debug (MESSAGE)
Display a diagnostic message to the standard error stream if the output
of diagnostic messages has been enabled.
=cut
sub debug($ $)
( run in 0.840 second using v1.01-cache-2.11-cpan-71847e10f99 )