I need a function that checks if file existsSeems to me that (on Windows) we can be assured that the specified $file exists if any one of the following three conditions returns true:
-T $file || -B $file
defined -T $file
defined -B $file
I was also wondering about using -f instead of -e. Are there any files on Windows that will report false for -f ? (If so, then we can't use -f as a test for existence.)
Someone really ought to file a bug report about this because the
perldoc -f -X documentation does *not* match the behaviour wrt '-e'.
That documentation implies that
-e '""' will return true only if a file named
"" exists.
Either the behaviour ought to change to fit the documentation, or the documentation be amended to fit the behaviour.
The documentation also implies that
defined -f '""' will return false (but it doesn't).
Here's what I ran as a check:
#!perl -l
use warnings;
print "-f:";
-f '""'
? print "Returned True (unwanted behaviour)\n"
: print "Returned False (wanted behaviour)\n";
print "defined -f:";
defined -f '""'
? print "Returned True (unwanted behaviour)\n"
: print "Returned False (wanted behaviour)\n";
print "-T || -B:";
(-T '""' || -B '""')
? print "Returned True (unwanted behaviour)\n"
: print "Returned False (wanted behaviour)\n";
print "defined -T:";
defined -T '""'
? print "Returned True (unwanted behaviour)\n"
: print "Returned False (wanted behaviour)\n";
print "defined -B:";
defined -B '""'
? print "Returned True (unwanted behaviour)\n"
: print "Returned False (wanted behaviour)\n";
And here's the output I got:
-f:
Returned False (wanted behaviour)
defined -f:
Returned True (unwanted behaviour)
-T || -B:
Returned False (wanted behaviour)
defined -T:
Returned False (wanted behaviour)
defined -B:
Returned False (wanted behaviour)
I don't mind filing a bug report about this, but I'll wait for comments about that first (in case I've overlooked something).
Cheers,
Rob
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.