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 thedo
,require
, oruse
operators. The key is the filename you specified (with module names converted to pathnames), and the value is the location of the file found. Therequire
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:
require Module1
(from packagemain
)require Module2
(from packageModule 1
)require Module1
(does nothing becauseModule1
is already in%INC
)Module1->import
(into packageModule2
)Module2->import
(into packageModule1
)Module1->import
(into packagemain
)
精彩评论