Thanks for pointing out those flaws, sgifford. I did modify my
method, so it didn't need to use the delay with select. It waits
in a loop until output is available. This will give multiline output
from READ, but now I'm stumped by another problem. It seems the output buffer which bc writes to with IPC::Open3 is limited to 4060 bytes. I've tried alot of things, but no matter what I do, bc
stops output at 4060 bytes, but will give full output if run by itself from the command line. Try 123^12345 ; which if run from the commandline will output about 16k, but when run from IPC::Open3 will clip at 4060 bytes.
#!/usr/bin/perl
use warnings;
use strict;
use IPC::Open3;
require 'sys/ioctl.ph';
#interface to "bc" calculator
my $pid = open3(\*WRITE, \*READ,\*ERROR,"bc");
#if \*ERROR is false, STDERR is sent to STDOUT
while(1){
my($error,$answer,$rsize,$esize)=('','',0,0);
print "Enter expression for bc, i.e. 2 + 2\n";
chomp(my $query = <STDIN>);
#send query to bc
print WRITE "$query\n";
#timing delay needed tp let bc output
#select(undef,undef,undef,.01);
#this while loop waits and eliminates need for above delay
#but is limited to 4060 bytes from bc
do {
#see which filehandles have output from perldoc -q filehandle
$esize = pack("L", 0);
ioctl(\*ERROR, FIONREAD(), $esize) or die "Couldn't call ioctl:
+$!\n";
$esize = unpack("L", $esize);
print "esize-> $esize\n" unless ($esize < 1);
$rsize = pack("L", 0);
ioctl(\*READ, FIONREAD(), $rsize) or die "Couldn't call ioctl: $
+!\n";
$rsize = unpack("L", $rsize);
print "rsize-> $rsize\n" unless ($rsize <1);
#get the output from bc
if($esize > 0){sysread(ERROR,$error,$esize); print "\e[1;31m ERRO
+R-> $error \e[0m \n"}
if($rsize > 0){sysread(READ,$answer,$rsize); print "Output->$quer
+y = $answer\n"}
} until(($esize > 0)or($rsize > 0));
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.