Hi all,

In putting together a CGI script, I get the very strange error, apparently from the BEGIN block:

String found where operator expected at test line 13, near "STDOUT "St +atus: 413 Request Entity Too Large\r\n"" (Do you need to predeclare STDOUT?) syntax error at test line 13, near "print" BEGIN not safe after errors--compilation aborted at test line 16.

What does the error BEGIN not safe mean?

Here is my code ...

#!/usr/bin/perl sub BEGIN { # Check for oversize request entity # my $post_maximum = 1047576; my $content_length = defined($ENV{'CONTENT_LENGTH'}) ? $ENV{'CONTE +NT_LENGTH'} : 0; if (($post_maximum > 0) && ($content_length > $post_maximum)) { print STDOUT ( $ENV{'SERVER_PROTOCOL'} || 'HTTP/1.0' ), " 413 +Request entity too large\r\n" print STDOUT "Status: 413 Request Entity Too Large\r\n"; exit 0; } } sub init { local $/ = "\n"; my $request_method = $ENV{'REQUEST_METHOD'} if defined $ENV{'REQUE +ST_METHOD'}; my $content_length = defined($ENV{'CONTENT_LENGTH'}) ? $ENV{'CONTE +NT_LENGTH'} : 0; my $query_string; METHOD: { # process multi-part POST method # if (($request_method =~ /^POST$/) && ($ENV{'CONTENT_TYPE'} =~ m!^multipart/form-data!)) { # On the TODO list! last METHOD; } # process GET or HEAD method # if ($request_method =~ /^(GET|HEAD)$/) { $query_string = $ENV{'QUERY_STRING'} if defined $ENV{'QUER +Y_STRING'}; $query_string ||= $ENV{'REDIRECT_QUERY_STRING'} if defined + $ENV{'REDIRECT_QUERY_STRING'}; last METHOD; } # process POST method # if ($request_method =~ /^POST$/) { if ($content_length > 0) { local $^W = 0; $query_string = read (STDIN, $query_string, $content_l +ength, 0); } $query_string .= (length($query_string) ? '&' : '') . $ENV +{'QUERY_STRING'} if $ENV{'QUERY_STRING'}; last METHOD; } print STDOUT ( $ENV{'SERVER_PROTOCOL'} || 'HTTP/1.0' ), " 405 +Method Not Allowed\r\n" print STDOUT "Status: 405 Method Not Allowed\r\n"; exit 0; } my $results = {}; if ((defined $query_string) && (length $query_string)) { if ($query_string =~ /[&=;]/) { my (@pairs) = split /[&;]/, $query_string; foreach (@pairs) { my ($param, $value) = split '=', $_, 2; $value ||= ''; $param = unescape($param); $value = unescape($value); push @{$results->{$param}}, $value; } } } return $results; } sub unescape { my ($todecode) = @_; return undef unless defined $todecode; $todecode =~ tr/+/ /; $todecode =~ s/%([\da-fA-F]{2})/chr hex($1)/ge; return $todecode; }

In reply to Strange Error From BEGIN by barou_

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.