Re: Beginning OO help
- From: "Mumia W." <paduille.4060.mumia.w+nospam@xxxxxxxxxxxxx>
- Date: Wed, 28 Mar 2007 01:40:45 GMT
On 03/27/2007 04:14 PM, Arvin Portlock wrote:
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.
Read "perldoc perltoot":
Start->Run->"perldoc perltoot"
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?
Read "perldoc perltoot"
Here's what I have so far:
package Pete::MyFirstClass;
require Exporter;
In this program, it probably doesn't make any difference, but it's probably better usually to do "use 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) { [...]
It's not a good idea to try to parse something as complicated as XML with regular expressions. Use one of the XML parsing modules instead.
After you've finished reading "perldoc perltoot," you can get an overview of the Perl documentation installed on your computer by reading "perldoc perl"
HTH
.
- References:
- Beginning OO help
- From: Arvin Portlock
- Beginning OO help
- Prev by Date: FAQ 8.33 Is there a way to hide perl's command line from programs such as "ps"?
- Next by Date: Re: Beginning OO help
- Previous by thread: Re: Beginning OO help
- Next by thread: Re: Beginning OO help
- Index(es):
Relevant Pages
|
|