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

Fellow monks, dear pirates,

as announced (Parrot Monks?) some time back here is the first of what could become a series of Parrot questions, depending on the feedback it generates in the Monastry. The idea is both to help myself get a grip on some basic Parrot concepts, and to, as Ovid put it, "expose more monks to the future".

Is there already a class loader in parrot, that keeps track of what files are loaded? In other words, can I use use in Parrot?

I am now using the load_bytecode opcode, but of course, if I accidently load the same file/class/module twice, its initialization code (supposed to be run only once) is run twice, resulting in an error and program crash.

main program file
.sub _main @MAIN print "starting\n" # load_bytecode is actually clever enough to load # the (uncompiled) imc file as well (cool) load_bytecode "MyClass.imc" # but loading it twice is bad load_bytecode "MyClass.imc" print "finished\n" .end
MyClass.imc file
.namespace ['MyClass'] .sub _initialize prototyped, @LOAD .local pmc tmpl_class print "registering class\n" newclass tmpl_class, 'MyClass' .end
Output:
starting
registering class
registering class
Class MyClass already registered!
I am using the 0.1.1 release of Parrot.

Thanks for your time.

Replies are listed 'Best First'.
Re: [Parrot] Is there a class loader?
by rg0now (Chaplain) on Feb 17, 2005 at 12:09 UTC
    After rereading PPD15, the only thing I can think of is that Parrot does not force the semantics of Perl-style "modules are packages are classes" (or something along these lines). It will leave the registration of the files being loaded to the actual language, if the language happens to want to implement a feature like that.

    So there is a nice class-loader in Parrot (since you could not instantiate two classes), but classes are not automatically associated with files. Hope this makes sense...

    rg0now

      > the semantics of Perl-style "modules are packages are classes"

      What Perl are you thinking of that has these semantics? In Perl 5 at least, modules are not packages are not classes (are not files)! They can be, of course, but that is by "the typical" convention, and is not close to being enforced like it is in, say, Java. This example breaks all those stipulations:

      # client code use NiftySuperPack; whooo("hey"); # exported function my $obj = Nifty->new(); # where did that constructer come from?

      # NiftySuperPack.pm # There *is* no package NiftySuperPack! package Nifty::Utils; sub whooo { ... } package Nifty; sub new { ... } { no strict; *{caller() . "::whooo"} = \&Nifty::Utils::whooo; # forced export }
        > What Perl are you thinking of that has these semantics? In Perl 5 at least, modules are not packages are not classes (are not files)!

        This is perfectly why I wrote: "modules are packages are classes" or something along these lines...:-)

        What I wanted to paraphrase is that there is nothing in Parrot that would enforce the rather exotic object model of Perl5. So, as far as I can tell you, Parrot does not implement an equivalent of %INC, which the OP objected. It is up to the language running on top of Parrot to implement something similar.

        By the way, a nice use of the object system of Parrot can be found in the precious SDL bindings of Parrot. The code lives in $PARROT_ROOT/runtime/parrot/library/SDL.

        rg0now

      It will leave the registration of the files being loaded to the actual language, if the language happens to want to implement a feature like that.

      I think one goal of Parrot is to allow using code written in many different languages (Perl5, Perl6, Python, Ruby) simultaneously. This seems to imply that classes created in these languages are interoperable (by adhering to the standards layed down in PDD15), and that Parrot should provide the mechanism to load and register them.

      Thus, I am assuming that "keeping track of %INC" will eventually be handled by Parrot, but how exactly is not yet specified. Is that it?