Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Help with Metaclasses

by Boots111 (Hermit)
on Oct 19, 2004 at 04:23 UTC ( [id://400405]=perlquestion: print w/replies, xml ) Need Help??

Boots111 has asked for the wisdom of the Perl Monks concerning the following question:

All~

I suppose this is not actively perl specific, but it definitely can apply to perl... I have seen a fair amount about metaclasses and tried to understand them through reading articles like this and this, and I can usually follow them to a degree. However, when the examples stop being simple, I have trouble understanding exactly what is going on (and in some cases it feels like the syntax is actively getting in the way). Does any one here know of a good article on metaclasses that bridges the gap between beginner usage and more advanced? Perhaps a side by side comparison of a problem being solved with and without metaclasses.

I also worry a little about being unable to avoid hanging myself with the amount of rope that metaclasses seem to provide. Perhaps a general idea of the sorts of things they are best used for would be helpful to me.

Thanks,
Boots
---
Computer science is merely the post-Turing decline of formal systems theory.
--???

Replies are listed 'Best First'.
•Re: Help with Metaclasses
by merlyn (Sage) on Oct 19, 2004 at 05:16 UTC
    As you investigate, perhaps by googling for "metaclass", beware that the Python and Perl usage of the term is starkly different from the Smalltalk meaning.

    In Smalltalk, the Metaclass is an instance of the Metaclass class that holds the methods used by the specific Class (of which it is the sole instance of the Metaclass), which in turn holds the methods for a specific instance. Got that? {grin}

    For instance (ugh), (and simplifying a bit), you create a new Array by sending "new" to the Array class, which invokes the method whose definition is found (by inheritance) in the method dictionary of the metaclass of Array. Once you have a specific Array, methods sent to it are looked up in the dictionary held in Array (and its parent classes).

    So, in Smalltalk, a Metaclass provides behaviors for a Class, and a Class provides behaviors for an instance. (Do I dare bring up that both Class and Metaclass are themselves classes, so they both have classes and metaclasses that are instances of themselves and each other? Ouch!)

    By contrast, Perl has only a symbol table (package) that provides behaviors for both classes and instances, distinguished by the first parameter (class methods have the class name, instance methods have the instance reference). The term "metaclass" (which I've not heard much in Perl) as you reference it seems to refer to "metaprogramming" instead: creating code that creates code.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

Re: Help with Metaclasses
by kvale (Monsignor) on Oct 19, 2004 at 04:43 UTC
    Programming with metaclasses does seem a bit scary; just how much indirection do we need in a program?

    Normally, an essential part of software development is class design and those classes are usually fixed for the duration of the program. By contrast, metaclasses create classes dynamically. A common use of metaclasses is to provide optional 'aspects' to basic classes, such as logging and debugging.

    I tend to stay away from metaclasses because it is harder to debug such programs. I tend to instead derive subclasses that contain the extra functionality needed so that I have multiple concrete classes: with debugging, without debugging, etc. It is more source code, but the class members are explicit and easy to see.

    Then again, I may be a metaclass Luddite :)

    -Mark

Re: Help with Metaclasses
by Arunbear (Prior) on Oct 19, 2004 at 13:21 UTC
    You should also read Guido van Rossum's paper for the nuts and bolts of Python metaclasses. For a real world usage of metaclasses, look at SQLObject - an object-relational mapping tool for Python.

    As others have pointed out, Perl does not currently have an equivalent construct; for something like that you will have to wait for Perl 6.

Re: Help with Metaclasses
by Zaxo (Archbishop) on Oct 19, 2004 at 04:43 UTC

    For perl, take a look at Class::Struct and Class::MethodMaker. (Added) See also Class::MakeMethods.

    There is a section of perltoot titled "Metaclassical Tools". It covers Class::Struct including a complete example application.

    In C++ genericity is covered by templates. In perl, that sort of thing is far less necessary because of no static typing.

    After Compline,
    Zaxo

      Don't think this is the same thing what the OP is talking about.

      To be frank, Perl does not provide the metaclass as provided in Python. Class:Struct is nothing more than a way to define a c-alike struct, which Perl lacks of (and I doubt we really need, with OO there. With OO, even c++ no longer needs struct as c did).

      "In C++ genericity is covered by templates. In perl, that sort of thing is far less necessary because of no static typing."

      That is again nothing close to Python's metaclass. The template concept in C++, in simply a patch to the language, so that it can provide the powerful STL, to match up with java's utility classes.

      In Java, it has whole bunch of utility classes, like Vector, Hashtable..., their elements are all simply declared as Object, which is everything and anything. C++ does not have this Object class, so it came up this template concept, which allows you to define things like LinkedList just once for whatever kind of element, and just resolve it at compilation time.

      However it is still not the same as in Java. Once you say something like LinkedList<int>, all the elements in that linked list are now nothing else but int. But in Java, it is not the same case, a Java Hashtable can contain anything, and element 0 can be of class BLAH, when element 1 might be of class YAHOO.

      A Perl hash or array has the similar nature as java's hahtable etc., however because of different reasons: in java, it is becasue of the Object base class; in Perl, it is not because of the UNIVERSAL base class, but becasue Perl is not a "strong type" language.

        The template concept in C++, in simply a patch to the language, so that it can provide the powerful STL, to match up with java's utility classes.

        Ahem. Templates were proposed for C++ in 1990, STL was developed in 1993, and Java was publicly announced by Sun in 1995.

        Java's classes like Vector are a type-unsafe, limited set of containers (for example, they can contain Object, but not int). I suspect they were a hack that came about because Java lacked templates. Now Java 5.0 has a version of templated containers at last.

        Back on the subject of perl metaprogramming, it could be done if desired. You can create code templates to be evaled at runtime. The Template toolkits are sometimes used this way. Also Inline::C and friends can generate wrapper code at runtime and call it.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://400405]
Approved by kvale
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-03-28 15:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found