开发者

importing 2 modules with use

开发者 https://www.devze.com 2022-12-25 22:22 出处:网络
I have 2 perl modules every module use the second one i.e Module1.pm use Module2 Module2.pm use Module1 what happen on the background when I load those开发者_Python百科 2 modules with use

I have 2 perl modules every module use the second one i.e Module1.pm

use Module2

Module2.pm

use Module1

what happen on the background when I load those开发者_Python百科 2 modules with use use Module1; use Module2;

could someone explain what happen on the background and why I not enter infinate loop ?


You don't fall into an infinite loop because of the special hash %INC:

%INC
The hash %INC contains entries for each filename included via the do, require, or use operators. The key is the filename you specified (with module names converted to pathnames), and the value is the location of the file found. The require operator uses this hash to determine whether a particular file has already been included.

Also, remember that use Module LIST is equivalent to

BEGIN { require Module; Module->import( LIST ); }

So when the main program uses Module1, the following sequence happens:

  1. require Module1 (from package main)
  2. require Module2 (from package Module 1)
  3. require Module1 (does nothing because Module1 is already in %INC)
  4. Module1->import (into package Module2)
  5. Module2->import (into package Module1)
  6. Module1->import (into package main)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号