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

Greetings all,
Lately I have been diving into OOP with Perl and have read the perlboot and perltoot tutorials. These have already helped my understanding tremendously as well as my latest project designs. Put simply I am enamored with Perl Objects right now.
In an attempt to learn as much as possible I have read over the perltooc tutorial and was hoping someone could help me understand a few points that are tripping me up.
Namely the following examples from perltooc

First Question:
package Some_Class; use strict; our %ClassData = ( # our() is new to perl5.6 CData1 => "", CData2 => "", ); for my $datum (keys %ClassData) { no strict "refs"; # to register new methods in package *$datum = sub { shift; # XXX: ignore calling class/object $ClassData{$datum} = shift if @_; return $ClassData{$datum}; } }

Though I feel I understand what this code is doing, registering accessor methods for each element of %ClassData, the *$datam = sub{#code} part is new to me. This is a data-type glob correct?
Does using this and registering it equal to a sub only populate the sub slot in the symbol table for *$datum? As I understood/understand it *$datum would have four slots so to speak in the symbol table ($$datum, @{$datum}, %{$datum} and &{$datum}). Does the above code only fill the appropriate type? My gut feeling is yes but having never seen something like this before I thought I would ask and make sure.

Next Question:
In the example code below
package Some_Class; use strict; our %Some_Class = ( # our() is new to perl5.6 CData1 => "", CData2 => "", ); # tri-natured: function, class method, or object method sub _classobj { my $obclass = shift || __PACKAGE__; my $class = ref($obclass) || $obclass; no strict "refs"; # to convert sym ref to real one return \%$class; } for my $datum (keys %{ _classobj() } ) { # turn off strict refs so that we can # register a method in the symbol table no strict "refs"; *$datum = sub { use strict "refs"; my $self = shift->_classobj(); $self->{$datum} = shift if @_; return $self->{$datum}; } }

how is the line my $self = shift->_classobj(); getting data? and what data is it getting? Is it getting $objclass from _classobj()'s @_ array or is it getting the first key from the \%$class that is returned from _classobj() or something else entirely?
Given the line $self->{$datum} = shift if @_; Im lead to think that its returning a reference to %Some_Class but I am having trouble seeing how shift is doing that?
Thank you all in advance.

-InjunJoel

Replies are listed 'Best First'.
Re: Help understanding perltooc examples?
by bart (Canon) on Feb 19, 2005 at 23:17 UTC
    the *$datam = sub{#code} part is new to me. This is a data-type glob correct?

    Actually, this question has nothing to do with OO.

    Yes, you are assigning to what is called a "typeglob", which is sort of a record of all datatypes associated with the name of a global variable (thus $a, @a, %a, ... — all globals, of course, lexicals are different) and yes, that part is magical in that only the slot in the typeglob is affected that agrees with the type of reference that you are assigning to it. In other words, if you assign a scalar reference, only the scalar part of the typeglob will be affected, and array, hash, filehandle... all will keep their old value.

    The best explanation that I recall having read, is in the O'Reilly book "Advanced Perl Programming", p.47. The author calls it "selective aliasing".

    Witness:

    @a = qw(a b c); $foo = "hello"; *a = \$foo; print <<"END"; array: @a scalar: $a END
    Result:
    array: a b c
    scalar: hello
    

    Not only that, but it's also a way to create variable's aliases:

    $foo = "hello"; *a = \$foo; $a = "bye"; print "$foo\n";
    Result:
    bye
    
    So I changed the value of the alias $a, and the original variable $foo changes with it. Both $a and $foo actually refer to the same variable internally.

    That would also work if $foo is a lexical (my) variable.

Re: Help understanding perltooc examples?
by dragonchild (Archbishop) on Feb 19, 2005 at 22:43 UTC
    (It actually has 7 slots, but that's not important.)

    Yes, it does create those methods. Each of those subroutines is a closure over $datum, so each one knows exactly what $datum it is dealing with.

    As for the shift->classobj(), that shift is affecting the @_ that came into the subroutine. So, if you were to say:

    my $x = $self->foo();
    where foo is some legal datum, what happens it the anonymous sub's @_ is populated with $self. That is shift'ed, then the _classobj() method is called on it and assigned to $self within the anonymous subroutine.
      Greetings,
      Ahhh! I see now!
      7 slots! wow... but it only populates for the &{$datum} slot so to speak. so $$datum, et al. are still usable.
      Thanks for the explanation on the shift->_classobj() call, it makes total sense now. Its acting on what shift is returning which just so happens to be the object instance doing the calling.
      Thanks again,

      -InjunJoel
      "I do not feel obliged to believe that the same God who endowed us with sense, reason and intellect has intended us to forego their use." -Galileo
Re: Help understanding perltooc examples?
by perlfan (Parson) on Feb 21, 2005 at 00:47 UTC
    Again, I must recommend Damian Conway's "Object Oriented Perl". The beginning chapters cover most of these things so well that they will most certainly help you "see the light" by putting together all of the knowledge you seem to have floating about in your head. I find it helpful when trying to decypher these things to remember that most anything can be or return a reference - even to another reference to another reference...etc. That combined with knowing Perl's syntactical sugar will allow you to see the big picture before long.