Go Back   CodingForums.com > :: Server side development > Perl/ CGI

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-28-2009, 09:49 PM   PM User | #1
pppebble88
New Coder

 
Join Date: Oct 2009
Posts: 41
Thanks: 4
Thanked 0 Times in 0 Posts
pppebble88 is an unknown quantity at this point
Printing Contents of Array in External File to Browser

I am trying to write the contents of an external file onto the web browser. The ultimate goal is to compare the entires in the text file to entries submitted through a form. If they match, perform an operation. When I run the perl command "perl -w submit.pl" in my command prompt, it works correctly. However, when I try to run it in a web browser, nothing displays. I have tried everything I can think of and can't get it working. I would appreciate some help. Thanks a lot.

Code:
# Standard header stuff
use CGI qw( :standard );
use strict;
print( header() );
print( start_html() );

checkFraud();

sub checkFraud  {	
	my $filename = "../fraud-values.txt";

	open(INPUT_FILE, $filename)
		or die "Couldn't open $filename";
	while (<INPUT_FILE>) {
		my $currentLine = $_;
		print ("$currentLine");
	}
	close(INPUT_FILE);

}
print ( end_html() );
pppebble88 is offline   Reply With Quote
Old 10-28-2009, 11:00 PM   PM User | #2
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
Code:
# Standard header stuff
use CGI qw( :standard );
use strict;

sub checkFraud  {	
	my $filename = "../fraud-values.txt";

	open(INPUT_FILE, $filename)
		or die "Couldn't open $filename";
	while (<INPUT_FILE>) {
		my $currentLine = $_;
		print ("$currentLine");
	}
	close(INPUT_FILE);

}


print( header() );
print( start_html() );
checkFraud();
print ( end_html() );
or

Code:
# Standard header stuff
use CGI qw( :standard );
use strict;
print( header() );
print( start_html() );
&checkFraud();
print ( end_html() );

sub checkFraud  {	
	my $filename = "../fraud-values.txt";

	open(INPUT_FILE, $filename)
		or die "Couldn't open $filename";
	while (<INPUT_FILE>) {
		my $currentLine = $_;
		print ("$currentLine");
	}
	close(INPUT_FILE);

}
you don't need to use print so many times. This will also work:
Code:
print header,
   start_html,
   checkFraud(),
   end_html;

best regards

Last edited by oesxyl; 10-28-2009 at 11:03 PM..
oesxyl is offline   Reply With Quote
Users who have thanked oesxyl for this post:
pppebble88 (10-29-2009)
Old 10-28-2009, 11:15 PM   PM User | #3
pppebble88
New Coder

 
Join Date: Oct 2009
Posts: 41
Thanks: 4
Thanked 0 Times in 0 Posts
pppebble88 is an unknown quantity at this point
Thanks for the help...Unfortunately, I am still getting the same results (nothing is displayed in the web browser, but it works fine in the command prompt)

