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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.