Topic: How to execute a Perl script in rails ?

Hi everyone,

I would like to build a web application able to analyse an uploaded file and then display the results.
My problem is that the analysis part will be composed of Perl scripts. I don't know anything about Perl (the script will be created by my client's team which only know that language) and I can't figure out where to start to execute a Perl script with Rails.

For information, the Perl scripts analyse the file and create an XML that Rails will use to display the information

In brief, here is what I would like to achieve :
Upload a file (Rails) => Launch the Perl analysis script (Rails) => File analysis (Perl) => Display the results (Rails)

Is it possible for Rails to use some Perl ? If yes, how can we execute a Perl script with rails ? Is the solution has something to do with CGI (I don't know anything about what this can be either) ?

Thanks in advance, for those that will take the time to answer my question.
Sylvain

Re: How to execute a Perl script in rails ?

Two options off the top of my head:
1) a Kernel.system call to Perl

system("perl your_perl_script.pl")

The problem with this may be that it only returns true or false depending on the success/failure of the system call to execute. If you need to store the results of the Perl script you'll want to use:

2) a Kernel.` call to Perl (note: that is a backtick)

results = `perl your_perl_script.pl`

Hopefully this gets you started.

"Yeah, well, you know, that's just, like, your opinion, man." -El Duderino

http://jerodsanto.net

Re: How to execute a Perl script in rails ?

Thank you so much for that, you saved my day !

To be sure I understand, I have to place my Kernel.system or Kernel.` in my ruby code (like in a controller) in order to execute the script and that's all ?

Few other questions :
Does my Perl script has to be stored in a specific folder in my rails App ?
What do I have to setup or install on my web server to launch a Perl script from Rails ?

Thanks again, I'm working on your answer.

Have a nive day everyone,
Sylvain

Re: How to execute a Perl script in rails ?

The solution I want to share is also an open question.

Would it be better if you do a HTTP request on a CGI ?
You get back the results and analyse it according to the output of your perl script.

Just depends of the usage and the purpose of your perl script.

Re: How to execute a Perl script in rails ?

nivuniconnu wrote:

To be sure I understand, I have to place my Kernel.system or Kernel.` in my ruby code (like in a controller) in order to execute the script and that's all ?

Correct.

nivuniconnu wrote:

Does my Perl script has to be stored in a specific folder in my rails App ?

No. It can be stored anywhere on disk but you may want to put it in your Rails app to keep everything together. You could create a /bin directory in your Rails root and call the script like this:

system "perl #{RAILS_ROOT}/bin/your_perl_script.pl"

nivuniconnu wrote:

What do I have to setup or install on my web server to launch a Perl script from Rails ?

All you need is Perl, Ruby, and Rails installed on the system and you're good to go.

"Yeah, well, you know, that's just, like, your opinion, man." -El Duderino

http://jerodsanto.net

Re: How to execute a Perl script in rails ?

PierreLecocq wrote:

Would it be better if you do a HTTP request on a CGI ?
You get back the results and analyse it according to the output of your perl script.

Just depends of the usage and the purpose of your perl script.

I must say, I don't know much about CGI, I tried to do some research about this but I didn't manage to understand the difference between a Perl/CGI script and a standard Perl script. What makes you choose one or the other solution ?

However, thanks for the tip, I'm also going to look deeper about this.
Sylvain

Re: How to execute a Perl script in rails ?

Thanks for your support sant0sk1,

I tried all this and it worked !
I've got one final question,
I have to tell the perl script which file has to be analyse, is ruby able to send data to the perl script using your solution ?
The ruby docs isn't very talkative about this.

Re: How to execute a Perl script in rails ?

thanks, that helped me a bit as well
i have exporting to excel working from my model using a plugin by Brian P. Hogan / NAPCS...
but for importing data from excel files (out of rails) i was using the roo plugin....

for some reason i couldnt figure out i couldnt have both
require 'roo'
require 'excel'

i think each plug in was over riding each others methods?

but i fixed my problem by doing this in my import action

if (file_name.slice("jbar") == "jbar") || (file_name.slice("jibar") == "jibar")
      @result = system("ruby script/web_populations/jibars.rb #{params[:url]}")
    end
    if file_name.slice("prime") == "prime"
      @result = system("ruby script/web_populations/prime.rb #{params[:url]}")
    end
    if file_name.slice("repo") == "repo"
      @result = system("ruby script/web_populations/repo.rb #{params[:url]}")
    end
    if file_name.slice("treasbil") == "treasbil"
      @result = system("ruby script/web_populations/tbill.rb #{params[:url]}")
    end
    if file_name.slice("bankacc") == "bankacc"
      @result = system("ruby script/web_populations/ba3.rb #{params[:url]}")
    end
    if file_name.slice("mtmdetail") == "mtmdetail"
      @result = system("ruby script/web_populations/bondexchange.rb #{params[:url]}")
    end

i know its not the best solution... but its working for now

thanks sant0sk1

Re: How to execute a Perl script in rails ?

Gone through the tutorial but my connections are making some problem... I will check this once more.


ringing in ear

Last edited by SabraNorman (2011-03-24 03:42:24)