I recently wanted to load another version of a module from a module of the same name. i.e. I wanted to load "../ModX.pm" instead of "./ModX.pm" (changing the load path wasn't an option at the time).
I tried this (in ./ModX.pm):
but this just kept the loaded version (./ModX.pm) loaded. After trying a few things, I found this worked:BEGIN { delete $INC{'ModX.pm'}; use lib '../'; use ModX; }
Which I found odd, because perldoc -f use says that use is exactly equivalent to: BEGIN { require Module; import Module LIST; } To some extent I was happy, but I was still curious.BEGIN { delete $INC{'ModX.pm'}; use lib '../'; require '../ModX'; # Yes, I know I haven't done import }
Some more playing around and I reaslised my error (which is probably obvious to many, but not to some, hence this post).
use is exactly equivalent to the above, and the issue is the nested BEGIN blocks. The effective code being run (ignoring imports) is:
and so...the inner BEGIN block executes before the contents of the enclosing block, so you get the effect of something like:BEGIN { delete $INC{'ModX.pm'}; use lib '../'; BEGIN { require 'ModX.pm'; } }
where the require does nothing (because we have not yet frobbed %INC).BEGIN { require 'ModX.pm'; delete $INC{'ModX.pm'}; use lib '../'; }
I thought this interesting. My error was in thinking of 'compile time' versus 'run time'. Everything I was doing was at 'compile time' so I wasn't thinking in terms of that.
Does anyone have anything to offer regarding:
In reply to BEGIN and compile-time by jbert
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |