I have never before had occasion to do the following kind of problem, so
I haven’t a clue where to start. I have a C filter that accepts a
string on stdin and returns a boolean based on it’s analysis of the
string. In a ruby program I want to pass a list of strings, one at a
time, to the filter and take appropriate action based on each return
value. Could someone point me in the right direction? I have never
worked with threads or pipes before.
A simple solution seems to be this: Write a shell script like
#! /bin/sh -f
echo “$*” | your_filter
or echo “$1” | your_filter
Then, from within ruby
result = system(“your_script”, “a string”)
result is boolean
Normally you would use IO.popen but I can’t see a way to access the exit
status of the process directly. Something like this comes to mind, but I
could not convice “popen” to behave like documented, because “-” is not
treated as documented
def f( word )
result = nil
IO.popen(“-”, “r+”) do |io|
if io
# parent process
io.puts word
result = io.readlines.shift.chomp
else
# child process
puts system(“more”)
end
end
I have never before had occasion to do the following kind of problem, so
I haven’t a clue where to start. I have a C filter that accepts a
string on stdin and returns a boolean based on it’s analysis of the
string. In a ruby program I want to pass a list of strings, one at a
time, to the filter and take appropriate action based on each return
value. Could someone point me in the right direction? I have never
worked with threads or pipes before.
f = IO.popen(“/usr/bin/false”,“w”)
f.write “ignoreme”
f.close
puts $? #>> 256
It would be nice if the status hung around as an instance variable of f
though. It would also be nice if you didn’t have to split it into
WEXITSTATUS and WTERMSIG by hand: e.g.
Normally you would use IO.popen but I can’t see a way to access
the exit status of the process directly.
$? won’t work?
It does appear to work as advertised:
Thanks for the hint!
f = IO.popen(“/usr/bin/false”,“w”)
f.write “ignoreme”
f.close
puts $? #>> 256
It would be nice if the status hung around as an instance variable of f
though.
Yes, I did not think of looking for a global variable. This is a nice
example of the drawbacks: If two threads use popen you can’t be sure any
more that the value you read from $? is the correct one.
Kind regards
robert
···
On Thu, Feb 27, 2003 at 06:57:18PM +0900, Michael Shigorin wrote:
On Tue, Feb 25, 2003 at 05:52:08PM +0900, Robert Klemme wrote:
On Thu, Feb 27, 2003 at 06:57:18PM +0900, Michael Shigorin wrote:
On Tue, Feb 25, 2003 at 05:52:08PM +0900, Robert Klemme wrote:
Normally you would use IO.popen but I can’t see a way to access
the exit status of the process directly.
$? won’t work?
It does appear to work as advertised:
Thanks for the hint!
f = IO.popen(“/usr/bin/false”,“w”)
f.write “ignoreme”
f.close
puts $? #>> 256
It would be nice if the status hung around as an instance variable of f
though.
Yes, I did not think of looking for a global variable. This is a nice
example of the drawbacks: If two threads use popen you can’t be sure any
more that the value you read from $? is the correct one.
Normally you would use IO.popen but I can’t see a way to access
the exit status of the process directly.
$? won’t work?
It does appear to work as advertised:
Thanks for the hint!
f = IO.popen(“/usr/bin/false”,“w”)
f.write “ignoreme”
f.close
puts $? #>> 256
It would be nice if the status hung around as an instance variable of
f
though.
Yes, I did not think of looking for a global variable. This is a nice
example of the drawbacks: If two threads use popen you can’t be sure
any
more that the value you read from $? is the correct one.
Pickaxe p. 218.
I couldn’t locate the pertinent table in the HTML version, believe it’s
not there…
···
On Thu, Feb 27, 2003 at 10:59:36PM +0900, Robert Klemme wrote:
Yes, I did not think of looking for a global variable. This is a nice
example of the drawbacks: If two threads use popen you can’t be sure
any
more that the value you read from $? is the correct one.