The easiest way is to use a module like IO::File, and either undefine the filehandle when you're finished writing to it, or have it close automatically when it goes out of scope.
Here's one way (going out of scope):
use strict; use warnings; use IO::File; # Create a new filehandle, and write to 'file.txt' my $fname = "file.txt"; { my $fh = new IO::File($fname, ">"); print $fh "Here's a single line of text\n"; # Close the file once the user types [RETURN] print "Type [RETURN] to close file ...\n"; <STDIN>; } # When this block ends, the enclose "scope" ends, and $fh is delet +ed print "Go check whether '$fname' was created (from a separate process) + ...\n"; while (1) { sleep 3; print "Still here... (type ^C when finished)\n"; }
And here's another (explicit deletion using undef) ...
use strict; use warnings; use IO::File; # Create a new filehandle, and write to 'file.txt' my $fname = "file.txt"; my $fh = new IO::File($fname, ">"); print $fh "Here's a single line of text\n"; # Close the file once the user types [RETURN] print "Type [RETURN] to close file ...\n"; <STDIN>; undef $fh; # Undefine the filehandle and it will close automati +cally print "Go check whether '$fname' was created (from a separate process) + ...\n"; while (1) { sleep 3; print "Still here... (type ^C when finished)\n"; }
In reply to Re: module which tells more warning about FILHANDLE
by liverpole
in thread module which tells more warning about FILHANDLE
by jesuashok
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |