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

i have a three scripts .the first one to open a namespipe in windows and wait for some one to connect to it. The second script is to run a command in the same system which will write to the named pipe created in first script.The third script is to retrive the information written on the pipe.all these three should be done from same sytem. the following are the codes ..how do i join them after starting the first script how do i wait for the second to connect from the same system..how do i initaite second script once first script is waiting to connect....

script one use strict; use Win32::Pipe; sub main() { my $pipename = "mynamedpipe"; print $pipename."\n"; my $pipe = new Win32::Pipe( $pipename); if (! $pipe) { die "Cannot create named pipe\n"; } print "pipe created\n"; $pipe->ResizeBuffer (10); print "waiting for the client to connect\n"; $pipe->Connect(); print "client connected\n"; script two status3 = system ("cmd.exe /c $loc_deploy"); print("DONE WITH INSTALLATION\n"); #loc deploy writes to named pipe script three use strict; use Win32::Pipe; my $data = ""; my $b = 0; while (($b = $pipe->Read())) { chomp $b; $data .= $b; print "$b"; } print "Message received: $data\n"; $pipe->Disconnect(); print "pipe disconnected\n"; $pipe->Close(); print "pipe closed\n"; }

Replies are listed 'Best First'.
Re: how to join these interdependent codes???
by ikegami (Patriarch) on Aug 24, 2009 at 06:30 UTC

    Parent:

    1. Create named pipe
    2. Spawn asynchronous child, passing name of the pipe as a parameter.
    3. Write to pipe.
    4. Close the pipe.
    5. Reap child.

    Child:

    1. Open the file provided as argument.
    2. Read from the file handle until eof.

    But it's very odd to use a *named* pipe if you're writing both programs. Anonymous pipes would suffice.

      hey after i create a named pipe ..i need to give connect().but i cant have the code to write to pipe just next to connect(). so it should be like creating named pipe and waiting for connecting.while waiting for connecting..i should invoke a method to write on pipe..

        hey after i create a named pipe ..i need to give connect().

        Do that after spawning the child.

        while waiting for connecting..i should invoke a method to write on pipe..

        No, write after you're connected.

Re: how to join these interdependent codes???
by Marshall (Canon) on Aug 24, 2009 at 13:18 UTC
    First, this ancient style of indenting is a BAD idea:
    sub subroutine { ...stuff in sub... } ...now continue # horrific style! OR, sub subroutine { ...stuff in sub... } ...now continue # horrific style! That's K&R style from the "bad old days" when an extra line meant an extra punched card. Now we don't need to worry about #num punched cards. We can be "extravagant" by putting { on a line by itself! VERSUS: sub subroutine #FAR BETTER .... { ...stuff in sub... } ..now continue with other stuff # MUCH better!
    Second,
    In Perl, there is no main() or sub main();

    Third,
    if you get (1) and (2) then we talk about (3). Ikegami is "on it" about (3)..

      I think perlstyle would disagree with you on your style choice. And while Unix was initially developed on paper tape, neither Unix nor C were optimized for or developed on punch cards.

      The style you describe as K&R is actually Whitesmiths, but the OP does appear to be using 1TBS (One true brace style), or BSD KNF (Kernel Normal Form) used in BSD Kernel (and most BSD userland) code, K&R is similar but function declarations are more like how you advocate. The indent style you prefer is more like Allman or GNU Style. See Indent_Style for all the details.
      I use BSD KNF form even in Perl.

        And while Unix was initially developed on paper tape, neither Unix nor C were optimized for or developed on punch cards.

        Yeah, it was even worse than punched cards! I didn't expect many folks to remember paper tape days. But yes, these more compact brace styles derive from those ancient days. And in ancient days \t was very common although its rare nowadays (thank goodness). Hard disc space was so expensive that \t got used a lot instead of multiple simple space characters. Now a blank, ' ' is so "cheap" that we should use them without regard to cost (number of bytes).

        Yes, I do advocate lining up curly braces for major functions. I use McCracken style for logic expressions in any language. I don't know a name for the style I use for Perl "stacks" of list orientated functions.

        I have become a disciple of the philosophy that says that the single most important comment that you can put into code is whitespace, be it a blank line or spaces to line things up nicely. Whitespace can separate "thought units" and and also "unite related thoughts into one unit".

        There are a number of ways to indent and space out code. But I think that we would agree that it is important to be consistent in what you do. I suspect that we disagree on what that consistent thing should be. But that that it should be consistent and that "\n" is THE most important comment character is something that I hope we agree upon.