Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Please excuse me if this has been answered elsewhere.

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:

$VAR1 = {}; $VAR1 = { 'import' => *Foo::import }; Can't locate object method "almost_any_method_besides_import" via pack +age "Foo"\ at test.pl line 9.
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 :)

Thanks,

Alan


In reply to import() magic by ferrency

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2024-04-19 03:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found