Wroof has asked for the wisdom of the Perl Monks concerning the following question:
Good morning Monks,
I am trying to write up a short test program to learn how to deal with files in Perl before I try and implement it into a program I have already written.
The idea of the test program is to open a file for appending and if the file does not exist create it and open it. The software in the end will be used to create a log in another program to help me track down an error that is occurring around once every 40-50 days.
What I have at the moment is just trying to open a file for appending/create the file, and then close the file before testing that it exists. This is all based on the IO:File Lib. The function $fh->open(">> test2.txt") always seams to return true even if the files does not exist and it does not create it either.
use IO::File; use strict; my $fh; sub test { my $filename = 'test2.txt'; unless (-f $filename) { print "File Exists!"; } else { print "File does not exist"; } } sub openFile { $fh = IO::File->new(); if ($fh->open(">> test2.txt")) { print "file open"; print <$fh>; $fh->close; } else { print "file failed to open"; } } openFile(); test();
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Check if a file exists and if not create it
by GotToBTru (Prior) on Aug 20, 2014 at 21:05 UTC | |
by Wroof (Novice) on Aug 20, 2014 at 21:26 UTC | |
|
Re: Check if a file exists and if not create it
by Your Mother (Archbishop) on Aug 20, 2014 at 21:50 UTC | |
by Your Mother (Archbishop) on Aug 22, 2014 at 01:52 UTC | |
|
Re: Check if a file exists and if not create it
by Laurent_R (Canon) on Aug 20, 2014 at 21:11 UTC | |
|
Re: Check if a file exists and if not create it - use sysopen
by Discipulus (Canon) on Aug 21, 2014 at 11:56 UTC | |
by t-rex (Scribe) on Jun 24, 2016 at 08:34 UTC | |
by Discipulus (Canon) on Jun 24, 2016 at 08:57 UTC | |
|
Re: Check if a file exists and if not create it
by Aldebaran (Curate) on Aug 21, 2014 at 21:59 UTC |