Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Re: Re: File deletion

by converter (Priest)
on Nov 01, 2002 at 06:06 UTC ( #209643=note: print w/ replies, xml ) Need Help??


in reply to Re: Re: File deletion
in thread File deletion

There's one minor problem with your example: your hash slice assignment creates the expected keys, so exists will work, but since you're assigning only one value to the slice, only the hash element with the key corresponding to the first element in @files will have a value (1), the rest will have the undefined value. This could lead to confusion (i.e. code that mysteriously refuses to work the way you expect it to) later on should you be required to make any changes that assume that each element of the hash has the value 1.

It would probably be best to either assign an empty list to the hash slice, which would give you a key for each filename in @files for use with exists, or to assign a list of true values with the same number of elements as the @files array using the repetition operator x.

@keep{@files} = (); # empty list @keep{@files} = (1) x @files; # list of "TRUE" values
With the latter assignment, you could modify the code to check the value of the hash element, instead of the key:
@keep{@files} = (1) x @files; foreach my $file ( @filelist ) { unlink $file unless $keep{$file}; }


Comment on Re: Re: Re: File deletion
Select or Download Code
Re^4: File deletion
by adrianh (Chancellor) on Nov 01, 2002 at 10:51 UTC

    ... and you should be testing the return value of unlink since it can fail :-)

    Something like this should work (untested code):

    @keep{@files} = (1) x @files; foreach my $file ( @filelist ) { next unless $keep{$file}; unlink $file == 1 or warn "could not unlink $file ($!)\n"; }

    (yes, I know that you could just test unlink for truth/false in this instance - but I always like to remind myself that it returns the number of successful deletions rather than success/failure).

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2014-08-29 04:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (275 votes), past polls