How can you interact with Perl programs from Ruby?

I understand that there is no “bridge” between Ruby and Perl so that you can directly access Perl functions from Ruby. I also understand that to call a Perl program from Ruby, you just put it in backticks (i.e. result = `./helloWorld.pl` ). However, this does not allow you to interact with the Perl program (i.e. you cannot interact with prompts or provide input). My quesitons are as follows:

  • Is there a way to provide input for Perl programs from Ruby (other than arguments)?

  • Am I really mistaken that there is no bridge between Ruby and Perl? Interacting with the stdin program seems like the wrong way to navigate the prompts, and the programs I'm dealing with are well designed and have libraries with relevant Perl functions.

+6
ruby perl
source share
8 answers

What you want does not really exist, as far as I know.

The closest thing to what you want, at a general level, is XDebug . It turns the process into a small server that will receive debug commands. This is usually used for debugging and profiling, and not as interprocess communication, but it is an opportunity. I believe that ActiveState Perl can be run as an XDebug server.

Otherwise, you need to explicitly program some kind of side channel that your Perl program listens for commands (which XDebug does). It can be as simple as opening a socket that reads a string, talks about it, encodes the result as YAML (or something else) and writes it back. REPL, but on the connector, not on the terminal.

There are obviously security implications that will remain as an exercise for the reader. You also don’t want to listen to the socket to interrupt the program, so now you need something event driven or streaming.

Sorry, I have nothing more specific. This will make an excellent CPAN module.

+2
source share

There's an Inline :: Ruby module, although I have no direct experience with it, which I can share.

EDIT . I tried this last night. Here's a review: Inline::Ruby was last updated in 2002, when v5.6 was the last stable release. The docs say it was tested only on Linux; I tried 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. Some unit tests passed, but others failed. It seemed that importing a Ruby class into the Perl namespace was ok, but it was less successful to import stand-alone 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 a module on your system and massage your Ruby code to work with the module, you may find it useful.

+5
source share

Use Ruby 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); } 
+5
source share

Here's how ruby ​​can use a python script, interacting with the stdin and stdout script.

foo.py reads two integers (each on a separate line) from standard input, adds them and writes the result to standard. I do not know perl, so please be kind to me:

 #!/usr/bin/perl $a = <STDIN>; $b = <STDIN>; $c = int($a) + int($b); print $c; 

foo.rb does foo.py, adding two numbers to add, returning the result and printing it:

 #!/usr/bin/ruby1.8 a = 1 b = 2 c = IO.popen('./foo.py', 'w+') do |pipe| pipe.puts(a) pipe.puts(b) pipe.close_write pipe.read end raise "foo.py failed" unless $? != 0 print "#{a} + #{b} = #{c}" # => 1 + 2 = 3 
+3
source share

perldoc perlipc :

 DESCRIPTION The basic IPC facilities of Perl are built out of the good old Unix signals, named pipes, pipe opens, the Berkeley socket routines, and SysV IPC calls. Each is used in slightly different situations. 

Ruby is able to manage each of them.

+3
source share

Perl and Ruby have different “adhesives”. Perl programs using the Expect module can do much more than just "wait for an exit." Most likely, you could communicate with a well-known protocol such as HTTP ... the Ruby daemon could expose a listener, and Perl could communicate with it or vice versa.

But no, you can't just “embed” ruby ​​code in Perl or vice versa. You have to start separate processes and communicate somehow.

+2
source share

Try Open4 . It is not designed specifically for interacting with perl, but with any program that requires input and output. I'm still learning how to use it, but I feel that it can satisfy your needs.

+2
source share

You mentioned in one of your comments that you want to run Perl code inside Ruby. This will not work if you cannot get the Ruby interpreter to understand Perl syntax. Can I get BNF / yacc / RE for Perl? Helps you understand what problems you will encounter.

0
source share

All Articles