ferrency has asked for the wisdom of the Perl Monks concerning the following question:
What nature of magic causes this code...
#!/usr/local/bin/perl -w use strict; use Data::Dumper; my $x = bless {}, 'Foo'; print Dumper(\%Foo::); $x->import(qw(:blah foo whatever 123)); print Dumper(\%Foo::); $x->almost_any_method_besides_import(qw(:blah foo whatever 123)); print Dumper(\%Foo::);
...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.)$VAR1 = {}; $VAR1 = { 'import' => *Foo::import }; Can't locate object method "almost_any_method_besides_import" via pack +age "Foo"\ at test.pl line 9.
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 :)
Thanks,
Alan
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: import() magic
by Ovid (Cardinal) on May 29, 2002 at 20:00 UTC | |
|
Re: import() magic
by broquaint (Abbot) on May 29, 2002 at 20:03 UTC | |
by Ovid (Cardinal) on May 29, 2002 at 20:21 UTC | |
by Anonymous Monk on May 29, 2002 at 21:57 UTC | |
|
Re: import() magic
by Anonymous Monk on May 29, 2002 at 21:58 UTC | |
by ehdonhon (Curate) on May 29, 2002 at 22:13 UTC | |
by Ovid (Cardinal) on May 29, 2002 at 22:44 UTC |