in reply to undefined question

If there's no output from the pipe open, there'll be nothing read from PROCS, so your loop won't execute. You could set a flag:

my $output; while (<PROCS>) { $output++; # do something else } print "Couldn't find $process\n" unless $output;

The a Proc::ProcessTable module might be another approach.

Update: I forgot to mention that your loop will not execute if a line evaluating to false in boolean context is read — that may not be a problem in this case, but it's worth remembering.

Replies are listed 'Best First'.
Re: Re: undefined question
by edan (Curate) on Jun 09, 2003 at 07:44 UTC
    I forgot to mention that your loop will not execute if a line evaluating to false in boolean context is read

    Actually, no - if you read the perlop 'I/O Operators' entry carefully, you'll find that using <FILEHANDLE> inside a while (or for(;;)) construct is treated specially - a defined test is done for you, to avoid exactly the problem you mention...

    I case you're too lazy to look it up, I quote:

    In these loop constructs, the assigned value (whether assignment is automatic or explicit) is then tested to see whether it is defined. The defined test avoids problems where line has a string value that would be treated as false by Perl, for example a "" or a "0" with no trailing newline. If you really mean for such values to terminate the loop, they should be tested for explicitly:

    while (($_ = <STDIN>) ne '0') { ... } while (<STDIN>) { last unless $_; ... }

    In other boolean contexts, "<filehandle>" without an explicit "defined" test or comparison elicit a warning if the "use warnings" pragma or the -w command-line switch (the "$^W" variable) is in effect.

    HTH

    --
    3dan

      You're right; I just confirmed this with B::Deparse. Thanks!

      $ perl -MO=Deparse while (my $in = <STDIN>) { print $in; } ^D while (defined(my $in = <STDIN>)) { print $in; } - syntax OK
Re: Re: undefined question
by Anonymous Monk on Jun 09, 2003 at 04:46 UTC
    Very nice. Quick and easy...and I woudln't have thought of it. Thanks!