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

前端 未结 3 1506
-上瘾入骨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:39

    The scoped nature of use is a documented feature:

    use Module

    Imports some semantics into the current package from the named module, generally by aliasing certain subroutine or variable names into your package.

    You could do what you want by going back to the stone age as in the following example, but please don't.

    Using MyModule as a stand-in for DBI:

    package MyModule;
    
    use Exporter 'import';
    our @EXPORT = qw/ foo /;
    sub foo { print "$_[0]!\n" }
    
    1;
    

    and then calling MyModule::foo from scriptA.pl

    foo "from scriptA";
    

    and from scriptB.pl

    foo "from scriptB";
    

    all kicked off from a main program of

    #! /usr/bin/perl
    
    use warnings;
    use strict;
    
    use MyModule;
    
    do "scriptA.pl" or die;
    do "scriptB.pl" or die;
    
    print "done.\n";
    

    gives the following output:

    from scriptA!
    from scriptB!
    done.

    You also could disable the scoping safety-feature with nasty eval games, but please don't do that either.

    If your design needs improvement—maybe scriptA and scriptB belong in the same package—that would be a far better investment of your time. Otherwise, bite the bullet and expend nine keystrokes.

    Please note that executing Perl libraries at runtime via do or require are a seriously dated approaches. The perlmod documentation describes the modern approach.

提交回复
热议问题