简体   繁体   中英

Path from ActivePerl in Windows 7 does not work

I have installed ActivePerl on a Windows 7 machine. Everything is okay, but it cannot read a configuration file from the given path. The code I used is

use constant CONFIG_FNAME => '/windows/system32/softwareconfig.txt';
$self->{"config"} = &read_config_file($CONFIG_FNAME);
warn "read_config_file(CONFIG_FNAME) returned undef" if !defined($self->{"config"});

It always returns the warning. It works if I use the softwareconfig.txt in the same directory and CONFIG_FNAME => 'softwareconfig.txt' .

This code runs perfectly on Windows XP. I also tried following paths, but none seems to work. These all work perfectly in Windows XP.

CONFIG_FNAME => 'c:/windows/system32/softwareconfig.txt'
CONFIG_FNAME => 'c:\\windows\system32\softwareconfig.txt'
CONFIG_FNAME => 'c:\\windows\\system32\\softwareconfig.txt'
CONFIG_FNAME => "c:\\windows\\system32\\softwareconfig.txt"
CONFIG_FNAME => '\\windows\\system32\\softwareconfig.txt'

What can be done to correct this?

Your problem likely has nothing to do with Perl. Windows\\system32 is a protected system directory, and Vista and later are rather protective of that location.

I do not recognize the file name 'softwareconfig.txt' as being a standard Windows file. If you are putting your own configuration files in protected system directories, stop doing that.

Environment::SpecialFolder Enumeration gives you the list of special folders on Windows.

If the configuration file is for a specific user, then a subdirectory under ApplicationData is appropriate. If it is for all users, then a subdirectory under CommonApplicationData is appropriate.

Please, always use strict; and use warnings at the beginning of all your scripts. It says:

Variable "$CONFIG_FNAME" is not imported at test1.pl line 7. (Did you mean &CONFIG_FNAME instead?)

The two lines must be:

self->{"config"} = read_config_file(CONFIG_FNAME);
warn "read_config_file(".CONFIG_FNAME.") returned undef" if !defined($self->{"config"});

A constant is a bare word. A constant is not interpolated inside quotes.

Your Perl code seems okay. You can just use a single forward slash. Perl substitutes this automatically. So /windows/system32/softwareconfig.txt will work if the service or program that is trying to access the file has the permission to read from that path.

My recommendation: Try changing the permission of the configuration file to allow from everyone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM