Re: Beginning OO help



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
.



Relevant Pages

  • need help for this project......
    ... I am new to XML and PERL and I have a few questions the answers to ... user and makes an XML file from the fetched data. ... will scrape data from the T-Mobile web page to an XML file. ...
    (comp.lang.perl.misc)
  • Re: Help with validating XML (DTD or Schema) with PERL
    ... to handle DTD or Schemas? ... Amongst the many things I've happily & successfully done with Perl & ... parsing XML is not one. ... original thought I would easily find a FILTER to handle DTD or Schema ...
    (comp.lang.perl.modules)
  • Re: :Oracle - Any advance on inserting CLOB to XMLTYPE?
    ... perl xmltypeinsert-testcase.pl ORCL scott tiger 63 ... DBI v1.47 ... >> with them (aside from convert the text into XML). ... >> insert on the table with the XMLTYPE in it. ...
    (perl.dbi.users)
  • RE: help me in reading the xml file
    ... Is there a way to parse and get the info from xml withou using ... There are many XML parsing modules in Perl; ... really need to learn how to install modules. ...
    (perl.beginners)
  • Re: Converting XML to Perl structures FAST
    ... I need to convert XML documents to Perl structures, very efficiently, ... (god of porn) ...
    (comp.lang.perl.misc)