A
Arvin Portlock
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 {};
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 {};