Bizar variable problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris W

    Bizar variable problem

    I have a simple html form that sends 3 values to perl cgi script. The
    perl script then makes some calculations and prints the results to the
    browser. It also writes the values to a file with some other static
    text. Problem is that the values don't get saved to the file. The file
    is created and the static text shows up in the file on both sides of
    where the values should be, if I print the variables to the browser it
    shows them correctly. Now for the bizarre part, if I do this in an old
    version of netscape (4.7) then it works fine but any other browser I
    have tried (IE, Netscape 7.1, Opera) it gives me this problem, which is
    really strange since I can't see how the browser has any effect on the
    server side of things. I have tried the script on both MS IIS and
    Apache with the same result.


    --
    Chris W
  • Gunnar Hjalmarsson

    #2
    Re: Bizar variable problem

    Chris W wrote:[color=blue]
    > ... It also writes the values to a file with some other static
    > text. Problem is that the values don't get saved to the file.[/color]

    <snip>
    [color=blue]
    > Now for the bizarre part, if I do this in an old version of
    > netscape (4.7) then it works fine but any other browser I have
    > tried (IE, Netscape 7.1, Opera) it gives me this problem, which is
    > really strange since I can't see how the browser has any effect on
    > the server side of things.[/color]

    How about posting the script or, even better, a short version of it
    that illustrates the problem you describe.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl

    Comment

    • Chris W

      #3
      Re: Bizar variable problem

      Ok here is the code.

      #!c:\perl\bin\p erl.exe
      use CGI qw(:standard);
      use CGI::Carp qw/fatalsToBrowser/;
      require 'config.pl';

      ############### ###############
      ###Variables in question######
      $item = param("item");
      $qty = param("qty");
      $price = param("price");
      ############### ###############

      $cartFileName = CGI::cookie("Ca rtFile");
      $total = $qty * $price;
      $shipping = "\$3.00";
      $gtotalstr = sprintf "%9.2f", ($total + 3 + ($total * $tax));
      $taxstr = sprintf "%9.2f", ($total * $tax);
      $totalstr = sprintf "%9.2f", $total;

      ############### ############### ############### ############### ######
      #########I have moved this code to various places and still no joy
      ############### ############### ############### ############### ######
      open CARTFILE, ">$orderdir/$cartFileName";
      print CARTFILE "$item - $qty - \$$price\n"; #THIS IS WHAT DOESN'T
      WORK.
      ######the two "-"s and the "$" are all that's in the file.
      close CARTFILE;

      print CGI::header();
      open TEMPLET, "<$cgidir/cart.html";
      while(<TEMPLET> ){
      print;
      if(/BODY GOES HERE/){

      ############### ############### ############
      #######This all prints as expected####### #
      print "<br><br>$i tem - $qty - \$$price\n<BR>< BR>";

      print <<"[END]";

      Sub Total:\$$totals tr<br>
      Tax :\$$taxstr<br>
      Shipping :$shipping<br>
      Total :\$$gtotalstr<b r>
      <FORM ACTION="$hcgidi r/GetPayment.pl" METHOD=POST>
      <INPUT TYPE="HIDDEN" NAME="total" VALUE="$total">
      Please select payment method.<br>
      <input type="radio" name="paymentTy pe" value="CreditCa rd">Credit
      Card
      <input type="radio" name="paymentTy pe" value="Check">M ail Check
      <INPUT TYPE="submit" name="submit" value="Continue ">
      </FORM>
      <br>
      </form>

      [END]

      }#end if
      } #end while templet



      --
      Chris W

      Comment

      • Gunnar Hjalmarsson

        #4
        Re: Bizar variable problem

        Chris W wrote:[color=blue]
        > Ok here is the code.
        >
        > #!c:\perl\bin\p erl.exe[/color]

        use strict;
        use warnings;

        <snip>
        [color=blue]
        > open CARTFILE, ">$orderdir/$cartFileName";
        > print CARTFILE "$item - $qty - \$$price\n"; #THIS IS WHAT DOESN'T WORK.[/color]

        open CARTFILE, ">$orderdir/$cartFileName" or die $!;

        The above suggestions are the basic methods to ask Perl for help with
        finding various code errors. Please use those methods before asking a
        Usenet group to help you debug your code.

        --
        Gunnar Hjalmarsson
        Email: http://www.gunnar.cc/cgi-bin/contact.pl

        Comment

        • Chris W

          #5
          Re: Bizar variable problem

          Gunnar Hjalmarsson wrote:[color=blue]
          >[color=green]
          > > #!c:\perl\bin\p erl.exe[/color]
          >
          > use strict;
          > use warnings;[/color]

          No change in behavior of the code once all the strict stuff was added.
          The only warnings were about a few vars from config.pl that are only
          used once and that isn't part of the problem

          [color=blue]
          >
          > open CARTFILE, ">$orderdir/$cartFileName" or die $!;[/color]

          I took my long or die thing out to simplify and that obviously isn't the
          problem, since as I said in my first message, the file is being created
          and written to, it just doesn't write the value of my variables in the
          file.

          Chris W

          Comment

          • Gunnar Hjalmarsson

            #6
            Re: Bizar variable problem

            Chris W wrote:[color=blue]
            > Gunnar Hjalmarsson wrote:[color=green]
            >>
            >> open CARTFILE, ">$orderdir/$cartFileName" or die $!;[/color]
            >
            > I took my long or die thing out to simplify[/color]

            I suggest that you take it back. You should make it a habit to never
            "simplify" that way, btw.
            [color=blue]
            > and that obviously isn't the problem,[/color]

            That's not obvious to me.
            [color=blue]
            > since as I said in my first message, the file is being created and
            > written to, it just doesn't write the value of my variables in the
            > file.[/color]

            If the variables are populated (which they obviously are if their
            content is printed to the screen), I find it very hard to believe that
            the script would actually print to the file and still exclude the
            variables. Maybe there is just an old file copy, and nothing gets
            printed. Maybe, since the filename seems to be dependent on some
            cookie, there is a cookie problem... You would get a clue if you
            checked the return value of that open statement.

            --
            Gunnar Hjalmarsson
            Email: http://www.gunnar.cc/cgi-bin/contact.pl

            Comment

            • Chris W

              #7
              Re: Bizar variable problem

              Gunnar Hjalmarsson wrote:
              [color=blue]
              > If the variables are populated (which they obviously are if their
              > content is printed to the screen), I find it very hard to believe that
              > the script would actually print to the file and still exclude the
              > variables.[/color]

              I find it hard to believe too, that's why I have tested it a million
              times and, that is exactly what happens if I use any browser except
              Netscape 4.7


              [color=blue]
              > Maybe there is just an old file copy, and nothing gets
              > printed.[/color]

              The file name is based on the time of day and every time I run the
              script it creates a new file I now have about 50 files with a first line
              that looks like "$ - -", so I know that's not the problem. Another
              script down the road also has not problem adding other data to the file,
              which makes it even more difficult to believe.


              [color=blue]
              > Maybe, since the filename seems to be dependent on some
              > cookie, there is a cookie problem... You would get a clue if you
              > checked the return value of that open statement.[/color]

              adding to or die in there doesn't change anything.


              I Just did some experimenting and noticed something odd. . . if I change

              open CARTFILE, ">$conf::orderd ir/$cartFileName" or die $!;
              #the file should already exist at this point

              to

              open CARTFILE, ">>$conf::order dir/$cartFileName" or die $!;

              Then the first line of the file is exactly like I want it to be, but it
              also prints a second line that doesn't have any values, just the " - -
              $"

              So what does that tell you?

              --
              Chris W

              Comment

              • Chris W

                #8
                Re: Bizar variable problem

                Chris W wrote:
                [color=blue]
                > I Just did some experimenting and noticed something odd. . . if I change
                >
                > open CARTFILE, ">$conf::orderd ir/$cartFileName" or die $!;
                > #the file should already exist at this point
                >
                > to
                >
                > open CARTFILE, ">>$conf::order dir/$cartFileName" or die $!;
                >
                > Then the first line of the file is exactly like I want it to be, but it
                > also prints a second line that doesn't have any values, just the " - -
                > $"[/color]

                I forgot to mention that again it works in netscape 4.7 as I expect it
                to, it is only when I use any other browser that it has the problem.

                Chris W

                Comment

                • Gunnar Hjalmarsson

                  #9
                  Re: Bizar variable problem

                  Chris W wrote:[color=blue]
                  > Gunnar Hjalmarsson wrote:[color=green]
                  >> Maybe, since the filename seems to be dependent on some cookie,
                  >> there is a cookie problem... You would get a clue if you checked
                  >> the return value of that open statement.[/color]
                  >
                  > adding to or die in there doesn't change anything.
                  >
                  > I Just did some experimenting and noticed something odd. . . if I
                  > change
                  >
                  > open CARTFILE, ">$conf::orderd ir/$cartFileName" or die $!;
                  > #the file should already exist at this point
                  >
                  > to
                  >
                  > open CARTFILE, ">>$conf::order dir/$cartFileName" or die $!;
                  >
                  > Then the first line of the file is exactly like I want it to be,
                  > but it also prints a second line that doesn't have any values, just
                  > the " - - $"
                  >
                  > So what does that tell you?[/color]

                  Not much. What happens if you hardcode the file name instead of
                  relying on the cookie value?

                  --
                  Gunnar Hjalmarsson
                  Email: http://www.gunnar.cc/cgi-bin/contact.pl

                  Comment

                  • Chris W

                    #10
                    Re: Bizar variable problem

                    Gunnar Hjalmarsson wrote:
                    [color=blue]
                    > Not much. What happens if you hardcode the file name instead of
                    > relying on the cookie value?[/color]

                    Same thing. I decied to look in my access long and found these
                    interesting entries.

                    When I run the script from IE or Opera or Netscape 7.1 I get this. . .

                    "POST /cgi-bin/addToCart.pl HTTP/1.1" 200 6473
                    "GET /cgi-bin/addToCart.pl? HTTP/1.1" 200 6417



                    When I use netscape 4.7 I get this in the long

                    "POST /cgi-bin/addToCart.pl HTTP/1.0" 200 6521
                    "GET /cgi-bin/? HTTP/1.0" 403 286

                    That has to have something to do with it

                    --
                    Chris W

                    Comment

                    • Alex Zeng

                      #11
                      Re: Bizar variable problem

                      Instead of

                      print CARTFILE "...";

                      You should try

                      printf CARFILE "....";

                      printf prints content to a file.

                      "Chris W" <chrisw3@cox.ne t> wrote in message
                      news:3FDB64EF.C 5CED0F@cox.net. ..[color=blue]
                      > Ok here is the code.
                      >
                      > #!c:\perl\bin\p erl.exe
                      > use CGI qw(:standard);
                      > use CGI::Carp qw/fatalsToBrowser/;
                      > require 'config.pl';
                      >
                      > ############### ###############
                      > ###Variables in question######
                      > $item = param("item");
                      > $qty = param("qty");
                      > $price = param("price");
                      > ############### ###############
                      >
                      > $cartFileName = CGI::cookie("Ca rtFile");
                      > $total = $qty * $price;
                      > $shipping = "\$3.00";
                      > $gtotalstr = sprintf "%9.2f", ($total + 3 + ($total * $tax));
                      > $taxstr = sprintf "%9.2f", ($total * $tax);
                      > $totalstr = sprintf "%9.2f", $total;
                      >
                      > ############### ############### ############### ############### ######
                      > #########I have moved this code to various places and still no joy
                      > ############### ############### ############### ############### ######
                      > open CARTFILE, ">$orderdir/$cartFileName";
                      > print CARTFILE "$item - $qty - \$$price\n"; #THIS IS WHAT DOESN'T
                      > WORK.
                      > ######the two "-"s and the "$" are all that's in the file.
                      > close CARTFILE;
                      >
                      > print CGI::header();
                      > open TEMPLET, "<$cgidir/cart.html";
                      > while(<TEMPLET> ){
                      > print;
                      > if(/BODY GOES HERE/){
                      >
                      > ############### ############### ############
                      > #######This all prints as expected####### #
                      > print "<br><br>$i tem - $qty - \$$price\n<BR>< BR>";
                      >
                      > print <<"[END]";
                      >
                      > Sub Total:\$$totals tr<br>
                      > Tax :\$$taxstr<br>
                      > Shipping :$shipping<br>
                      > Total :\$$gtotalstr<b r>
                      > <FORM ACTION="$hcgidi r/GetPayment.pl" METHOD=POST>
                      > <INPUT TYPE="HIDDEN" NAME="total" VALUE="$total">
                      > Please select payment method.<br>
                      > <input type="radio" name="paymentTy pe" value="CreditCa rd">Credit
                      > Card
                      > <input type="radio" name="paymentTy pe" value="Check">M ail Check
                      > <INPUT TYPE="submit" name="submit" value="Continue ">
                      > </FORM>
                      > <br>
                      > </form>
                      >
                      > [END]
                      >
                      > }#end if
                      > } #end while templet
                      >
                      >
                      >
                      > --
                      > Chris W[/color]


                      Comment

                      • Jürgen Exner

                        #12
                        Re: Bizar variable problem

                        Alex Zeng wrote:[color=blue]
                        > Instead of
                        >
                        > print CARTFILE "...";
                        >
                        > You should try
                        >
                        > printf CARFILE "....";[/color]

                        Why do you think a formatted print without a format would solve any problem?
                        [color=blue]
                        > printf prints content to a file.[/color]

                        True. Just as print() does.
                        Actually neither print nor printf care much about where CARFILE is pointing
                        to. It could be a file, could be printer, could be a port, could be a
                        monitor, could be anything else.

                        jue


                        Comment

                        • Alex Zeng

                          #13
                          Re: Bizar variable problem

                          My Bad, I got confused with C/Perl. In C, printf is used to print to file


                          Comment

                          • Alex Zeng

                            #14
                            Re: Bizar variable problem

                            I saw the script was running on Windows platform, could that be because of
                            the pathname of a file is different from Linux? Like

                            $order_dir/$cart_file_name

                            shall really be:

                            $order_dir\\$ca rt_file_name ???

                            Print out the filename, and see if it follows the MS Window file naming
                            convention.

                            "Chris W" <chrisw3@cox.ne t> wrote in message
                            news:3FDB64EF.C 5CED0F@cox.net. ..[color=blue]
                            > Ok here is the code.
                            >
                            > #!c:\perl\bin\p erl.exe
                            > use CGI qw(:standard);
                            > use CGI::Carp qw/fatalsToBrowser/;
                            > require 'config.pl';
                            >
                            > ############### ###############
                            > ###Variables in question######
                            > $item = param("item");
                            > $qty = param("qty");
                            > $price = param("price");
                            > ############### ###############
                            >
                            > $cartFileName = CGI::cookie("Ca rtFile");
                            > $total = $qty * $price;
                            > $shipping = "\$3.00";
                            > $gtotalstr = sprintf "%9.2f", ($total + 3 + ($total * $tax));
                            > $taxstr = sprintf "%9.2f", ($total * $tax);
                            > $totalstr = sprintf "%9.2f", $total;
                            >
                            > ############### ############### ############### ############### ######
                            > #########I have moved this code to various places and still no joy
                            > ############### ############### ############### ############### ######
                            > open CARTFILE, ">$orderdir/$cartFileName";
                            > print CARTFILE "$item - $qty - \$$price\n"; #THIS IS WHAT DOESN'T
                            > WORK.
                            > ######the two "-"s and the "$" are all that's in the file.
                            > close CARTFILE;
                            >
                            > print CGI::header();
                            > open TEMPLET, "<$cgidir/cart.html";
                            > while(<TEMPLET> ){
                            > print;
                            > if(/BODY GOES HERE/){
                            >
                            > ############### ############### ############
                            > #######This all prints as expected####### #
                            > print "<br><br>$i tem - $qty - \$$price\n<BR>< BR>";
                            >
                            > print <<"[END]";
                            >
                            > Sub Total:\$$totals tr<br>
                            > Tax :\$$taxstr<br>
                            > Shipping :$shipping<br>
                            > Total :\$$gtotalstr<b r>
                            > <FORM ACTION="$hcgidi r/GetPayment.pl" METHOD=POST>
                            > <INPUT TYPE="HIDDEN" NAME="total" VALUE="$total">
                            > Please select payment method.<br>
                            > <input type="radio" name="paymentTy pe" value="CreditCa rd">Credit
                            > Card
                            > <input type="radio" name="paymentTy pe" value="Check">M ail Check
                            > <INPUT TYPE="submit" name="submit" value="Continue ">
                            > </FORM>
                            > <br>
                            > </form>
                            >
                            > [END]
                            >
                            > }#end if
                            > } #end while templet
                            >
                            >
                            >
                            > --
                            > Chris W[/color]


                            Comment

                            • Gunnar Hjalmarsson

                              #15
                              Re: Bizar variable problem

                              Alex Zeng wrote:[color=blue]
                              > I saw the script was running on Windows platform, could that be
                              > because of the pathname of a file is different from Linux? Like
                              >
                              > $order_dir/$cart_file_name
                              >
                              > shall really be:
                              >
                              > $order_dir\\$ca rt_file_name ???
                              >
                              > Print out the filename, and see if it follows the MS Window file
                              > naming convention.[/color]

                              There is no such need. '/' can well (and should better) be used as the
                              directory separator when writing paths in Perl, whether the program
                              shall be run on Unix/Linux or Windows.

                              --
                              Gunnar Hjalmarsson
                              Email: http://www.gunnar.cc/cgi-bin/contact.pl

                              Comment

                              Working...