in reply to Trouble Passing File Handles

coldguy was right, you cannot use a reference as a hash key. Also, I don't think you can use unlink on a filehandle ... just a filename. If you want to stay the same path, I would just expand your structure a bit:

#!/usr/local/bin/perl use strict; my $files = [ { name => 'file1.txt', fh => undef, unlink => 1 }, { name => 'file2.txt', fh => undef, unlink => 1 }, { name => 'file3.txt', fh => undef, unlink => 1 }, ]; foreach my $file ( @$files ) { open( $file->{fh}, '>', $file->{name} ) or quit( $files, "Cannot open $file->{name}: $!" ); } quit( $files, "CloseUnlinkTest.pl finished" ); sub quit { my( $files, $message ) = @_; foreach my $file ( @$files ) { close( $file->{fh} ) if $file->{fh}; unlink( $file->{name} ) if $file->{unlink}; } }

-derby

Replies are listed 'Best First'.
Re^2: Trouble Passing File Handles
by Photius (Sexton) on Jun 17, 2010 at 14:32 UTC
    Thanks a 10^6 !!! This will work perfectly. I like your clean elegant solution. I should wander the monastery halls more often! I always learn new things. -- photius