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

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

Thread Previous | Thread Next
Michael Fowler
January 9, 2002 11:05
Re: why does open()ing happen only at the <FD> line ?
Message ID:
On Tue, Jan 08, 2002 at 08:06:14PM +0530, Prahlad Vaidyanathan wrote:
> <script>
> open(FD,'sudo grep -h "Connect time" /var/log/message* | grep -v COMMAND |')  or die $! ;
> # Do some stuff ...
> while ( <FD> ) {
> # do more stuff
> }
> </script>
> In the above script (an attempt to check my ppp usage), the script
> actually runs 'sudo' only when reading from the file (line #3) and not
> when open() is run (line #1).

How did you go about determining this?  As far as I know, this is not the
case; the command is run immediately upon open.  It can be proven with:

    perl -wle 'open(F, q{perl -wle "print STDERR q{test}" |") || die; sleep;'

If the command was only executed when F is read from you'd never see "test". 
However, when I run the above, I see "test".  The sleep is in there to prove
it's not being run due to the implicit close at the end of the script.

> ps. Is it inadvisable to open the said file like this (see the script
> above) ? If so, why ?

Opening it like this is fine, but I would use Perl to do the grepping, not
several invocations of grep.

Programmer, System Administrator

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About