[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Calling perl function in ruby

Loga Ganesan

2/24/2009 6:22:00 AM

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?
--
Posted via http://www.ruby-....

5 Answers

Robert Klemme

2/24/2009 2:33:00 PM

0

2009/2/24 Loga Ganesan <loganathan_gpt@yahoo.co.in>:
> 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

--
remember.guy do |as, often| as.you_can - without end

Loga Ganesan

2/25/2009 12:54:00 PM

0

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
--
Posted via http://www.ruby-....

Maran Chandrasekar

2/25/2009 1:08:00 PM

0

Loga Ganesan 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....
>
> 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

--
Posted via http://www.ruby-....

Thomas Preymesser

2/25/2009 4:58:00 PM

0

2009/2/25 Loga Ganesan <loganathan_gpt@yahoo.co.in>:
> #! /usr/bin/ruby
> =A0 =A0 =A0 =A0#!/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

--=20
Thomas Preymesser
thopre@gmail.com
http://thopre.google...
http://thopre.word...
Sent from: Berlin Berlin Deutschland.

Eric Hodel

2/25/2009 5:04:00 PM

0

On Feb 23, 2009, at 22:21, Loga Ganesan wrote:

> 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.