Error when reading from file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sdja
    New Member
    • Aug 2007
    • 6

    Error when reading from file

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

    [code=perl]
    #!c:\perl\bin\p erl -wT
    #print "content-type: text/html \n\n";
    use CGI qw(:standard);
    use strict;
    print header;
    print start_html ("ebanking") ;

    my $ne=param('name ');
    my $pwid=param('pw d');

    open(FP,"input. txt") or die "cannot open file";
    @raw_data=<DAT> ;
    close(DAT);

    foreach $account (@raw_data) {
    chop($account);
    ($w_name, $pswd, $accno, $address, $type, $balance) = split(/\|/,$account);

    if (( $ne eq $w_name) and ( $pwid eq $pswd)) {
    print " \nName: $w_name Balance: $balance" ;
    } else {
    print "\n User name and password not match";
    }
    }#fore each

    print end_html;
    [/code]

    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...
    Last edited by miller; Sep 3 '07, 07:30 PM. Reason: Code Tag and ReFormatting
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    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:
    [code=perl]
    print header;
    print start_html ("ebanking") ;
    print end_html;
    [/code]

    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.

    [code=perl]
    print("header") ;
    print ("start_html"," ebanking");
    print("end_html ");
    [/code]

    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

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      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.

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #4
        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.

        Comment

        • KevinADC
          Recognized Expert Specialist
          • Jan 2007
          • 4092

          #5
          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 )=split(/\|/,$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 )=split(/\|/,$account);

          Comment

          • numberwhun
            Recognized Expert Moderator Specialist
            • May 2007
            • 3467

            #6
            See, this is why I need to learn more CGI!!! Sorry about that!

            Jeff

            Comment

            • numberwhun
              Recognized Expert Moderator Specialist
              • May 2007
              • 3467

              #7
              Originally posted by KevinADC
              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

              Comment

              • KevinADC
                Recognized Expert Specialist
                • Jan 2007
                • 4092

                #8
                I'll make a CGI programmer of you if it kills me.... err.... wounds me.... errr.... causes me mild discomfort in me gulliver. ;)

                Comment

                • numberwhun
                  Recognized Expert Moderator Specialist
                  • May 2007
                  • 3467

                  #9
                  Originally posted by KevinADC
                  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. ;-)

                  Comment

                  • miller
                    Recognized Expert Top Contributor
                    • Oct 2006
                    • 1086

                    #10
                    Originally posted by sdja
                    i am beginner in perl cgi programming..i am atempting to read from a file....
                    my cgi script is

                    [code=perl]
                    open(FP,"input. txt") or die "cannot open file";
                    @raw_data=<DAT> ;
                    close(DAT);
                    [/CODE]
                    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

                    Comment

                    Working...