# Calling perl function in ruby

**URL:** <https://www.ruby-forum.com/t/calling-perl-function-in-ruby/161183>\
**Category:** Ruby\
**Created:** [February 24, 2009, 7:22am UTC](https://www.ruby-forum.com/t/calling-perl-function-in-ruby/161183 "2009-02-24T07:22:13Z")\
**Posts on this page:** 6\
**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, 7:22am UTC](https://www.ruby-forum.com/t/calling-perl-function-in-ruby/161183/1 "2009-02-24T07:22:13Z")

</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 an 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, 3:34pm UTC](https://www.ruby-forum.com/t/calling-perl-function-in-ruby/161183/2 "2009-02-24T15:34:37Z")

</div>

2009/2/24 Loga G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Below code is used for calling c function in ruby.

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

Additionally to what Brian recommended: did you check whether there is  
another option? Maybe there is a Ruby implementation of the Perl code  
you need. IMHO generally the benefits of embedding one scripting  
language in another are pretty low so I’d rather either use a Ruby  
implementation of the code you need or do as Brian suggested and keep  
the Perl code in a separate process which you then call via fork,  
popen or whatever means is appropriate.

Kind regards

robert

---

<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 25, 2009, 1:54pm UTC](https://www.ruby-forum.com/t/calling-perl-function-in-ruby/161183/3 "2009-02-25T13:54:30Z")

</div>

Ya, Here is the code to acheive it. But I don’t know that whether it is  
an efficient coding.

#! /usr/bin/ruby

# 

require ‘perl’

perl\_obj = Perl.new()

perl\_obj.eval("  
#!/usr/bin/perl  
use strict;  
use warnings;

```
    sub factorial {
            my $n = 1;
            $n *= $_ for 2..shift;
            return $n;
    }")

```

ret = perl\_obj.call(“factorial”, 5)

puts ret

---

<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 25, 2009, 2:08pm UTC](https://www.ruby-forum.com/t/calling-perl-function-in-ruby/161183/4 "2009-02-25T14:08:58Z")

</div>

Loga G. wrote:

> Ya, Here is the code to acheive it. But I don’t know that whether it is  
> an efficient coding.
> 
> #! /usr/bin/ruby
> 
> # 
> 
> require ‘perl’  
> downloaded from [http://www.yoshidam.net/perl-0.2.9.tar.gz](http://www.yoshidam.net/perl-0.2.9.tar.gz)
> 
> perl\_obj = Perl.new()
> 
> perl\_obj.eval("  
> #!/usr/bin/perl  
> use strict;  
> use warnings;
> 
> ```
> sub factorial {
> my $n = 1;
> $n *= $_ for 2..shift;
> return $n;
> }")
> 
> ```
> 
> ret = perl\_obj.call(“factorial”, 5)
> 
> puts ret

Yes, It is working. Nicely done, Loganathan

---

<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 25, 2009, 6:05pm UTC](https://www.ruby-forum.com/t/calling-perl-function-in-ruby/161183/5 "2009-02-25T18:05:30Z")

</div>

On Feb 23, 2009, at 22:21, Loga G. wrote:

> ```
> long factorial(int max) {
> 
> ```
> 
> Question:
> 
> Likewise, instead of c I have to write a perl function and should call  
> it?
> 
> Is there an way?

There’s an Inline::Perl in ZenHacks, but it’s never been released.

---

<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 25, 2009, 5:58pm UTC](https://www.ruby-forum.com/t/calling-perl-function-in-ruby/161183/6 "2009-02-25T17:58:56Z")

</div>

2009/2/25 Loga G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> #! /usr/bin/ruby  
> #!/usr/bin/perl

you should use #!/usr/bin/env ruby  
#!/usr/bin/env perl  
instead.  
This is more portable and takes the ruby command from the PATH of the  
user and not from a fixed path.

-Thomas
