This is a typical error, see Server error, see CGI Help Guide , Troubleshooting Perl CGI scripts, CGI::Carp, DebugCGI , CGI::Carp::Fatals
The checklists work, but here you go , first try
#!/usr/bin/perl --
use strict;
use warnings;
use CGI ();
use CGI::Carp qw(fatalsToBrowser);
use JSON ();
my $q = CGI->new;
my $data = $q->param('POSTDATA') ;
#~ my $data = $q->param('POSTDATA') || '{"a":100,"b":300}';
my $json = JSON->new->utf8;
my $input = $json->decode( $data );
my $a = $input->{a};
my $b = $input->{b};
my $c = $a + $b;
print $q->header("application/json");
print $json->encode({ c => $c });
then try #!/usr/bin/perl --
use strict;
use warnings;
use CGI ();
use CGI::Carp qw(fatalsToBrowser);
use JSON ();
Main( @ARGV );
exit( 0 );
sub Main {
binmode STDOUT;
my $q = CGI->new;
my $cb = AddJsonGetBytes( $q );
print $q->header("application/json"), $cb;
return;
}
sub AddJsonGetBytes {
my( $q ) = @_;
my $data = $q->param('POSTDATA') ;
my $json = JSON->new->utf8;
my $input = $json->decode( $data );
my $a = $input->{a};
my $b = $input->{b};
my $c = $a + $b;
return $json->encode({ c => $c });
}
then try #!/usr/bin/perl --
use strict;
use warnings;
use CGI ();
use CGI::Carp qw(fatalsToBrowser);
use JSON ();
Main( @ARGV );
exit( 0 );
sub Main {
binmode STDOUT;
my $q = CGI->new;
my $cb = AddJsonGetBytes( $q );
print $q->header("application/json"), $cb;
return;
}
sub AddJsonGetBytes {
my( $q ) = @_;
my $json = JSON->new->utf8;
my $data = $q->param('POSTDATA') ;
if( not defined $data ){
return $json->encode({ error => 'no valid input' });
}
my $input = $json->decode( $data );
my $a = $input->{a};
my $b = $input->{b};
my $c = $a + $b;
return $json->encode({ c => $c });
}
then try #!/usr/bin/perl --
use strict;
use warnings;
use CGI ();
use CGI::Carp qw(fatalsToBrowser);
use JSON ();
Main( @ARGV );
exit( 0 );
sub Main {
binmode STDOUT;
my $q = CGI->new;
my $json = JSON->new->utf8;
my $cb = AddJsonGetBytes( $q , $json );
print $q->header("application/json"), $cb;
return;
}
sub AddJsonGetBytes {
my( $q, $json ) = @_;
my $data = $q->param('POSTDATA') ;
if( not defined $data ){
return $json->encode({ error => 'no valid input' });
}
my $input = $json->decode( $data );
my $a = $input->{a};
my $b = $input->{b};
my $c = $a + $b;
return $json->encode({ c => $c });
}
:)
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|