Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I'm running Strawberry Perl 5.30 on Windows 10, and am finding that the test -e 'C:\Users' resolves to a false value; in fact the -e operator operating on any valid, existing entity (file name or directory name) always returns a false value.

I recall that in the past I've found that the file test operator -r also does not work as expected under Strawberry, because when my program first uses -r to verify that a file is readable before attempting to read it, and on getting a true value then goes on to read it, can still find that the file is in fact unreadable.

Why is this? I would have thought that when Strawberry Perl was created as the primary Windows port of Perl that the code for the -e, -r, and other single-letter file tests would have been rewritten using Windows-friendly code to provide the same return values for Windows files that their UNIX counterparts do for UNIX files.

As to a workaround, is there any sort of switch that I can set anywhere in my Windows environment or perl program that will tell perl to cause the one-letter file test operators to behave analogously to how they do in UNIX?

If not, I've been examining the Test::File CPAN module. Its methods do work the way that I need them to, e.g. the file_exists_ok() and file_readable_ok() methods are effective replacements for the -e and -r file test functions. However, the use of the Test::File module comes at a price --

(1)the total number of tests that are to be performed during the entirety of the program run needs to be specified in advance (via the specification of the File::More module that must come first) notwithstanding that the number of tests may not in fact be knowable in advance, such as in a directory tree traversing and searching program;

(2) the Test::File module writes the results of its testing to stdout or stderr (depending on the outcomes of the tests) and I have not found anything in the Test::File documentation to get it to just silently perform its tests and do nothing more than simply provide true or false return values;

(3) I store the majority of my perl programs on a network drive where they can be invoked from either a Windows or a Linux environment. In my programs' initialization routines, they need to examine the value of $^O and behave accordingly. When the value of $^O is "MSWin32", then they need to say "use Test::Main tests => <whatever>" and "use Test::File". Otherwise (when the value of $^O is, say, "linux"), then the preceding two use instructions are never executed, but being in the program code they are nonetheless parsed. I have the Test::More and Test::File modules installed in my Windows CPAN directory from which they are loaded and executed, but because any program that includes these two use instructions is likely also to be invoked from a Linux environment these two modules would need to be included in my Linux CPAN directory as well, never to be executed but only to keep a program from failing during the initial parsing step because they appear as targets of use instructions (which are never executed but nonetheless parsed). Having them in the Linux CPAN directory only for syntactic considerations seems kludgy and problematic.

I can squelch the unwanted verbose output by relegating the use of TEST::FILE to eval blocks, and I can create a function that returns a true or false value to indicate whether or not a specified file or directory exists by listing the members of its parent directory and checking to see if any of the member names matches the specified name and is of the same type (file vs. directory), but this all seems like a lot of effort to have to go to just to check within the Strawberry Perl environment for Windows whether or not a given file or directory exists and/or is readable.

Given the ubiquity of Windows, I'm sure that this file test operator issue that I'm describing has been encountered countless times before. What have others done that have turned out to be good alternatives?


In reply to Strawberry Perl on Windows 10 file test operators do not work as expected by fireblood

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2024-04-25 18:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found