Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Override tempdir DESTROY

by mmaule (Initiate)
on Nov 08, 2012 at 22:27 UTC ( #1003009=perlquestion: print w/replies, xml ) Need Help??
mmaule has asked for the wisdom of the Perl Monks concerning the following question:

Hi: Fairly new to perl as you can guess by this question. I'm using tempdir from File::Temp. My script needs to do some pre-delete processing on files in the tempdir prior to it being cleaned up. What is the correct way to do that? To be more specific - some of the files in tempdir will get created with a user other than $< (using sudo on Linux). I need to have a hook whereby I can revert the ownership to $< prior to DESTROY so that they can be cleaned up. thanks much Mark

Replies are listed 'Best First'.
Re: Override tempdir DESTROY
by tobyink (Abbot) on Nov 08, 2012 at 23:00 UTC

    Subclass File::Temp::Dir and override the DESTROY method. This is somewhat complicated by the fact that File::Temp::Dir doesn't have a traditional constructor.

    package My::File::Temp::Dir; use File::Temp (); our @ISA = qw(File::Temp::Dir); sub new { my $class = shift; my $self = File::Temp->newdir(@_); bless $self, $class; } sub DESTROY { my $tempdir = shift; ...; # do stuff $tempdir->SUPER::DESTROY(@_); } 1;

    Then, stop using the tempdir function and use My::File::Temp::Dir->new instead.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: Override tempdir DESTROY (wrap)
by tye (Sage) on Nov 08, 2012 at 22:52 UTC

    Write your own, tiny class. Have your class's new() construct the File::Temp object. Have your DESTROY do the steps needed (File::Temp's DESTROY will automatically be called when you clear the File::Temp object attribute from your your object or after your object gets cleared out). Your object could just be a blessed scalar reference to the File::Temp object, for example. Your new() can also do post-creation steps, if needed.

    - tye        

Re: Override tempdir DESTROY
by runrig (Abbot) on Nov 08, 2012 at 22:52 UTC
    Create a ReleaseAction or a Guard that does the cleanup. You could create another class with a DESTROY method as others suggest, but I think this is more lightweight (or at least a pre-shrink-wrapped way to do the same thing).
Re: Override tempdir DESTROY
by remiah (Hermit) on Nov 08, 2012 at 22:38 UTC

    Hello. Maybe...

    perldoc -tf chown perldoc -tf chmod

      Right. But I need to be able to do chown/chmod when the tempdir DESTROY is method is destroyed. The issue is that under the tempdir, I have files that are undeletable by the current user (they were created earlier in the script and are owned by a user other then the current user). So, when my script exits, either normally or asynchronously via an error path/interrupt, the files under the tempdir do not get cleaned up. What I would like is a hook whereby I have the opportunity to reset ownership on the files prior to when the tempdir DESTROY method tries to unlink the files.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1003009]
Approved by Perlbotics
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (7)
As of 2018-04-21 16:28 GMT
Find Nodes?
    Voting Booth?