By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,203 Members | 1,620 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,203 IT Pros & Developers. It's quick & easy.

Error when reading from file

P: 6
hi...
i am beginner in perl cgi programming..i am atempting to read from a file....
my cgi script is

Expand|Select|Wrap|Line Numbers
  1. #!c:\perl\bin\perl -wT
  2. #print "content-type: text/html \n\n";
  3. use CGI qw(:standard);
  4. use strict;
  5. print header;
  6. print start_html ("ebanking");
  7.  
  8. my $ne=param('name');
  9. my $pwid=param('pwd');
  10.  
  11. open(FP,"input.txt") or die "cannot open file";  
  12. @raw_data=<DAT>;
  13. close(DAT);
  14.  
  15. foreach $account (@raw_data) {
  16.     chop($account);
  17.     ($w_name, $pswd, $accno, $address, $type, $balance) = split(/\|/,$account);
  18.  
  19.     if (( $ne eq $w_name) and ( $pwid eq $pswd)) {
  20.         print " \nName: $w_name Balance: $balance" ;
  21.     } else {
  22.         print "\n User name and password not match";
  23.     }
  24. }#fore each
  25.  
  26.  print end_html;
  27.  
when the above code is executed using WIN 32 apache it shows an internal error...this is the syntax given in every website....
if i remove the print statements corresponding to cgi and run in command line perl it works perfectly....

please help...
Aug 31 '07 #1
Share this Question
Share on Google+
9 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
To begin with, you do not need the shebang line (#!c:\perl\bin\perl -wT) in Windows perl scripting. It is completely ignored. To use taint mode, I believe when you trigger the script on the command line, you would simply specify "perl -T <scriptname>" to trigger it correctly.

Next, do not use the "-w" switch. It is better to use the "use warnings;" pragma instead of the switch.

As for the following code:
Expand|Select|Wrap|Line Numbers
  1. print header;
  2. print start_html ("ebanking");
  3. print end_html;
  4.  
You should really get into the practice of using either just quotes around the text you wish to print or the proper syntax as shown below for these lines. Also, if you have warnings turned on, then Perl will throw an error regarding the use of unquotted strings and their possible conflict with commands or future reserved words.

Expand|Select|Wrap|Line Numbers
  1. print("header");
  2. print ("start_html","ebanking");
  3. print("end_html");
  4.  
Now, try correcting those minor issues and if you could please provide the error that you are seeing, that would be great as well. We just want to see what it is kicking back at you.

Regards,

Jeff
Aug 31 '07 #2

KevinADC
Expert 2.5K+
P: 4,059
Actually Jeff, these are functions of the CGI module (in bold):

print header;
print start_html ("ebanking");
print end_html;

so that is proper syntax. Putting quotes around print statements is actually not very proper:


print("header");
print ("start_html","ebanking");
print("end_html");


Just use quotes sans the parenthesis for simple prints. But as noted, the original code was correct so no parenthesis or quotes are needed in this case.
Sep 1 '07 #3

KevinADC
Expert 2.5K+
P: 4,059
While it is true that Windows ignores the shebang line, apache does not. So using the -T switch should work if apache is being used even on Windows.
Sep 1 '07 #4

KevinADC
Expert 2.5K+
P: 4,059
While it is true that Windows ignores the shebang line, apache does not. So using the -T switch should work if apache is being used even on Windows.

The problem appears to be the use of "strict" (a good thing) but there are a number of undeclared variables:

@raw_data=<DAT>;
foreach $account (@raw_data)
($w_name,$pswd,$accno,$address,$type,$balance)=spl it(/\|/,$account);


the above three lines need to be changed to:

my
@raw_data=<DAT>;
foreach my $account (@raw_data)
my ($w_name,$pswd,$accno,$address,$type,$balance)=spl it(/\|/,$account);
Sep 1 '07 #5

numberwhun
Expert Mod 2.5K+
P: 3,503
See, this is why I need to learn more CGI!!! Sorry about that!

Jeff
Sep 1 '07 #6

numberwhun
Expert Mod 2.5K+
P: 3,503
While it is true that Windows ignores the shebang line, apache does not. So using the -T switch should work if apache is being used even on Windows.
See, that I didn't know. I thought it was completely ignored and not use. I was unaware that Apache still read it. Must keep that bit if information in mind.

Regards,

Jeff
Sep 1 '07 #7

KevinADC
Expert 2.5K+
P: 4,059
I'll make a CGI programmer of you if it kills me.... err.... wounds me.... errr.... causes me mild discomfort in me gulliver. ;)
Sep 1 '07 #8

numberwhun
Expert Mod 2.5K+
P: 3,503
I'll make a CGI programmer of you if it kills me.... err.... wounds me.... errr.... causes me mild discomfort in me gulliver. ;)
No worries, it'll only be a flesh wound. ;-)
Sep 1 '07 #9

miller
Expert 100+
P: 1,089
i am beginner in perl cgi programming..i am atempting to read from a file....
my cgi script is

Expand|Select|Wrap|Line Numbers
  1. open(FP,"input.txt") or die "cannot open file";  
  2. @raw_data=<DAT>;
  3. close(DAT);
  4.  
You're opening the file handle FP, but attempting to read from the handle DAT. Most likely this is a copy and paste error, but that's your most glaring problem.

- Miller
Sep 3 '07 #10

Post your reply

Sign in to post your reply or Sign up for a free account.