# Calling perl function in ruby

**URL:** <https://www.ruby-forum.com/t/calling-perl-function-in-ruby/161204>\
**Category:** Ruby\
**Created:** [February 24, 2009, 9:55am UTC](https://www.ruby-forum.com/t/calling-perl-function-in-ruby/161204 "2009-02-24T09:55:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![loganathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ec9cab/32.png) [@loganathan](https://www.ruby-forum.com/u/loganathan)\
**Post date:** [February 24, 2009, 9:55am UTC](https://www.ruby-forum.com/t/calling-perl-function-in-ruby/161204/1 "2009-02-24T09:55:39Z")

</div>

Below code is used for calling c function in ruby.

#! /usr/bin/ruby

require ‘/usr/lib/ruby/1.8/inline.rb’

puts “Inline module…”  
class MyTest  
inline do |builder|  
builder.c "  
long factorial(int max) {  
int i=max, result=1;  
while (i \>= 2) { result \*= i–; }  
return result;  
}"  
end  
end  
t = MyTest.new()  
fact = t.factorial(5)  
puts fact

Question:

Likewise, instead of c I have to write a perl function and should call  
it?

Is there any way?

---

<div class="post-metadata">

**Author:** ![loganathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ec9cab/32.png) [@loganathan](https://www.ruby-forum.com/u/loganathan)\
**Post date:** [February 24, 2009, 10:25am UTC](https://www.ruby-forum.com/t/calling-perl-function-in-ruby/161204/2 "2009-02-24T10:25:18Z")

</div>

Loga G. wrote:

> Likewise, instead of c I have to write a perl function and should call  
> it?
> 
> Is there any way?

Only by embedding a whole Perl interpreter inside your Ruby interpreter.

Usually it’s better to make the Perl code a completely separate process,  
and use some form of IPC to talk to it. Simplest to set up are probably  
HTTP, or XMLRPC over HTTP.

It depends on your problem domain. Maybe starting a Perl process using  
IO.popen(…) and talking to it over (its) stdin/stdout will be easier.  
If the data is binary, then a simple binary protocol like (4 bytes  
length) followed by (N bytes of data) will be much faster than wrapping  
the request in XML and HTTP.
