`use` package scope: how to make it cross files?

前端 未结 3 1476
-上瘾入骨i
-上瘾入骨i 2021-01-13 06:06

In scriptA.pl, there is use DBI

In scriptB.pl, there is require \"scriptA.pl\"

But we still cannot use DB

3条回答
  •  爱一瞬间的悲伤
    2021-01-13 06:28

    I find myself wondering what it is you are attempting to achieve?

    If you simply want to reduce boilerplate code (15 common use Foo declarations in every file), you can use a module like ToolKit to create a standard set of modules to use:

    Put this into Powerboy.pm:

     package Powerboy;
    
     use base 'ToolSet'; 
    
     ToolSet->use_pragma( 'strict' );
     ToolSet->use_pragma( 'warnings' );
    
     ToolSet->export(
         'DBI'  => undef,  # Export the default set of symbols
     );
    
     1; 
    

    And then in your scripts, simply do:

     use Powerboy;
    
     # We have strict, warnings and DBI now.
    

提交回复
热议问题