5 Replies - 14556 Views - Last Post: 28 January 2013 - 02:28 AM

#1 shwetakagliwal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 23-December 07

perl script for prompt box input from user

Posted 23 December 2007 - 10:33 AM

I am working on a statistics project that has to be done using perl. i have basic html knowledge bu dont know perl script. although I know perl.
I have successfully done perl programs for calculating statistical parameters like mean, median, regression, etc.
following is the successful code for calculating mean using perl. i want to run this program on a web browser, so how can i use perl script and html to accept the input required for the number of cells inorder to create a table for data input?
kindly help me.

#!usr/local/bin/perl
print"Enter the observations\n";
@array= <>;

$n = @array;
print"Frequency=$n\n";

$x = 0;

for($i = 0; $i<= ($n-1); $i++)
{
$x = $x + @array[$i];
}

$mean = $x/$n;

print"Arithmetic mean= $mean \n";

Is This A Good Question/Topic? 0
  • +

Replies To: perl script for prompt box input from user

#2 GWatt  Icon User is offline

  • member icon

Reputation: 270
  • View blog
  • Posts: 3,068
  • Joined: 01-December 05

Re: perl script for prompt box input from user

Posted 23 December 2007 - 12:07 PM

You're going to have to use cgi to get this to work. The first thing to do is make a webpage that has the input form.

<html>
	<body>
		<form action = "stat.cgi" method = "POST">
			Data: <input type = "text" name = "data">
			<input type = "Submit" value = "Send">
		</form>
	</body>
</html>



That's your basic html form. Then, create a file called "stat.cgi"
#!/usr/bin/perl -wT

print "Content-type: text/html";	#This is VERY important

$data = param('data');	#get data entered into textbox
@array = split("/\s*/", $data);	#separate with spaces.

# 
# You can put your driver code here
# 


Was This Post Helpful? 0
  • +
  • -

#3 KevinADC  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 401
  • Joined: 23-January 07

Re: perl script for prompt box input from user

Posted 23 December 2007 - 01:45 PM

He will have to use CGI, but your perl example is incomplete, the param() function will not work unless the CGI module is loaded.
Was This Post Helpful? 0
  • +
  • -

#4 GWatt  Icon User is offline

  • member icon

Reputation: 270
  • View blog
  • Posts: 3,068
  • Joined: 01-December 05

Re: perl script for prompt box input from user

Posted 23 December 2007 - 02:03 PM

Oyah.
:splat:

I forgot that part.

So basically, put use CGI qw(:standard); after the shebang line.
Was This Post Helpful? 0
  • +
  • -

#5 tale103108  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 27-January 13

Re: perl script for prompt box input from user

Posted 27 January 2013 - 04:51 PM

Windows Vista SP2
Firefox 18
ActiveState Perl 5.8
======================
All I get back from the browser is my Perl code. Am I missing something here?


#!/usr/bin/perl -wT
use CGI qw(:standard);

print "Content-type: text/html"; #This is VERY important

$data = param('data'); #get data entered into textbox
@array = split("/\s*/", $data); #separate with spaces.

#
# You can put your driver code here
#
Was This Post Helpful? 0
  • +
  • -

#6 dsherohman  Icon User is offline

  • Perl Parson
  • member icon

Reputation: 226
  • View blog
  • Posts: 654
  • Joined: 29-March 09

Re: perl script for prompt box input from user

Posted 28 January 2013 - 02:28 AM

View PostGWatt, on 23 December 2007 - 08:07 PM, said:

print "Content-type: text/html";	#This is VERY important


You forgot another VERY important part: The blank line to indicate the end of the HTTP headers. If you're going to send the content-type manually, that should be:
print "Content-Type: text/html\n\n";



View Posttale103108, on 28 January 2013 - 12:51 AM, said:

All I get back from the browser is my Perl code. Am I missing something here?

Your web server software (probably either Apache or IIS) needs to be told in its configuration that .cgi files should be executed instead of sending their contents to the user as text. In Apache, you would do this by adding the line
AddHandler cgi-script .cgi


to your Apache config file, which I assume would be called httpd.conf, but I have no idea where it would be located. (I work in unix environments, not Windows, so I don't know how things would be laid out on your system.)

Also, while plain old CGI works great for basic interfaces where you just have one or two screens to submit simple data and display simple results, be aware that it can get very convoluted and easily becomes unmaintainable for larger applications with more complex interactions. What you've described of your needs so far should remain well under that threshold, but, if you do start running into problems maintaining structure in your code, you should look into using a web application framework instead of plain CGI. There are several good frameworks out there these days; I personally use (and would recommend) Dancer.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1