You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Vineet Chopra <vi...@gmail.com> on 2006/11/28 16:47:31 UTC

Need help in calling a perl script

Hi All,
I am using struts 1.2.9 and wanted to call a perl script.
If any one has done it, will appreciate the help.
An Example or a sample code will be great.

Also is it possible to send input parameters to the script?

-- 
Thank you,
Vineet Chopra

Re: [OT] Need help in calling a perl script

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Vineet,

Vineet Chopra wrote:
> Hi All,
> I am using struts 1.2.9 and wanted to call a perl script.
> If any one has done it, will appreciate the help.
> An Example or a sample code will be great.

Do you just want to call Perl on the command line, or do you have some
kind of perl-cgi thing that you need to interact with?

> Also is it possible to send input parameters to the script?

That depends on how you want to invoke Perl. If you are using a URL to
call-back to your web server in order to invoke Perl on your behalf
(using CGI), then no, you can't send command-line parameters to Perl...
you'll have to use CGI for that.

On the other hand, if you're going to be calling Perl directly, then you
can certainly use command-line parameters.

I'm sure there are HOWTOs out there for how to do this kind of thing,
but you want to start by looking at the java.lang.Runtime class's exec()
methods. There are a bunch, depending on what information you want to
pass. Choose carefully.

The following considerations are extremely important:

1. Using Runtime.exec() allows users of your webapp to start new
   processes on your server. Are you really sure you want this to
   happen? Imagine if all of your users hit your Perl-invoking
   action at once. Would it kill your server?

2. You can't trust any information passed-in through request
   parameters. If you want to use command-line parameters,
   make sure that you "taint check" them so that someone
   doesn't add "; rm -rf /" to the end of your command and
   end up destroying your filesystem.

3. Always remember to handle the resulting Process object (returned
   from Runtime.exec) properly. For instance, if you don't get the
   input and error streams from your spawned-process and read from them,
   they may never complete and never fail. Always read error and input
   until they are closed. If you are not providing input (via stdin,
   using an OutputStream from Java) to your process, then consider
   closing the OutputStream so that your process won't (accidentally)
   wait around for data on stdin.

4. Unless you really know what you are doing, always call
   Process.waitFor() to make sure that the process has terminated.
   You don't want a ton of processes starting and never finishing,
   without you knowing about it. At least if your Perl processes are
   never finishing, you'll know it because you'll get problems
   in your webapp, too.

Lastly, what are you doing with Perl? Can the same thing be done within
Java? That would be ideal, since spawning processes from a webapp is a
dangerous business. Alternatively, could you put information in a
database describing what needs to be done, and run Perl in a
batch-oriented way -- like using a cron job to periodically do these
operations? I think you'll end up with a more stable webapp that way.

Hope that helps,
- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFbGKf9CaO5/Lv0PARAtSnAJ9j4+uReDOhs2PvPvs1pU2ADIRsYwCdGllD
fUZOCFRzb1Siin728+Snh/Q=
=ziUJ
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org