develooper Front page | perl.beginners | Postings from January 2002

RE: why does open()ing happen only at the <FD> line ?

Thread Previous | Thread Next
From:
Bob Showalter
Date:
January 10, 2002 06:03
Subject:
RE: why does open()ing happen only at the <FD> line ?
Message ID:
2E4528861499D41199D200A0C9B15BC031B6FB@FRISTX
> -----Original Message-----
> From: Prahlad Vaidyanathan [mailto:slime@vsnl.net]
> Sent: Thursday, January 10, 2002 12:14 AM
> To: beginners@perl.org
> Cc: Michael Fowler
> Subject: Re: why does open()ing happen only at the <FD> line ?
> 
> 
> Hi,
> 
> On Wed, 09 Jan 2002 Michael Fowler spewed into the ether:
> [-- snip --]
> > How did you go about determining this?  As far as I know, 
> this is not the
> 
> I'll explain. This is something I just ran. 
> 
> [ My /var/log/messages is a test file containing 1 line only. ]
> 
> <code>
> open(FD,"sudo less /var/log/messages |") ;
> print "test\n" ;
> my $line = <FD> ;
> print "$line\n" ;
> print "test #2\n" ;
> </code>
> 
> $ ./test.pl
> Password:test
>     [waits for password here]
> Jan  6 06:51:13 marvin syslogd 1.4-0: restart.
> 
> test #2
> </output>
> 
> As you can see, it runs 'sudo' _after_ printing "test\n". ie. when it
> reads from the file.

I think you're getting confused about the way pipe open works.

The fact that the "Password:" prompt (which is printed by sudo)
appears before "test" shows you that sudo *is* being run when you
call open() (although the output could have come in either order,
in this case it demonstrates that sudo is running).

The key is that a pipe open(), unlike a call to system() or backticks,
does not wait for the called program to exit.

Here's the sequence of events:

     test.pl                          sudo
     -------                          ----
1.   calls open()  --- forks -->      starts
2.   prints "test"                    prints "Password" to tty
3.   blocks waiting for               calls getpass(), blocks
     sudo to write to FH              waiting for password
4.                                    exec's less
5.                                    less writes to stdout
6.   receives data from
     less on FH

Now the question is, how do you want or expect it to be acting
differently?

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About