#!/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}; } }