open2 problems 
Author Message
 open2 problems

Greetings,

I have been trying to get open2 and open3 to do SOMETHING.  I have
searched the FAQs and dejanews and perldoc.  I am missing something
here and I hope somebody can point it out for me...

Here is a stripped down use of open2 which doesn't do anything.  The
co-process (cat) is created (I get a PID and I can see the process
using ps) and I get no warnings or errors, but I also get no output.  

Can anybody see what I am doing wrong?

Thanks.

######################################################################
#!perl -w
use strict;
use FileHandle;
use IPC::Open2;

my($WTR) = new FileHandle;
my($RDR) = new FileHandle;

my($pid) = open2($RDR,$WTR,"/usr/bin/cat -u -");
print "pid='$pid'\n";

# This should be enough to fill up any buffer...
foreach(0..5000) {
        print $WTR "012345678901234567890123456789012345678901234567890123456789\n";


Quote:
}

######################################################################

Once I get this working, I am gonna want to use select to see if there
is output pending...

-- Harold Bamford
--
-- Harold Bamford

   (630)713-1351



Fri, 22 Dec 2000 03:00:00 GMT  
 open2 problems

 [courtesy cc of this posting sent to cited author via email]

In comp.lang.perl.misc,

:I have been trying to get open2 and open3 to do SOMETHING.  I have
:searched the FAQs and dejanews and perldoc.  I am missing something
:here and I hope somebody can point it out for me...

:my($pid) = open2($RDR,$WTR,"/usr/bin/cat -u -");
:# This should be enough to fill up any buffer...
:foreach(0..5000) {
:       print $WTR "012345678901234567890123456789012345678901234567890123456789\n";


:}

Your bug is that:


*Exhausts* the filehandle.

But how is that going to read lines up until end of file?  Why
would it give you end of file?  You didn't give it one, you know!

Did you mean

    $line = <FH>;

by chance?

--tom
--
Common sense is the collection of prejudices acquired by age 18.
      --Einstein



Fri, 22 Dec 2000 03:00:00 GMT  
 open2 problems


Quote:

>Your bug is that:


>*Exhausts* the filehandle.
>Did you mean

>    $line = <FH>;

Thanks, Tom.  I appreciate your quick response.  However, that is
what I originally tried.  Trying to suck it all in was an act of
desperation.  And I just tried it again.  I get the pid printed
out and then nothing.  So, the new (but still useless) version of
the script is:

######################################################################
#!/opt/nwstools/bin/perl -w
use strict;
use FileHandle;
use IPC::Open2;

my($WTR) = new FileHandle;
my($RDR) = new FileHandle;

my($pid) = open2($RDR,$WTR,'/usr/bin/cat -u -');
print "pid='$pid'\n";

foreach(0..5000) {
        print $WTR "012345678901234567890123456789012345678901234567890123456789\n";
        my($lines) = <$RDR>;
        print "got back: $lines";

Quote:
}

######################################################################
--
-- Harold Bamford

   (630)713-1351


Sat, 23 Dec 2000 03:00:00 GMT  
 open2 problems
NEVER EVER fix a script after you paste it into a message <sigh>, hope
this supersede catches it...

Quote:

> Thanks, Tom.  I appreciate your quick response.  However, that is
> what I originally tried.  Trying to suck it all in was an act of
> desperation.  And I just tried it again.  I get the pid printed
> out and then nothing.  So, the new (but still useless) version of
> the script is:

<snip script>

Try the following, it works for me...

--- Start ---
use strict;
use FileHandle;
use IPC::Open2;
use IO::Select;

my $WTR = new FileHandle;
my $RDR = new FileHandle;

my $pid = open2($RDR,$WTR,'/usr/bin/cat -u -') || die "Horribly: $!";
print "pid='$pid'\n";

my $sel = new IO::Select;
$sel->add($RDR);
$sel->add($WTR);

foreach(0..5000) {
  print $WTR "012345678901234567890123456789012345678901234567890123456789\n";

  if ($sel->can_read($RDR)) {
    my $line = <$RDR>;
    print "got back: $line";
  }  

Quote:
}

--- End ---

But since you go 5001 rounds writing and do not know how many times
you have read you should have a loop reading until you receive EOF,
but now you won't be blocking on the read at least.

HTH

//B?rre

--

We expect them [Salvadoran officials] to work toward the elimination of
human rights.  -- Dan Quayle



Sat, 23 Dec 2000 03:00:00 GMT  
 open2 problems

Quote:

>NEVER EVER fix a script after you paste it into a message <sigh>, hope
>this supersede catches it...

Why?

In any case, this seems to work well.  Thanks.  However, I really don't
understand what the critical change was.  Clearly the script is not
{*filter*} on the read anymore (due to checking with select), but I would
THINK that this shouldn't be necessary -- 'cat -u' is supposed to
output immediately.  Obviously not.  But it does when I use it
interactively. Can anybody explain what is happening here?

Quote:
>  if ($sel->can_read($RDR)) {
>    my $line = <$RDR>;
>    print "got back: $line";
>  }  

Thanks again.

-- Harold

