Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to determine which pragmas are active?

Tags:

perl

pragma

I'm wondering if there's a way to determine what pragmas are active within a specific scope.

perlvar suggests %^H but it doesn't seem to do anything useful in the following one-liner:

$ perl -e 'use strict; use warnings; use utf8; use Data::Printer; p %^H'
{}

I ask since it took me quite a while to figure out that an implicit utf8 pragma loaded by Mojolicious::Lite was creating trouble that my Regexp::Grammars tests had failed to capture, and I hope there's a better way to troubleshoot such problems.

like image 608
Zaid Avatar asked Dec 22 '25 15:12

Zaid


1 Answers

%^H is for making your own pragmas. strict, warnings and utf8 all set bits in $^H.

But changes to both $^H and %^H are restored at the end of the BEGIN block being executed. So you need to use caller to access their value at run-time.

$ perl -e'
   use Data::Printer;

   sub f {
      CORE::say sprintf "%X", (caller(0))[8];
      p %{ (caller(0))[10] // {} };
   }

   {
      use strict;
      use warnings;
      use utf8;
      f();
   }

   {
      f();
   }
'
8007E2
{}
0
{}
like image 150
ikegami Avatar answered Dec 24 '25 11:12

ikegami



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!