The correct way to add my own pragma to old perl sw

I have a really old perl system (about 8-10 years old) but big (files with more than 100 + pm). Now, for some reason, you need to "reconfigure" it - step by step.

One of the first things I want to do is insert my pragma into each module:

use MySw::PerlDefs; 

what will things like Modern :: Perl and / or like in this question contain: How to make "use My :: defaults" with modern perl and utf8 settings?

QST1: What is the recommended method?

  • by adding use MySw::PerlDefs; we get

      package MySw :: SomePackage;
     use MySw :: PerlDefs;  #my new "pragma"
    
  • or add PerlDefs enclosed in a BEGIN block after the package declaration? eg:.

      package MySw :: SomePackage;
      BEGIN {use MySw :: PerlDefs;} #my new "pragma" in the BEGIN block
    

Questions:

  • What is the preferred method?
  • What are the differences and / or disadvantages?

Ps: I understand how BEGIN is called at compile time, but in the above context, is this no better than simple use?

+6
source share
1 answer

BEGIN block bypass will not work; the effect of lexical pragmas does not go beyond the block.

For comparison:

 $ perl -e'BEGIN{ use Modern::Perl; } $x=42; print "$x\n"' 42 $ perl -e'use Modern::Perl; $x=42; print "$x\n"' Global symbol "$x" requires explicit package name at -e line 1. Global symbol "$x" requires explicit package name at -e line 1. Execution of -e aborted due to compilation errors. 
+3
source

Source: https://habr.com/ru/post/918283/


All Articles