Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: (2) scratching the surface of File::Temp (unlink w/o $filehandle ?)

by ybiC (Prior)
on Feb 10, 2001 at 09:45 UTC ( #57578=note: print w/ replies, xml ) Need Help??


in reply to Re: scratching the surface of File::Temp
in thread (code) scratching the surface of File::Temp

Like you point out, a, if $fh is used instead of TMP, I lose $filehandle as soon as $fh is closed.   So how can I unlink0 with no $filehandle without generating "Use of uninitialized value @ /usr/local/lib/site_perl/File?Temp.pm line (1525|1584), <STDIN> chunk 1" ?

#!/usr/bin/perl -w use strict; use File::Temp qw(tempfile unlink0 ); use vars qw($fh $filename); my $template = 'fileXXXXXXXXXX'; my $dir = '/tmp/'; ($fh, $filename) = tempfile($template, DIR => $dir) or die "Error creating $filename: $!"; print $fh "\nS'working?\n" or die "Error writing to $filename: $!"; close $fh or die "Error closing $filename: $!"; print "\nUnlinking $filename\n<ENTER> to continue, CTRL+C to abort.\n" +; my $continue = (<STDIN>); unlink0 ($fh, $filename) or die "Error unlinking file $filename safely: $!"
I wouldn't be surprised if it's explained in the module's pod and I'm just not picking up on it.
    cheers,
    Don
    striving for Perl Adept
    (it's pronounced "why-bick")

Update: Good monk a says below "not supposed to close $fh... unlink0 will handle..."
A-ha!   that seems to do the trick. 8^)

Update #2: But if I don't "close $fh" then "print $fh "\nS'working?\n" or die;" doesn't really happen.   So I'm still doing something wrong 8^(


Comment on Re: (2) scratching the surface of File::Temp (unlink w/o $filehandle ?)
Download Code
Re: Re: (2) scratching the surface of File::Temp (unlink w/o $filehandle ?)
by a (Friar) on Feb 10, 2001 at 09:55 UTC
    Short answer: I don't think you can. Either you're not supposed to close $fh (unlink0 will handle that) or your first method: open a 2nd link and go from there. One hopes tilly|tye|chipmunk will be along w/ the correct answer real soon now ...

    a

      You have the correct answer. And indeed it is addressed in the documentation. unlink0 performs checks that depend on having both the filename and the open filehandle, and if you do the close yourself it cannot verify that the close was safe.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://57578]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (9)
As of 2014-08-29 15:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (281 votes), past polls