Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I've tried to use "-|" pipe option on "perl v5.8.4 built for MSWin32-x86-multi-thread" as below :

use strict; use warnings; use threads; #sub sub1{- my $pid = open(HCIDUMP, "-|"); defined($pid) || die "can't fork: $!"; if($pid){ while(<HCIDUMP>){ my $test = readline(<HCIDUMP>); print "this is priting from open - $test\n"; } close(HCIDUMP); }else{ exec("sdb shell hcidump") || die "can't exec program: $!"; }
Then, perl returns
"'-' is not recognized as an internal or external command,operable pro +gram or batch file."
Is this usage wrong or just not working in window?

Thanks Br

David Lim

Replies are listed 'Best First'.
Re: "- |" pipe option doesn't work
by Corion (Patriarch) on Jan 09, 2017 at 15:22 UTC

    A self-pipe is not supported under Windows Perl. See perlport:

    open to |- and -| are unsupported. (Win32, RISC OS)

      Hm. Is that doc quote 5.8.4 specific? Because it works okay in 5.8.9.

      This code:

      #! perl -slw use strict; open PIPE, '-|', q[ c:\\perl32\\bin\\perl.exe -v ] or die $^E; print "<<<$_>>>" while <PIPE>;

      Produces this output:

      C:\test>\perl32\bin\perl junk99.pl <<< >>> <<<This is perl, v5.8.9 built for MSWin32-x86-multi-thread >>> <<<(with 12 registered patches, see perl -V for more detail) >>> <<< >>> <<<Copyright 1987-2008, Larry Wall >>> <<< >>> <<<Binary build 826 [290470] provided by ActiveState http://www.Active +State.com >>> <<<Built May 24 2009 09:21:05 >>> <<< >>> <<<Perl may be copied only under the terms of either the Artistic Lice +nse or the >>> <<<GNU General Public License, which may be found in the Perl 5 source + kit. >>> <<< >>> <<<Complete documentation for Perl, including FAQ lists, should be fou +nd on >>> <<<this system using "man perl" or "perldoc perl". If you have access + to the >>> <<<Internet, point your browser at http://www.perl.org/, the Perl Home + Page. >>> <<< >>>

      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
      In the absence of evidence, opinion is indistinguishable from prejudice.

        I think the quote specifically refers to the naked pipe-self-open:

        open my $fh, '-|' or die;

        which basically launches a forked copy of the current process. I don't think this has ever been implemented for Windows Perl.

        I don't have a Win32 machine available now, but perlport in blead contains a slightly reformulated version of the same:
        open (Win32, RISC OS) Open modes "|-" and "-|" are unsupported.

        ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
      Thanks for confirmation

        I don't see in your code why you would need a self-pipe+fork.

        For reading from an asynchronous process, I've used a plain pipe-open quite successfully:

        my $cmd = 'sdb shell hcidump |'; my $child = open( my $hci, $cmd ) or die "Couldn't spawn [$cmd]: $!"; while (<$hci>) { ... }; END { if( $child ) { kill 'KILL' => $child; }; $child = 0; };

        Update: There was the pipe missing from the command line, whoops!

Re: "- |" pipe option doesn't work (updated)
by haukex (Archbishop) on Jan 09, 2017 at 15:30 UTC

    Hi,

    It looks like you want to run the external command "sdb shell hcidump". There are several modules to help you, like IPC::Run3, or perhaps IPC::Run, which has the ability to communicate with the subprocess interactively. Update: However, the latter has somewhat spotty test results on Windows, whereas the former looks pretty good on all OSes. A potential disadvantage of IPC::Run3 is that it can't communicate with the subprocess interactively, and in many cases it uses temp files for the redirections (although I personally don't really see that as a problem).

    Hope this helps,
    -- Hauke D