bjhobbs has asked for the wisdom of the Perl Monks concerning the following question:

I am using ActivePerl for Windows and I am having a problem reading a file from a server (different machine than the script is running on).

Most of the time, the script runs fine, and the files are read without problems. Sometimes, though, my perl script is unable to find the files and I get the following error:"No such file or directory"

The files do exist and I have no trouble accessing them with some C++ code that I have written. I've tried reading the perldoc on the open method but I didn't find anything helpful.

Here's some Perl code that emulates the error:

use strict; open(FILENAMES, "<filenames.txt") or (die "filenames.txt could not be opened: $!\n"); my ($sec, $min, $hour, $day, $month, $year) = localtime(time()); $year += 1900; $month++; while (my $fileInfoString = <FILENAMES>) { chomp $fileInfoString; my @fileInfo = split(/\t/, $fileInfoString); my $filePath = sprintf "%s\\%d\\%02.d\\", $fileInfo[0], $year, $month; unless(open SOURCE_FILE, "<$filePath$fileInfo[1]") { print STDERR "$filePath$fileInfo[1] not opened: $!\n"; next; } print STDERR "$filePath$fileInfo[1] opened.\n"; my $count; while (my $line = <SOURCE_FILE>) { $count++; } print "$count lines.\n"; close SOURCE_FILE; }

filenames.txt contains a list of filepaths (something like \\10.1.101.196\folder\) and filenames (like 023511X.dat).

Can anybody tell me if there is there a problem with the code I have written or could this be an issue with my network speed (I have noticed that sometimes it takes a long time to access the server using other methods)? Does Perl time out if it doesn't get a response fast enough? If so, is there a way to extend the amount of time before it times out?

Replies are listed 'Best First'.
Re: can't open file from another machine
by Corion (Patriarch) on Jun 29, 2005 at 18:21 UTC

    In principle what you are doing should work, despite what the others are telling you. I would think that the "file not found" errors are the result of either the share not being available or the machine not being reachable, but I'm not aware of any setting to influence the timeout...

Re: can't open file from another machine
by brian_d_foy (Abbot) on Jun 29, 2005 at 17:47 UTC

    The open command lets you access local files (or files made to look local by network mounts and so on). It doesn't connect to other machines for you. You'll have to fetch the file yourself, or set up some system so you can access over the network (then not use open()).

    --
    brian d foy <brian@stonehenge.com>
      Not so. In a Windows environment, \\ip_address\share\dir\filename is a perfectly valid argument for open. (Tested.) It isn't a Perl feature, it's an OS feature. Unix has a similar feature: NFS.

        That's what I said: it's not open() and something else has to let you open the file like that. If the local or remote machines aren't set up for that, Perl isn't going to do it for you.

        --
        brian d foy <brian@stonehenge.com>
Re: can't open file from another machine
by bofh_of_oz (Hermit) on Jun 29, 2005 at 18:04 UTC
    AFAIK, open() only opens local files. To open network files, you can chdir \\10.1.101.196\folder (or rather, chdir "//10.1.101.196/folder") and then open the file...

    --------------------------------
    An idea is not responsible for the people who believe in it...