The task is as follows: We have the variable "$ image" - with binary data / image /. In the old version, save the data to a file:

open A, '>img.png'; binmode(A); print A $image; close A; 

Next, execute the system command with saving the output to the $ res variable

 my $res = `tesseract img.png stdout --psm 8 myconf`; 

Actually the question itself:
How to pass arguments without saving the $ image variable to a file, since tesseract provides for simultaneous input and output by a similar command:

 tesseract stdin stdout --psm 8 myconf 

    1 answer 1

     use IPC::Open2; my $pid=open2(my $read, my $write, "tesseract stdin stdout --psm 8 myconf"); # Запускаем команду, при этом получаем два файловых дескриптора # $write - stdin программы, $read - stdout программы # $pid - pid запущенного процесса в ОС print $write $image; # Пишем изображение на вход программы close($write); # Закрываем дескриптор вывода, для сообщения программе об окончании ввода local $/; # Будем читать сразу весь вывод (а не по одной строчке) my $res = <$read>; # Читаем waitpid($pid, 0); # Получаем код возврата (без этого в системе заведутся зомби) 
    • I thank, but something empty window hangs, displays nothing - usr13
    • @ usr13 See what actually hangs on. did the tesseract start, didn’t you forget to close $ write. Do not confuse read / write. I checked this example on cat and wc, it worked correctly. besides, in a very similar scheme, I run a tesseract in an antispam, although there it has other parameters, instead of stdin and stdout, the signs "-" are indicated. So also check that everything is correct with its parameters and whether it is waiting for something - Mike
    • @Mike maybe he just needs to show the result? say $res; - nörbörnën
    • @ nörbörnën I think there is something else there, he would not hang, but ended. And I think the conclusion that the vehicle will make to fit your needs - Mike
    • in the tesseract processes it hangs, if instead of stdin you specify a file, then it runs, naturally $ res is printed, "type 1.png | tesseract stdin stdout --psm 8 myconf" - also runs - usr13