|Think about Loose Coupling|
import() magicby ferrency (Deacon)
|on May 29, 2002 at 19:23 UTC||Need Help??|
ferrency has asked for the wisdom of the Perl Monks concerning the following question:
Please excuse me if this has been answered elsewhere.
What nature of magic causes this code...
...to output the following:
According to all the perldoc I can find, import isn't special: it's just a class method of your class, which may or may not exist. And "use" doesn't care if it doesn't exist. (I notice that the pod at perlmonks.com says use doesn't raise an error if import doesn't exist, but my pod says use doesn't call import if it doesn't exist.)
However, the test above demonstrates that independant of use, trying to call method import which doesn't exist is handled differently than with almost any other method ("almost" only because I'm not 100% sure there is no other similar case... I don't know of any offhand, however).
I'd expect the above code to bomb on the $x->import line with an error message similar to the one shown for the second method call. Instead, it creates an import method in package Foo. Not only that, but the method does nothing instead of bombing because the things I'm trying to import don't exist.
Not only that, but use strict and warnings don't have anything to say on the matter either.
I have no problem with the fact that there's magic here, but I'd like to know what the magic is and where it's documented, to train myself that this is actually predictable :)