in reply to Perl System('') question

And another alternative is to use open:

my $result; # first open or die open my $eject, '-|', 'eject -n' or die "Can't eject: $!\n"; # Then read in lineby line while (<$eject>) { next unless /^device is/; # until the result is found $result= $_; last; } # done close $eject;

Especially when filtering output, I prefer this method because you can loop over the output line by line and react as soon as you found your searched input.


s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
+.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e

Replies are listed 'Best First'.
Re^2: Perl System('') question
by pobocks (Chaplain) on Nov 29, 2008 at 10:26 UTC
    Hmmm... What does this do to a program that's still running when you "last" out of the loop? Like, say you're running a find, and want to kill the find as soon as it spots the thing it's looking for?
    for(split(" ","tsuJ rehtonA lreP rekcaH")){print reverse . " "}print "\b.\n";
      close $eject; will wait for the child to end (after closing pipes to the child?) If you wish to save it from doing extra work, you could ask it to end by sending a TERM signal to it before calling close. The return value of open '-|' is the child's PID.

        ++ to both of you! I didn't think about that yet.


        s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
        +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
      close $eject; will wait for the child to end (after closing pipes to the child?) If you wish to save it from doing extra work, you could ask it to end by sending a TERM signal to it before calling close. The return value of open '-|' is the child's PID.