0

The following code below is in a perl script that is executed on windows OS

What do the following means:

open(CMD, "$cmd 2>&1 |");
$oldh = select(CMD);
$| = 1;
select($oldh);

Can anyone please explain? Is there any point of writing this code? Because, first we are selecting "CMD" as a new filehandle, and then after 1 statement "$|=1" (literally doing nothing), we are again jumping back to the old filehandle ($oldh).

PPP
  • 329
  • 1
  • 9
  • I suspect you're not showing us the whole code. There's probably also some `print CMD ...` going on further down in the script? – simbabque Aug 03 '22 at 11:51
  • @simbabque, lines mentioned above are in order in the code. – PPP Aug 03 '22 at 12:10
  • 1
    It sets auto flush(no buffering) on cmd handle. – mpapec Aug 03 '22 at 12:48
  • 1
    CMD->autoflush ; should be an equivalent – mpapec Aug 03 '22 at 12:51
  • 1
    Setting `$|` (autoflush, see it [in perlvar](https://perldoc.perl.org/perlvar#$%7C)) works for the "default" filehandle, so with `select` they first make `CMD` default so that they can then set autoflush (for it), then they revert back to whatever the default was (normally `STDOUT`). The `CMD` need not be default in order to read from it for that pipe open (`while ()` etc). What @mpapec mentions, calling `autoflush` method on `CMD` is so much clearer and better, but this code may have been written a long time ago when that facility may have not existed yet? – zdim Aug 03 '22 at 20:07
  • (by "default" I mean whatever's been last select-ed) – zdim Aug 03 '22 at 20:39

0 Answers0