5

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

Community
  • 1
  • 1
kobame
  • 5,766
  • 3
  • 31
  • 62
  • [`use Package`](http://perldoc.perl.org/functions/use.html) is the same as `BEGIN{ require Package; Package->import() }` – Brad Gilbert Jun 17 '12 at 15:47
  • Is there no hope for installing an upgraded Perl, perhaps with perlbrew? Some modern features weren't available in older Perl versions (`//=`, for example). Plus all the nice new bug fixes. – DavidO Jun 17 '12 at 15:59
  • Trying to just patch your pragmas into an existing application won't go well. That being said: 100+ modules isn't really that big. 1000+ is. –  Jun 17 '12 at 16:39
  • @duskwuff - as i said, that was the 1st step - not the only step. But it *is* a good practice having own "defaults". – kobame Jun 17 '12 at 17:27
  • @DavidO - sure it will run in upgraded perl - defining a common "defaults" for all modules only helps later. – kobame Jun 17 '12 at 17:28

1 Answers1

3

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.
ysth
  • 96,171
  • 6
  • 121
  • 214