Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remote program execution using cgi-perl

Status
Not open for further replies.

jasper23

Programmer
Oct 26, 2007
1
Hello,
I am developing a cgi-perl script that takes some value from a html form as input and stores them in a data file. I have a program called "irr" in my server, it is executed just by typing irr at the shell prompt. Upon execution the program first displays a line describing the types of input required and waits for the input in the next line.I intend to pass the inputs from the values stored in the data file by the script. The following is the code that i came up with:


#!/usr/bin/perl -w
use warnings;
use CGI qw:)all);
use CGI::Carp qw(fatalsToBrowser);
use Fcntl qw:)flock);
use strict;

my $dataFile="data.txt";
print header(-type=>'text/html');
print start_html("Internal Rate of Return Calculator");

my $value = `date`;
print $value,"<br\>";

my $irr;
if(!param)
{
form();
print end_html();
}
else
{
store();
result();
print end_html();

}

#-----------------------------------
sub form(){
print << "HERE";
<form id="form" action="" method="POST"/>
<h1> Welcome to IRR Calculator </h1>
<p>Net-Present-Value: <input name="value" type="text"/></p>
<p>Amortization-In-Years: <input name="years" type="text"/></p>
<p>Monthly-Cashflow: <input name="monthly" type = "text"/></p>
<p><input name="submit" type="submit" value="submit"/></p>
</form>
HERE
}

#------------------------------------
sub store(){
my $value = param('value');
my $years = param('years');
my $monthly = param('monthly');
open (DH, ">$dataFile") || die "Coudn't open the data file: $!";
print DH $value, " ", $years, " ", $monthly;
close(DH);

}

#-------------------------------------
sub result(){
my $irr = `irr<data.txt`;
print $irr;
}

----------------------------------------------------------------
Unfortunately the result is never displayed on the browser. I have tried using the command : `irr<data.txt | cat` but that doesn't work either. Where am I doing wrong? A little help would be greatly appreciated. Thank you.

 
You should put full paths to all programs. If the program is interactive and can't be run just from comman line options you need to look at the Expect module.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
I'd start by trying to narrow in on what is failing. My first guess would be that travs69 is correct. A 'CGI' application runs as the generic web server user on your server. Thus, the environment that it 'sees' may be very different from yours. Normally, for security reasons, you do not give the web server user a wide 'path'. You narrow it down to being able to see/use just those things that it must have. Consequently, it may not be able to find 'irr'. So, give it a fully qualified path to the executable.

I'd try to use as simple a piece of code as possible to trouble shoot getting irr to behave. Something like:

Code:
#!/usr/bin/perl -wT
use strict;
use CGI;
use CGI::Carp qw(FatalsToBrowser, WarningsToBrowser);

my $cgi_obj = new CGI;
print $cgi_obj->header, $cgi_obj->start_html;

my $output_string = `/path/to/irr.pl data.txt`;
print "<pre>$output_string</pre>\n", $cgi_obj->end_html;

exit;

Further, if this code is going on any web server and will be accepting inputs from the a form (anyone who can issue a GET or POST), then you really should be using 'Taint' mode. It forces you to jump through some hoops, but, they are hoops that you really should be jumping through, anyway.

Turn on 'Taint' mode by including the capital 'T' in the first line of your code.

#!/usr/bin/perl -T



'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Try print end_html after the form is sent. ????

Computer thought: I teach a lot of programming so I can learn. You can never learn it all.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top