0

I was asked to develop a web front end to a script that was written in Perl. I have no idea where to begin. How could a webpage interact with a command line script?

EDIT: So after two weeks of research, I have the project completed. The basic answer I was looking for is: develop a webpage using html, use cgi to communicate with the perl script and reconstruct the script to work with a webpage. Thanks for everyone's help though, it was somewhat helpful.

Zatara7
  • 241
  • 2
  • 11
  • Just like any other application, using HTTP. – Hunter McMillen Jun 20 '13 at 15:29
  • What research have you done? I'm pretty sure some googling would uncover where to start on this... – MiiisterJim Jun 20 '13 at 15:46
  • Question not clear. Do you want to develop a Perl web app, or do you want to develop a Perl web client or do you want a web app written in PHP or some other language (including Perl) to interact with a Perl script from inside the web server? – ruben2020 Jun 20 '13 at 15:52

2 Answers2

3

To get your Perl script to interact with a web app over the internet:

Look at my example code here.

I am using the HTTP and LWP Perl modules to interact with this web app.

It's posting a form.

Basically, what this script is doing, is it is iterating through a list of Qt translation files and it is POSTing it to this online web app, which is translating it using Bing Translation API. And then the script gets the translated file back and saves it to the harddisk. In the POST form, it also specifies what is the source and target languages are. So, this is an example of a script running on your PC and interacting with a web app on the internet including sending data and files and receiving them back.

Here's a good book chapter on the topic.

To develop a web app using Perl:

Use modperl with Apache. The wikipedia page for this also has some info.

The other way is to do CGI scripting with Perl. Here's a tutorial and the wikipedia page for this is also useful. If you're not sure what is CGI, read this or this.

And yet another way is to use Perl web frameworks like Dancer, Mojolicious and Catalyst.

ruben2020
  • 1,549
  • 14
  • 24
  • I do not want to develop a web app using Perl. I would like the web app to interact with a Perl script. – Zatara7 Jun 20 '13 at 15:35
  • That's what developing a web app _is_ – Bojangles Jun 20 '13 at 15:40
  • 1
    @Zatara7 — If you want to develop the web application in some other language then the fact that the existing application is written in Perl doesn't really matter, but the language you want to use matters **a lot** and the only language you've mentioned is Perl. – Quentin Jun 20 '13 at 15:41
  • @Zatara7 - You mean you want to write a web client using Perl, right? Look at my book chapter link in the answer for more info. – ruben2020 Jun 20 '13 at 15:49
3

There are two sides to this.

Writing a web front end

Generally speaking, you should use Plack/PGSI for this. It acts as a middleware layer that lets you run a Perl web application in a variety of ways (I usually go for FastCGI, other options include mod_perl and CGI).

You can use a framework such as Dancer or Catalyst (which use Plack under the hood), but they are probably overkill (depending on what the script does).

If you needs are very basic, you can use CGI without Plack (the CGI module makes this quite easy). CGI isn't very efficient, but it is very easy to configure. You won't notice the inefficiencies unless you are calling the script frequently or the script has a high startup time.

Accessing the existing script

The quick and dirty way would be for your web application to spawn a shell and run it in that. You could use backticks, system or exec or, if you want more control, IPC::Run.

The robust way would be to break out the business logic of the script into a module and rewrite the command line program to parse arguments and then involve the appropriate functions from the module. You then do the same thing for the web application (the difference being how you present the input and output).

Community
  • 1
  • 1
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335