in reply to Re: How do I open a r/w named pipe as a file? *Should* be simple...
in thread How do I open a r/w named pipe as a file? *Should* be simple...

Ah, a Win32 named pipe (in Unix it is trivial). Fetch Win32API::File (if it didn't come with your copy of Perl) and you can do this:

use Win32API::File qw( createFile OsFHandleOpen ); my $h= createFile( "//./pipe/MyPipe", "r" ) or die "Can't read from pipe/MyPipe: $^E\n"; $^E= 0; # Clear possibly misleading errors out. OsFHandleOpen( FILE, $h, "r" ) or die "Can't associate Perl handle: $! ($^E)\n";
and then just use FILE like you would any old Perl file handle. (See the module documentation for more details like how to open the pipe for write access or for read/write access, etc.)

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

Replies are listed 'Best First'.
Tye, that does the job.
by Coleoid (Sexton) on Mar 29, 2001 at 03:04 UTC
    Grand, that works beautifully. I would not have opened Win32API::File on my own. Just to wrap it up, it looks like this, once fit to my program:

    my $hPipe = createFile( $PIPE_NAME, "rwe" ) or die "Can't read from [$PIPE_NAME]: $^E\n"; $^E = 0; # Clear possibly misleading errors out. no strict 'subs'; OsFHandleOpen( PIPE, $hPipe, "rw" ) or die "Can't associate Perl handle: $! ($^E)\n"; use strict 'subs';
    The 'e' portion of the "rwe" arg to createFile() causes an error if the file doesn't already exist, which is the behavior I want. If the pipe server isn't running, I need to know.

    I expect there's a better way to quiet the complaint about the filehandle being a bareword, too.

    Since I'm not reporting $^E unless there's an error, is it necessary to clear it ahead of time?

    Thanks for the prompt answer to something that turned out trickier than I thought.

      Sorry, I forgot about that. OsFHandleOpen( \*PIPE, ... ) is what I would use.

              - tye (but my friends call me "Tye")
        Right, right, I remember that now. Haven't been slinging around filehandles before now.

        Sad to say, I spoke too soon. The piping isn't happy yet, the pipe server gets garbage characters at the end of the client's message, using this method. The client gets nothing but garbage in return. Think I'm going to take it home and mull it over.