Beginning OO help



I'm creating my very first object oriented module. I've read about
objects in so many perl books and online tutorials, and yet I still
can't get the hang of it. It's very frustrating. I've started by
designing a very simple class that has similar functionality to
what I eventually want to come up with, but not nearly as complex.
Just to get the hang of the basics before I get more complicated.
The class encapsulates an XML file. It has two attributes: the
file size and a linked list of the element names in the order they
are encountered in the file. Use it like this:

use Pete::MyFirstClass;

my $xmldoc = new Pete::MyFirstClass ('filename.xml');

print "The size of the document is ", $xmldoc->size, "\n";
print "Here is a list of the elements in the document:\n";

my $element = $xmldoc->elements;
while ($element) {
print $element->{name}, "\n";
$element = $element->{next};
}

__END__

Question 1: How do I turn {name} and {next} into accessor methods
rather than raw hash values?

Here's what I have so far:

package Pete::MyFirstClass;
require Exporter;
@ISA = qw(Exporter);
use strict 'vars';

sub new {
my ($self, $file) = @_;
my $size = -s $file;
my $elements = {};
my $last_element;
open (FILE, $file);
while (my $line = <FILE>) {
while ($line =~ /<([a-z0-9]+)[^<>]*>/g) {
my $name = $1;
my $tag = {};
$tag->{name} = $name;
if ($elements) {
$last_element->{next} = $tag;
$last_element = $tag;
} else {
$elements = $tag;
$last_element = $tag;
}
}
}
close (FILE);
return bless [$size, $elements];
}

sub size { return $_[0]->[0]; }
sub root { return $_[0]->[1]; }

__END__

## I'm guessing I'll need to create a new Element class with methods
## "next" and "name"? But I can't seem to get it to work right

package Elements;

sub new {
my $self = shift;
}

sub name {};
sub next {};

.



Relevant Pages

  • Re: Beginning OO help
    ... objects in so many perl books and online tutorials, ... Just to get the hang of the basics before I get more complicated. ... sub new { ...
    (comp.lang.perl.misc)
  • Re: Look At This Package
    ... sub full_name ... > use strict; ... > package UserInfo; ... >> This works, but again, accessor methods would be better. ...
    (perl.beginners)
  • Re: To MsgBox, or not to MsgBox
    ... >result in a hang if MsgBox is called. ... semi-ownerless MessageBox on the screen, ... Sub Main ...
    (microsoft.public.vb.general.discussion)
  • RE: Intermittent Form_Unload hangs
    ... > End Sub ... > I don't have any code in the Terminate or Query_Unload events. ... > Intermittently the app will hang in a processor loop unloading just this one ... > Anybody got anything to say about SP6 experiences? ...
    (microsoft.public.vb.bugs)
  • Re: Command Button / Macro wont execute properly
    ... Mine doesn't hang, but it needs ... Sub CommandButton1_Click ... 'Turn screen updating and autocalculation back on for normal operations ...
    (microsoft.public.excel.programming)