Calling perl function in ruby

L

Loga Ganesan

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?
 
R

Robert Klemme

2009/2/24 Loga Ganesan said:
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
 
L

Loga Ganesan

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
 
M

Maran Chandrasekar

Loga said:
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
 
E

Eric Hodel

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?

There's an Inline::perl in ZenHacks, but it's never been released.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,078
Latest member
MakersCBDBlood

Latest Threads

Top