daxim has asked for the wisdom of the Perl Monks concerning the following question:

Is the misbehaviour shown in program "bar" below a known bug? What's the work-around?

I am not able to step through with the debugger, alas demoting the statements to run-time loading/importing makes the bug disappear.

$ cat foo #!/usr/bin/env perl5.10.1 use utf8; use strict; use CGI::Carp qw(fatalsToBrowser); $c; $ ./foo # ok Status: 500 Content-type: text/html <h1>Software error:</h1> <pre>Global symbol &quot;$c&quot; requires explicit package name at ./ +foo line 5. Execution of ./foo aborted due to compilation errors. </pre> <p> For help, please send mail to this site's webmaster, giving this error + message and the time and date of the error. </p> [Mon Jan 24 xx:xx:xx 2011] foo: Global symbol "$c" requires explicit p +ackage name at ./foo line 5. [Mon Jan 24 xx:xx:xx 2011] foo: Execution of ./foo aborted due to comp +ilation errors. $ cat bar #!/usr/bin/env perl5.12.1 use utf8; use strict; use CGI::Carp qw(fatalsToBrowser); $c; $ ./bar # nok BEGIN not safe after errors--compilation aborted at /usr/lib/perl5/5.1 +2.1/Carp.pm line 104. $ cat quux #!/usr/bin/env perl5.12.1 # use utf8; use strict; use CGI::Carp qw(fatalsToBrowser); $c; $ ./quux # ok Status: 500 Content-type: text/html <h1>Software error:</h1> <pre>Global symbol &quot;$c&quot; requires explicit package name at ./ +quux line 5. Execution of ./quux aborted due to compilation errors. </pre> <p> For help, please send mail to this site's webmaster, giving this error + message and the time and date of the error. </p> [Mon Jan 24 xx:xx:xx 2011] quux: Global symbol "$c" requires explicit +package name at ./quux line 5. [Mon Jan 24 xx:xx:xx 2011] quux: Execution of ./quux aborted due to co +mpilation errors.

Replies are listed 'Best First'.
Re: utf8 whacks Carp in 5.12.1
by Anonyrnous Monk (Hermit) on Jan 25, 2011 at 05:22 UTC

    I can reproduce it with 5.12.2, too.  Definitely a bug, I'd say.

    FWIW, the same problem exists with

    #!/usr/local/bin/perl5.12.2 use strict; use diagnostics; use utf8; $c;

    As soon as "use utf8" is in the game, the output is

    BEGIN not safe after errors--compilation aborted at /usr/local/lib/per +l5/5.12.2/diagnostics.pm line 569.

    while if you comment it out, you get the expected "Global symbol "$c" requires explicit package name", followed by the corresponding diagnostic message.

    The order of the use statements doesn't seem to matter, and use utf8 without use diagnostics doesn't produce the error.  The code works fine with 5.10.1.

      Yep, the problem is with the matching done inside of diagnostics.pm at the reported line. If before this line one adds

      utf8::encode($_) if utf8::is_utf8($_);
      on line 552 of the diagnostics.pm then the problem goes away. Well, actually this problem goes away, instead the Carp.pm fails, again with regex matching.
      Global symbol "$c" requires explicit package name at test.pl line 8. Execution of test.pl aborted due to compilation errors (#1) (F) You've said "use strict" or "use strict vars", which indicates + that all variables must either be lexically scoped (using "my" or +"state"), declared beforehand using "our", or explicitly qualified to say which package the global variable is in (using "::"). BEGIN not safe after errors--compilation aborted at /usr/lib64/perl5/5 +.12.2/Carp.pm line 113.
      Really weird problem. Things work fine for substitute but fail for regular matching.

      Looks like this bug happens to be only in $SIG{__DIE__} handler. At least I could reproduce it in

      use strict; use utf8; BEGIN { $SIG{__DIE__} = \&report; sub report { my $arg = shift; $arg = "'$arg'" unless $arg =~ /^-?[\d.]+\z/; warn("Done\n"); } } $c;