Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The correct way adding my own pragma to old perl sw

Tags:

perl

I got an really old perl system (approx 8-10 years old), but a big one (100+ pm files). Now for some reason need "remodernize" it - step-by-step.

One of first thing what i want get done is insert into every module my pragma:

use MySw::PerlDefs;

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

QST1: What is the recommended way?

  1. adding use MySw::PerlDefs; so will get

    package MySw::SomePackage;
    use MySw::PerlDefs;         #my new "pragma"
    
  2. or add the PerlDefs enclosed in the BEGIN block after the package declaration? e.g.:

     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 drawbacks?

Ps: I understand than the BEGIN exectuted at compile time, but in the above context - it is not better than the "simple use"?

like image 987
kobame Avatar asked Jun 17 '12 14:06

kobame


1 Answers

Wrapping the use in a BEGIN block is not going to work; the effect of lexical pragmas will not extend beyond the end of the block.

Compare:

$ 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.
like image 134
ysth Avatar answered Nov 12 '22 18:11

ysth