Home > Mobile >  Use of uninitialized value inside a print
Use of uninitialized value inside a print

Time:06-27

I have Perl code behind a web server, and I combined that Perl script with pure HTML. The important part looks like this:

#!/usr/local/bin/perl

use strict;
use warnings;

print "Content-type: text/html\n\n";
print <<ENDHTML;
<!DOCTYPE html>
<html lang="hu">
        <head>
    ...
        </head>
        <body >
        ...
                <section >
                <h3 >some title</h3>
        <p>some text</p>
        </section>
        ...
    </body>
    </html>
ENDHTML

When somebody opens the web page, this error message will appear in logs:

2022/06/27 13:28:44 [error] 7811#100158: *106 FastCGI sent in stderr: "Use of uninitialized value $12 in concatenation (.) or string at /path/to/file/index.pl line 78.

I know this is because of use warnings;, and if I disable it, then nothing will appear in the log. But, it would be nice if I could ignore the part of script from print <<ENDHTML; to ENDHTML because they are part of the HTML code. Is there a way to make this happen and I can also use warnings;?

CodePudding user response:

Your here-doc is interpolating variables within the string because you used a bare ENDHTML without explicit quotes. This is the same as using double quotes: ".

If you use single quotes, you will avoid variable interpolation, and this will eliminate the warning message. Change:

print <<ENDHTML;

to:

print <<'ENDHTML';

You can retain use warnings;. Keep in mind that if your here-doc does have variables which you haven't shown, they will not be interpolated either.

Refer to quote-like operators (search for EOF)

CodePudding user response:

It's the dollar signs in your heredoc string. Perl thinks these are variables.

<!--                                      XXXX        -->
                <section >

You need to escape them, like this:

                <section >

Although I wonder what kind of HTML that is. You can't have dollar signs and parentheses as part of class names.

More details on $12:

Perl ignores the space after the $, because a dollar sign can't stand on its own in Perl syntax, and treats the numbers 12 after it as a variable name. $12 would refer to the 12th capture group of a regular expression match. Variable names that start with numbers cannot have letters afterwards, so it's $12 and not $12u.

  • Related