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

I want to read a file on another NT4 server in my LAN. On my NT4(Server1) w. Xitami Intranetserver and Perl 5.6.0 i need to read simple logfiles from Server2. I've tried: -----
Open (FH,'<//Server2/f$/logconn.txt'); @result = <FH>; #line 5 close (FH);
----- With the following cgi error: readline() on closed filehandle main::FH at E:\Program\Xitami\webpages\cgi-bin\readlog.pl line 5. Can someone help out?

Replies are listed 'Best First'.
(tye)Re: Perl - Read a file on another server
by tye (Sage) on May 04, 2001 at 08:27 UTC

    If your CGI script is being run from IIS, then it is probably being run in a security context that doesn't give it access to any NT-level network resources (such as shared drives) and is almost certainly not going to have administrative access to a remote machine.

    Put at least or die "$!" after your open (and, no "Open" isn't going to work so I guess that isn't really the code that is failing). You could even throw $^E into the error message as sometimes that adds just a hint of more information on Win32.

    You are going to have to rethink your design.

            - tye (but my friends call me "Tye")
Re: Perl - Read a file on another server
by TGI (Parson) on May 04, 2001 at 03:44 UTC

    I've been able to use UNC names with the open command. I just say:

    open (LOG, "<//Server/path/file.log") or die "Oh my: $!";

    Granted I'm using a weird custom internally hacked perl, and more likely to matter, I'm just accessing the C drive through a defined share point.

    It looks like you are connecting to the admin share of the F drive. I think that might be the cause of your problem. Try creating a normal share point and using it instead.


    TGI says moo

Re: Perl - Read a file on another server
by eejack (Hermit) on May 04, 2001 at 07:33 UTC
    I would check the existance of the file. TGI might be right about the share, or the file just might not be there.

    $file = '//Server2/f$/logconn.txt'; print "it exists\n" if -e $file; open (FH,'<$file') || die "a horrible death"; @result = <FH>; close (FH);

    I get that same error when the file doesn't exist (using -w).

    EEjack

Re: Perl - Read a file on another server
by jink (Scribe) on May 04, 2001 at 17:43 UTC
    I think there may be a problem with your filename. Aren't the slashes supposed to be backslashes?
    open(FH, "<\\\\Server2\\f\$\\logconn.txt") || die("Can't open file! ($!)\n");
    should do the trick. Didn't try it here, though.

    All Camels are equal, but some Camels are more equal than others.
      Howdy jink,

      The filename was quoted with single quote marks...
      $file = '//Server2/f$/logconn.txt';
      which would be fine. However if he were using double quote marks like then you would be correct. Also interesting to not that the direction of the slashes make no real difference on win32 perl.

      $file = '//Server2/f$/logconn.txt'; and $file = '\\Server2\f$l\logconn.txt';
      are equivalent.

      EEjack

        $file = '//Server2/f$/logconn.txt';
        and
        $file = '\\Server2\f$\logconn.txt';

        are equivalent.

        (I removed the "l" typo)
        Try this:

        print '//Server2/f$/logconn.txt', $/; print '\\Server2\f$\logconn.txt', $/;
        and you'll see
        //Server2/f$/logconn.txt \Server2\f$\logconn.txt
        Use that second one as a file path and Server2 won't even be bothered.

        This is why I always double \ in Perl quoted strings (except for "here documents" which is the one form of quoting in Perl that doesn't treat \ as special) and so would write the second one as: '\\\\Server2\\f$\\logconn.txt' even when using single quotes. Otherwise you get in the habit of thinking that \ isn't special in single quotes and make the mistake you just did (which can be real hard to debug when you do it).

        P.S. Note that / doesn't work as a directory separator when you pass file names as command-line arguments to many Win32 commands. I believe that this is the only exception (well, other than some GUI tools don't allow them either, but most people rarely use Perl to stuff strings into a GUI [though I've done that]).

                - tye (but my friends call me "Tye")