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:
require Module1
(from package main
)
require Module2
(from package Module 1
)
require Module1
(does nothing because Module1
is already in %INC
)
Module1->import
(into package Module2
)
Module2->import
(into package Module1
)
Module1->import
(into package main
)