I am using ActiveState Perl 5.8.8 in Windows XP and Windows Server 2003. I want to create a subroutine to close and optionally unlink one or more open files. If I understand correctly, close takes a file handle and unlink takes a filename or glob (but not a handle). So the first challenge is that I really don't want to have to send both the file handles *and* filenames to the subroutine. But apparently I'm also doing something more basically stupid, because my subroutine does not succeed in even closing the files with the handles I'm sending it (which were opened as $fh variables). 1) What am I doing wrong with closing the files using the passed handles? 2) Any suggestions on how a single subroutine can both close and unlink a file without having to pass it both the file handle and filename?
#!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;
}
I would greatly appreaciate any direction the wise monks can provide.
Thanks!
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.