Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you interact with Perl programs from Ruby?

Tags:

ruby

perl

It's my understanding that there's no "bridge" between Ruby and Perl to let you call into Perl functions directly from Ruby. It's also my understanding that to call a Perl program from Ruby, you simply put it in backticks(i.e. result = `./helloWorld.pl`). However, this doesn't allow interaction with the Perl program(i.e. you can't interact with prompts or provide input). My quesitons are as follows:

  1. Is there any way to provide input to Perl programs from Ruby(aside from arguments)?

  2. Am I wrong that there is no bridge between Ruby and Perl? Interacting with a program's stdin seems like the wrong way to go when navigating prompts, and the programs I'm dealing with are well-designed and have libraries with the appropriate Perl functions in them.

like image 585
Mike Avatar asked Jan 22 '10 01:01

Mike


2 Answers

There's the Inline::Ruby module, though I don't have any direct experience with it that I can share.

EDIT: I did try it out last night -- here's the review: Inline::Ruby was last updated in 2002, when v5.6 was the latest stable release. Docs say it has only been tested on Linux; I was trying to use it with v5.10.1 on Cygwin. Got it to build after some hacking of the XS/C code that comes with the module. Passed some unit tests but failed others. Seemed to import Ruby class's into Perl's namespace ok but was less successful importing standalone functions. Summary: if you need a quick and dirty bridge for Perl and Ruby, Inline::Ruby will probably disappoint you. If you have the patience to figure out how to build the module on your system, and to massage your Ruby code to work with the module, you could find it useful.

like image 121
mob Avatar answered Sep 18 '22 05:09

mob


Use Ruby's exec()

rubypl.rb

#!/usr/bin/ruby -w

script = 'perlscript.pl'
exec("/usr/bin/perl #{script}")

perlscript.pl

#!/usr/bin/perl -w
use strict;
print "Please enter your name: ";
my $name = <STDIN>;
chop($name);
if ($name eq "")
{
    print "You did not enter a name!\n";
    exit(1);
} else {
    print "Hello there, " . $name . "\n";
    exit(0);
}
like image 26
user198470 Avatar answered Sep 18 '22 05:09

user198470