End of script output before headers: multiplicar.cgi

1

I have been doing a simple example of a calculator using html and perl with the CGI module but I do not understand the error here is the HTML form

<form id="formu" action="multiplicar.cgi" method="POST" class="form register">
  <h2 class="form_titulo"> Calculadora </h2>  
     <div class="contenedor-inputs">
      <br>
      <input type="text" name="numero"  id="nombre" placeholder="Ingresa el número  " maxlength="2" class="input-100" required    />   
       <input type="text" name="numero2"  id="nombre" placeholder="Ingresa el número  " maxlength="2" class="input-100" required    />   
<input type="radio" name="opcion" value="sumar" /> Suma <br /> 
<input type="radio" name="opcion" value="restar" />Resta<br /> 
<input type="radio" name="opcion" value="multiplicar" />Multiplicación<br /> 
<input type="radio" name="opcion" value="dividir" />Division<br /> '
</td> 
   <input class="form-btn" type="submit" id="btnenviar" value="Ingresar"/>



     </div>

  </form>

And the perl code with the CGI module is as follows:

#!"C:\xampp\perl\bin\perl.exe"

use strict;
use CGI;

my $cgi  = new CGI;
my $num  = $cgi->param('numero');
my $num2 = $cgi->param('numero2');
my $op   = $cgi->param('opcion');

if ($op eq "sumar") {
    $total = $num+$num2;
}
elsif ($op eq "restar") {
    $total = $num-$num2;
}
elsif ($op eq "multiplicar") {
    $total = $num*$num2;
}
elsif ($op eq "dividir") {
    if ($num2 <= 0) {
        $total = "No se puede dividir entre 0";
    }
    else {
        $total = $num/$num2;
    }
}
else {
    $total = "opción inválida";
}

print $cgi->header("text/html");
print $total;
    
asked by Carlos Saz 24.09.2017 в 19:31
source

1 answer

1

The error message says that the web server is receiving text without first having received the mandatory headers.

In the program we see the call to header () correctly placed, so the problem must be somewhere else. Maybe, Perl is getting some error message.

If we pass the program with the option -c Perl will do a syntactic check:

perl -c multiplicar.cgi

If it does not come out, nothing, everything is fine, but in this case, this comes out:

Global symbol "$total" requires explicit package name at calculadora.pl line 12.
Global symbol "$total" requires explicit package name at calculadora.pl line 15.
Global symbol "$total" requires explicit package name at calculadora.pl line 18.
Global symbol "$total" requires explicit package name at calculadora.pl line 22.
Global symbol "$total" requires explicit package name at calculadora.pl line 25.
Global symbol "$total" requires explicit package name at calculadora.pl line 29.
Global symbol "$total" requires explicit package name at calculadora.pl line 33.

The module strict forces us to program strictly, so Perl will complain if we use a variable if we have not declared it before, which is just what is happening: the variable is not declared $ total .

The solution is to put this at the beginning of the program:

my $total;
    
answered by 25.09.2017 в 04:04