Calling perl function in ruby

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?

2009/2/24 Loga G. [email protected]:

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

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

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

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

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.

2009/2/25 Loga G. [email protected]:

#! /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