Below is the complete perl file (I have a few other functions declared, but all of them function FINE, which is why I didn't originally include them...)

Thanks again for the continued help!

Code:
# Standard header stuff
use CGI qw( :standard );
use strict;


# Get inputs
my $name    	= param("name");
my $phone   	= param("phone");
my $email   	= param("email");
my $address     = param("streetAddress");
my $city    	= param("city");
my $state   	= param("state");
my $zip     	= param("zip");
my $passwd   	= param("passwd");
my @events      = param("events");
my $firstClub   = param("firstClub");
my $comments    = param("experience");
my $years 	    = param("years");
my $validInput = 1;



#validateInfo();
#if($validInput == 1) {
#	printHeader();
#}
#checkFraud();

sub validateInfo  {
	my $message1 = "";
	my $message2 = "";
	my $message3 = "";
	if(($name eq "") )  {
		$message1 = "You must enter a name!";
		$validInput = 0;
	}
	if(!($email =~ /^\w.+\@\w+(\.org|\.net|\.com)$/i )){
		 $message2 = "You must enter a valid email addres (Eg. home\@home.net or home\@home.com or home\@home.org) ";
		 $validInput = 0;
	}
	if((!@events)) {
		$message3 = "You must select at least ONE event";
		$validInput = 0;
	}
	if(!$validInput) {
		print h1("$message1" . "<br />" . "$message2"  . "<br />" . "$message3");
		print ("Click <a href='http://www.m111662.it350.cs.usna.edu/Lab08/register.html'>HERE</a> to go back and make the appropriate corrections <br />");
	}
	 
}

sub printHeader	{
	print h1("Registration Submitted!");
	print ("Below are the details of your submission.  Thanks a lot! <br />");
	print ("<br />");
	print ("Name: $name <br />");
	print ("Phone: $phone <br />");
	print ("Email: $email <br />");
	print ("Address: $address <br />");
	print ("City: $city <br />");
	print ("State: $state <br />");
	print ("Zip: $zip <br />");
	print ("Password: $passwd <br />");
	print ("Events: @events <br />");
	print ("Is this your first time to be a member of a running club?: $firstClub <br />");
	print ("Experience: $comments <br />");
	print ("Years of running experience: $years <br />");
}

sub checkFraud  {	
	my $filename = "../fraud-values.txt";

	open(INPUT_FILE, $filename)
		or die "Couldn't open $filename";
	while (<INPUT_FILE>) {
		my $currentLine = $_;
		print ("$currentLine");
	}
	close(INPUT_FILE);
}

print( header() );
print( start_html() );
checkFraud();
print ( end_html() );
-W
pppebble88 is offline   Reply With Quote
Old 10-28-2009, 11:33 PM   PM User | #4
FishMonger
Super Moderator


 
Join Date: May 2005
Location: Southern tip of Silicon Valley
Posts: 2,753
Thanks: 2
Thanked 149 Times in 144 Posts
FishMonger will become famous soon enoughFishMonger will become famous soon enough
Have you checked the web server error log?

Have you verified that the permissions are set correctly?

Is the script it the cgi-bin directory?

What url are you specifying?

Add this with the "Standard header stuff"
Code:
use CGI::Carp qw(fatalsToBrowser);
FishMonger is offline   Reply With Quote
Users who have thanked FishMonger for this post:
pppebble88 (10-29-2009)
Old 10-28-2009, 11:44 PM   PM User | #5
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
also check the path to the "../fraud-values.txt" file. I suspect that this is the problem.
it work for me from command line and as cgi script.

Edit: by the way, you don't have shbang in what you post here, I hope you have it in the original code

best regards

Last edited by oesxyl; 10-28-2009 at 11:47 PM..
oesxyl is offline   Reply With Quote
Users who have thanked oesxyl for this post:
pppebble88 (10-29-2009)
Old 10-29-2009, 01:43 AM   PM User | #6
pppebble88
New Coder

 
Join Date: Oct 2009
Posts: 41
Thanks: 4
Thanked 0 Times in 0 Posts
pppebble88 is an unknown quantity at this point
Ok,

I have tried what yall have suggested and I get the following error:

Quote:
Software error:

Couldn't open fraud-values.txt at d:\inetpub\it350\www.m111662.it350.cs.usna.edu\Lab08\submit.pl line 75.
The text file is in the same directory as the submit.pl script. Below is the exact code that is in the perl script. Thanks for all of the continued help!

Code:
#!/usr/local/bin/perl
# Standard header stuff
use CGI qw( :standard );
use strict;
use CGI::Carp qw(fatalsToBrowser);


# Get inputs
my $name    	= param("name");
my $phone   	= param("phone");
my $email   	= param("email");
my $address     = param("streetAddress");
my $city    	= param("city");
my $state   	= param("state");
my $zip     	= param("zip");
my $passwd   	= param("passwd");
my @events      = param("events");
my $firstClub   = param("firstClub");
my $comments    = param("experience");
my $years 	    = param("years");
my $validInput = 1;



#validateInfo();
#if($validInput == 1) {
#	printHeader();
#}
#checkFraud();

sub validateInfo  {
	my $message1 = "";
	my $message2 = "";
	my $message3 = "";
	if(($name eq "") )  {
		$message1 = "You must enter a name!";
		$validInput = 0;
	}
	if(!($email =~ /^\w.+\@\w+(\.org|\.net|\.com)$/i )){
		 $message2 = "You must enter a valid email addres (Eg. home\@home.net or home\@home.com or home\@home.org) ";
		 $validInput = 0;
	}
	if((!@events)) {
		$message3 = "You must select at least ONE event";
		$validInput = 0;
	}
	if(!$validInput) {
		print h1("$message1" . "<br />" . "$message2"  . "<br />" . "$message3");
		print ("Click <a href='http://www.m111662.it350.cs.usna.edu/Lab08/register.html'>HERE</a> to go back and make the appropriate corrections <br />");
	}
	 
}

sub printHeader	{
	print h1("Registration Submitted!");
	print ("Below are the details of your submission.  Thanks a lot! <br />");
	print ("<br />");
	print ("Name: $name <br />");
	print ("Phone: $phone <br />");
	print ("Email: $email <br />");
	print ("Address: $address <br />");
	print ("City: $city <br />");
	print ("State: $state <br />");
	print ("Zip: $zip <br />");
	print ("Password: $passwd <br />");
	print ("Events: @events <br />");
	print ("Is this your first time to be a member of a running club?: $firstClub <br />");
	print ("Experience: $comments <br />");
	print ("Years of running experience: $years <br />");
}

sub checkFraud  {	
	my $filename = "fraud-values.txt";

	open(INPUT_FILE, $filename)
		or die "Couldn't open $filename";
	while (<INPUT_FILE>) {
		my $currentLine = $_;
		print ("$currentLine");
	}
	close(INPUT_FILE);
}

print( header() );
print( start_html() );
checkFraud();
print ( end_html() );
pppebble88 is offline   Reply With Quote
Old 10-29-2009, 01:51 AM   PM User | #7
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
ooops, windowsss,
I'm not familiar with this os,
anyway check what FishMonger suggested, cgi-bin, server logs, permission, url, read his post again.

best regards
oesxyl is offline   Reply With Quote
Users who have thanked oesxyl for this post:
pppebble88 (10-29-2009)
Old 10-29-2009, 03:26 AM   PM User | #8
FishMonger
Super Moderator


 
Join Date: May 2005
Location: Southern tip of Silicon Valley
Posts: 2,753
Thanks: 2
Thanked 149 Times in 144 Posts
FishMonger will become famous soon enoughFishMonger will become famous soon enough
Since you're using inetpub, it would appear that you're using IIS, which I haven't used for a long time. And, since this looks to be a homework assignment, we won't be able to provide a complete answer. However, we still can give you a little guidance.

Try specifying the full path to fraud-values.txt.
FishMonger is offline   Reply With Quote
Old 10-29-2009, 03:38 AM   PM User | #9
pppebble88
New Coder

 
Join Date: Oct 2009
Posts: 41
Thanks: 4
Thanked 0 Times in 0 Posts
pppebble88 is an unknown quantity at this point
FishMonger,

Thanks for the info. This is actually a lab and we are allowed to use any sources available to us. I figured it was a "path" issue, but if I do not know the directory structure, how am I going to insert the full path to the .txt file?

Thanks.
pppebble88 is offline   Reply With Quote
Old 10-29-2009, 03:45 AM   PM User | #10
FishMonger
Super Moderator


 
Join Date: May 2005
Location: Southern tip of Silicon Valley
Posts: 2,753
Thanks: 2
Thanked 149 Times in 144 Posts
FishMonger will become famous soon enoughFishMonger will become famous soon enough
Well, you stated that the text file is in the same directory as the script, so in what directory did you put the script. BIG hint, look at your error message.
FishMonger is offline   Reply With Quote
Old 10-29-2009, 03:48 AM   PM User | #11
pppebble88
New Coder

 
Join Date: Oct 2009
Posts: 41
Thanks: 4
Thanked 0 Times in 0 Posts
pppebble88 is an unknown quantity at this point
The text file IS in the same directory as the script...I am wondering why it is adding the "www.m111662.it350.cs.usna.edu\Lab08\submit.pl" onto the path "d:\inetpub\it350\"...That is where I think the problem is...It is using both a full path in addition to the web site address...That is what the error message says...

I must be missing something. Thanks.
pppebble88 is offline   Reply With Quote
Old 10-29-2009, 03:52 AM   PM User | #12
pppebble88
New Coder

 
Join Date: Oct 2009
Posts: 41
Thanks: 4
Thanked 0 Times in 0 Posts
pppebble88 is an unknown quantity at this point
GOT IT!!!

I used the following additions to my code:

use Cwd 'abs_path';
print abs_path($0);

to get the full path and then set the path to:

my $filename = "d:/inetpub/it350/www.m111662.it350.cs.usna.edu/fraud-values.txt";

Awesome...Thanks for all of the help!
pppebble88 is offline   Reply With Quote
Old 10-29-2009, 03:57 AM   PM User | #13
pppebble88
New Coder

 
Join Date: Oct 2009
Posts: 41
Thanks: 4
Thanked 0 Times in 0 Posts
pppebble88 is an unknown quantity at this point
Question:

Shouldn't this code match the value in $name to the currently entry being examined from the file?

Code:
sub checkFraud  {	
	my $filename = "d:/inetpub/it350/www.m111662.it350.cs.usna.edu/fraud-values.txt";	

	open(INPUT_FILE, $filename)
		or die "Couldn't open $filename";
	while (<INPUT_FILE>) {
		my $currentLine = $_;
		if($currentLine eq $name) {
			print ("FRAUD!");
		}		
	}
	close(INPUT_FILE);
}
pppebble88 is offline   Reply With Quote
Old 10-29-2009, 06:35 AM   PM User | #14
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
Quote:
Originally Posted by pppebble88 View Post
Question:

Shouldn't this code match the value in $name to the currently entry being examined from the file?

Code:
sub checkFraud  {	
	my $filename = "d:/inetpub/it350/www.m111662.it350.cs.usna.edu/fraud-values.txt";	

	open(INPUT_FILE, $filename)
		or die "Couldn't open $filename";
	while (<INPUT_FILE>) {
		my $currentLine = $_;
		if($currentLine eq $name) {
			print ("FRAUD!");
		}		
	}
	close(INPUT_FILE);
}
depend if $name is defined somewhere and what is inside.

best regards
oesxyl is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:50 PM.


Advertisement
Log in to turn off these ads.