BTW, this was a contrived example which is why I wasn't counting input
or output lines, or checking on open2's success or any of the other
normal good programming stuff.  Perhaps a full working example of open2
and open3 should be put in the documentation.  Sure would have saved me!
--
-- Harold Bamford

   (630)713-1351



Sat, 23 Dec 2000 03:00:00 GMT  
 open2 problems
 [courtesy cc of this posting sent to cited author via email]

:Perhaps a full working example of open2
:and open3 should be put in the documentation.  Sure would have saved me!

You mean the one in perlipc, which I just tested and which works?

           use IPC::Open2;
           $pid = open2( \*Reader, \*Writer, "cat -u -n" );
           print Writer "stuff\n";
           $got = <Reader>;
           print "Got $got\n";

--tom
--
You have made an excellent hit on the UNIX.--More--



Sat, 23 Dec 2000 03:00:00 GMT  
 open2 problems


Quote:

>>Perhaps a full working example of open2
>>and open3 should be put in the documentation.
>You mean the one in perlipc, which I just tested and which works?
>    use IPC::Open2;
>    $pid = open2( \*Reader, \*Writer, "cat -u -n" );
>    print Writer "stuff\n";
>    $got = <Reader>;
>    print "Got $got\n";

Works for you, but not me.  It just hangs there.  It may be a bug in
perl5 or (more likely) a mis-compilation of the binary.

And to answer the inevitable:

Output of perl -v:
        This is perl, version 5.004_03

        Copyright 1987-1997, Larry Wall

        Perl may be copied only under the terms of either the Artistic License or the
        GNU General Public License, which may be found in the Perl 5.0 source kit.

Output of perl -V:      
        Summary of my perl5 (5.0 patchlevel 4 subversion 3) configuration:
          Platform:
            osname=solaris, osvers=2.5.1, archname=sun4-solaris
            uname='sunos nwsgpb 5.5.1 generic_103640-08 sun4u sparc sunw,ultra-2 '
            hint=recommended, useposix=true, d_sigaction=define
            bincompat3=y useperlio=undef d_sfio=undef
          Compiler:
            cc='cc', optimize='-O', gccversion=
            cppflags='-I/opt/gnu/include'
            ccflags ='-I/opt/gnu/include'
            stdchar='unsigned char', d_stdstdio=define, usevfork=false
            voidflags=15, castflags=0, d_casti32=define, d_castneg=define
            intsize=4, alignbytes=8, usemymalloc=y, randbits=15
          Linker and Libraries:
            ld='cc', ldflags =' -L/opt/local/lib -L/opt/gnu/lib'
            libpth=/opt/local/lib /opt/gnu/lib /lib /usr/lib /usr/ccs/lib
            libs=-lsocket -lnsl -ldl -lm -lc -lcrypt
            libc=/lib/libc.so, so=so
            useshrplib=false, libperl=libperl.a
          Dynamic Linking:
            dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags=' '
            cccdlflags='-Kpic', lddlflags='-G -L/opt/local/lib -L/opt/gnu/lib'

        Characteristics of this binary (from libperl):
          Built under solaris
          Compiled at Aug 28 1997 23:02:33

            /opt/nwstools/lib/perl5/sun4-solaris/5.00403
            /opt/nwstools/lib/perl5
            /opt/nwstools/lib/perl5/site_perl/sun4-solaris
            /opt/nwstools/lib/perl5/site_perl
            /opt/nwstools/lib/perl5/sun4-solaris
            .
--
-- Harold Bamford

   (630)713-1351



Sat, 23 Dec 2000 03:00:00 GMT  
 open2 problems

Quote:



> >NEVER EVER fix a script after you paste it into a message <sigh>, hope
> >this supersede catches it...

> Why?

Because the first one didn't even compile.

Quote:

> In any case, this seems to work well.  Thanks.  However, I really don't
> understand what the critical change was.  Clearly the script is not
>{*filter*} on the read anymore (due to checking with select), but I would
> THINK that this shouldn't be necessary -- 'cat -u' is supposed to
> output immediately.  Obviously not.  But it does when I use it
> interactively. Can anybody explain what is happening here?

It seems to be something with the FileHandle module. As Tom C. shows
in another post it works with glob's. I was thinking that open2's
autoflush on the writer might be messed up but neither
"$WTR->autoflush 1" nor 'select $WTR; $| = 1;' fixed the problem.
I'm stumped, anybody with an explanation?

        //B?rre

--

Who would have predicted...  that Dubcek, who brought the tanks in in
Czechoslovakia in 1968 is now being proclaimed a hero in Czechoslovakia.
Unbelievable.  -- Dan Quayle



Sun, 24 Dec 2000 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. IPC::Open2 problem, process doesn't see input

2. CGI and open2 problem

3. IPC::Open2 problem, process doesn't see input

4. Different IPC::open2 problems

5. Problem with STDOUT and open2()

6. Strange problem with dbm and open2 as root

7. Problem with STDOUT and open2()

8. Problems with open2.pl

9. Problem with FAQ's open2.pl?

10. open2 zombie problem

11. problems with open2 use in cgi

12. Problem with open2 and sysread/syswrite

 

 
Powered by phpBB® Forum Software