Forum: Ruby Calling perl function in ruby

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
9b3d14efbedc70a652b441b2ec6b871b?d=identicon&s=25 Loga Ganesan (loganathan1)
on 2009-02-24 07:22
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?
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-02-24 15:34
(Received via mailing list)
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
9b3d14efbedc70a652b441b2ec6b871b?d=identicon&s=25 Loga Ganesan (loganathan1)
on 2009-02-25 13:54
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
A4e57ee39838ea9623272392ac4b5274?d=identicon&s=25 Maran Chandrasekar (manimaran)
on 2009-02-25 14:08
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.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
Cd6b438f1238ee36cf4daecbae1d3917?d=identicon&s=25 Thomas Preymesser (Guest)
on 2009-02-25 17:58
(Received via mailing list)
2009/2/25 Loga Ganesan <loganathan_gpt@yahoo.co.in>:
> #! /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
58479f76374a3ba3c69b9804163f39f4?d=identicon&s=25 Eric Hodel (Guest)
on 2009-02-25 18:05
(Received via mailing list)
On Feb 23, 2009, at 22:21, Loga Ganesan 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.
This topic is locked and can not be replied to.