Photius has asked for the wisdom of the Perl Monks concerning the following question:
I would greatly appreaciate any direction the wise monks can provide. Thanks!#!C:/Perl/bin/perl /w # CloseUnlinkTest.pl use strict; my $file1 = 'file1.txt'; my $file2 = 'file2.txt'; my $file3 = 'file3.txt'; open(my $handle1, ">", $file1) or &quit({}, "Cannot open $file1: $!"); + # Quit, dont need to close any files open(my $handle2, ">", $file2) or &quit({$handle1=>0}, "Cannot open $f +ile2: $!"); # Quit, close $file1 open(my $handle3, ">", $file3) or &quit({$handle1=>0, $handle2=>0}, "C +annot open $file3: $!"); # Quit, close $file1 and file2 # Do stuff with the files ... &quit({$handle1=>1, $handle2=>1, $handle3=>1}, "CloseUnlinkTest.pl fin +ished"); # Quit, close and unlink all 3 files sub quit { my ($filesToClose, $message) = @_; + # Get calling parameters my %filesToClose = %$filesToClose; + # Dereference $filesToClose into hash %filesToClose print "\%filesToClose = @{[%filesToClose]}\n"; + # Debug to show hash contents print "message = $message\n"; + while (my ($file, $unlink) = each %filesToClose) { + # Loop thru the files to be closed print "closing $file with $unlink\n"; + # Debug to show the key and value of this hash entry close($file) or print "Error closing $file: $!\n"; + # Close this file unlink($file) or print "Error unlinking $$file: $!\n" if $unlink; + # Unlink this file if $unlink is not zero } exit; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Trouble Passing File Handles
by johngg (Canon) on Jun 16, 2010 at 23:04 UTC | |
by Photius (Sexton) on Jun 17, 2010 at 14:22 UTC | |
|
Re: Trouble Passing File Handles
by coldguy (Sexton) on Jun 16, 2010 at 22:34 UTC | |
by Photius (Sexton) on Jun 17, 2010 at 14:17 UTC | |
|
Re: Trouble Passing File Handles
by derby (Abbot) on Jun 17, 2010 at 12:25 UTC | |
by Photius (Sexton) on Jun 17, 2010 at 14:32 UTC | |
|
Re: Trouble Passing File Handles
by zek152 (Pilgrim) on Jun 17, 2010 at 14:11 UTC |