real, simple sample OOP intro text??!!

G

Geoff Cox

Hello

I am trying to get started with Perl OOP but all the texts I have seen
so far, both from the Net and Perl bools are, either too abstract or
assume far too much ... I never want to see another OOP explanation
based on mythical cars, flowers, countries or whatever!

I am looking for a simple but real example of code based on a real
class (package) with the methods etc clearly explained.

Is this asking too much?!

Back in 1998, when I was trying to understand ODBC and CGI, I had the
same problem. In the end I was able to write a simple, step by step
approach (http://www.micro-active.com/dbase.htm) which was picked up
by a newsgroup and for several years afterwards I received thanks from
around the world for being in between too abstract and too complex ...

Can someone do the same for me now re Perl and OOP !!!???

Cheers

Geoff
 
T

Tassilo v. Parseval

Also sprach Geoff Cox:
I am trying to get started with Perl OOP but all the texts I have seen
so far, both from the Net and Perl bools are, either too abstract or
assume far too much ... I never want to see another OOP explanation
based on mythical cars, flowers, countries or whatever!

It appears to be rather popular among OOP-tutorial-writers to use these
kind of examples. I also sometimes wish they used simple real-world
scenarios. Or rather, I wished they had used them years ago when I
learnt it.
I am looking for a simple but real example of code based on a real
class (package) with the methods etc clearly explained.

Is this asking too much?!

From your rant above, I conclude that you have probably already read
perlboot.pod. If not, then you'll hate it because it involves animals.

A little more demanding is perltoot.pod. It has the advantage that it
uses less colorful examples (and ones more likely to occur in the
context of computers and programming).
Can someone do the same for me now re Perl and OOP !!!???

Do you consider yourself totally clueless as for OOP or have you already
picked up a few things from the tutorials that you've read rather
joylessly? Perhaps you have already reached stage 2 which means you know
little but you know enough to ask questions relating to OOP in this
group. People here will help you with them.

Tassilo
 
B

Bob Walton

Geoff Cox wrote:

....

I am trying to get started with Perl OOP but all the texts I have seen
so far, both from the Net and Perl bools are, either too abstract or
assume far too much ... I never want to see another OOP explanation
based on mythical cars, flowers, countries or whatever!

I am looking for a simple but real example of code based on a real
class (package) with the methods etc clearly explained.

....


Have you tried "Object Oriented Perl" by Damian Conway, Manning
Publications? I thought it was crystal clear and generally excellent.
My apoligies if it is one of the offending books -- different things for
different folks.
 
G

Geoff Cox

Have you tried "Object Oriented Perl" by Damian Conway, Manning
Publications? I thought it was crystal clear and generally excellent.
My apoligies if it is one of the offending books -- different things for
different folks.

Bob,

I am going to a book shop today to have a look at the all the latest
books on Perl and OOP so will include this one. Have seen an earlier
posting re Damian's book and the difficulty of sorting which is which
in the downloadable collection of scripts...have you seen this and do
you know if this has it been sorted out?

Cheers

Geoff
 
U

Uri Guttman

GC> I am going to a book shop today to have a look at the all the latest
GC> books on Perl and OOP so will include this one. Have seen an earlier
GC> posting re Damian's book and the difficulty of sorting which is which
GC> in the downloadable collection of scripts...have you seen this and do
GC> you know if this has it been sorted out?

AFAIK that wasn't a major issue. but i will second the book (and i
should know as i did a tech edit on it). it is the best (and only) book
on OOP and it is very well written. sure it uses a goofy set of examples
based on a cd collection but all books need to do that. real world
applications are usually too large for a single book to use and also
teach. but also most real world applications will not have the structure
that can be used to illustrate and compare different coding designs. you
choose a simple application so you can show how it can be developed in
many different ways and compare them. then the coding differences are
easily seen and can be highlighted. in a large real world app, only
small parts may have to do with an OO style or feature that is
important. so it would be very hard to show different ways of doing that
without reqwriting the whole thing.

this is just like learning math in school. 3 + 4 = 7 is needed to
compare to 4 + 3 = 7 and also 5 + 2 = 7. if you jumped right into
complex accounting examples, you would never grasp basic addition.

so you ain't gonna find any book or doc that does what you want. either
they use some dummy application to work with or they will focus on a
short program which highlights a particular feature.

so get OOP and read it and don't complain about something which will
never change as it is the right way to write a book on coding.

uri
 
G

Geoff Cox

joylessly? Perhaps you have already reached stage 2 which means you know
little but you know enough to ask questions relating to OOP in this
group. People here will help you with them.

Tassilo,

I was a little tired and frustrated when I posted above, but would
like to say that I was very grateful for the "mini tutorial" response
which you made to one of my earlier postings on this!

A couple of questions re that. I have used a modified version of your
code below and it does get me all the intro texts from the
total-260304.txt file but I have not understood how to get the <h2>
.... </h2> lines and I have not understood how to parse through the
html.htm file getting the <h2>.. </h2> data and the intro texts from
the total-260304.txt file in the order in which the <option statements
appear in html.htm.

I follow what you say re finding <option and value but cannot see how
I would get <h2>, <p> etc..

Would appreciate a little light here!

Geoff


package MyParser;
use base qw(HTML::parser);

open (OUT, ">>d:/a-keep9/nondb/parser/test/results.htm");
my $n=0;

print OUT ("<html><head><title>test</title></head><body> \n");
print OUT ("<table width='100%'> \n");

sub start {
my ($self, $tagname, $attr, $attrseq, $origtext) = @_;
if ($tagname eq 'option') {
&getintro($attr->{ value });
}
if ($tagname eq 'h') {
print OUT ("<tr><td>$attr->{ href } </td></tr> \n");
}
}

sub getintro {
open (INN, "d:/a-keep9/nondb/db/total-260304.txt");
while (defined (my $lineintro = <INN>)) {
if ($lineintro =~ /@_','(.*?)'\)\;/) {
print OUT ("<tr><td> $1 </td></tr>\n");

}

}
close(INN);
}

package main;
open (IN, "d:/a-keep9/nondb/parser/html.htm");
undef $/;
my $html = <IN>;
my $parser = MyParser->new;
$parser->parse( $html );

print OUT ("</body></html \n");
 
G

Geoff Cox

so get OOP and read it and don't complain about something which will
never change as it is the right way to write a book on coding.

Uri,

Are you really certain that OOP could not be taught from a real set of
data, a real class (module), real scripts etc? I will look at Damian's
book and try to be converted!

Cheers

Geoff
 
T

Tassilo v. Parseval

Also sprach Geoff Cox:
Tassilo,

I was a little tired and frustrated when I posted above, but would
like to say that I was very grateful for the "mini tutorial" response
which you made to one of my earlier postings on this!

It can be done for HTML::parser because it's just one module. I'd be a
bit more reluctant towards writing one on OOP. It's probably a topic too
huge for a single usenet posting.

Note that HTML::parser's subclassing technique doesn't require the full
set of OOP skills. It uses just one tiny aspect of it which I tried to
make clear in my posting. Apparently however 'subclass' can't be easily
explained without knowing what a 'class' in general is.
A couple of questions re that. I have used a modified version of your
code below and it does get me all the intro texts from the
total-260304.txt file but I have not understood how to get the <h2>
... </h2> lines and I have not understood how to parse through the
html.htm file getting the <h2>.. </h2> data and the intro texts from
the total-260304.txt file in the order in which the <option statements
appear in html.htm.

I follow what you say re finding <option and value but cannot see how
I would get <h2>, <p> etc..

Would appreciate a little light here!

Geoff

[ indenting fixed a bit ]
package MyParser;
use base qw(HTML::parser);

open (OUT, ">>d:/a-keep9/nondb/parser/test/results.htm");
my $n=0;

print OUT ("<html><head><title>test</title></head><body> \n");
print OUT ("<table width='100%'> \n");

sub start {
my ($self, $tagname, $attr, $attrseq, $origtext) = @_;
if ($tagname eq 'option') {
&getintro($attr->{ value });
}

For a tag like

<option value="bla">

this will call

getintro("bla");

in the moment when this tag was encountered in the HTML document.
if ($tagname eq 'h') {
print OUT ("<tr><td>$attr->{ href } </td></tr> \n");
}

What is the <h> tag and why does it have an "href" attribute?
You said you were looking for <h2> tags. That would mean you'd at least
have to write:

if ($tagname eq 'h2') {

Also, <h1>-<h6> don't have an "href" attribute. The only attribute they
can have is AFAIK "align". Or are you trying to access the "href"
attribute of a previous tag? If you want to do that, you have to store
it for later use (usually on a stack or so) when the start() method is
called for the tag containing this attribute. By the time start() is
%$attr wont have it because it belongs to a different said:
}

sub getintro {
open (INN, "d:/a-keep9/nondb/db/total-260304.txt");
while (defined (my $lineintro = <INN>)) {
if ($lineintro =~ /@_','(.*?)'\)\;/) {
print OUT ("<tr><td> $1 </td></tr>\n");

}

}
close(INN);
}

Now apparently you are searching for a line containing:

bla','<SOME_CHARACTERS>');

where you capture <SOME_CHARACTERS>.

Can you explain what kind of data there is in the file total-260304.txt?
To me it looks a bit like a fraction of the arguments for function calls.

At this point I don't understand how the <option>, <h2> and <p> tags
relate to each other. Can you give a small sample of the data in the
input HTML file and also of the data in the total-*.txt files?
And finally, show us how the result made from these files is supposed to
look like.

Tassilo
 
U

Uri Guttman

GC> Are you really certain that OOP could not be taught from a real
GC> set of data, a real class (module), real scripts etc? I will look
GC> at Damian's book and try to be converted!

my point is true with almost any computer topic or book. true read world
things are way too complex for a single book. anything that can be
covered properely in a book needs to be simple. did you get my point
about making highlights and comparisons? a complex system would hide
them and make the book impossible to follow. there is this
time/space/complexity tradeoff when writing a book. if it falls outside
that boundary then few will read or buy it which makes it a
failure. simple examples make it accessible and the author can focus on
the coding ideas and not the actual data. i have read dozens of computer
books and none have ever done something with complex real world data.
the perl/mason book actually used a real world problem that was the
apprentice.perl.org site but even that isn't that complex.

and i reiterate, get and read OOP. you will learn plenty and it won't be
a waste of your money or time.

uri
 
G

Geoff Cox

On 4 Apr 2004 08:52:54 GMT, "Tassilo v. Parseval"

Tassilo,
What is the <h> tag and why does it have an "href" attribute?
You said you were looking for <h2> tags. That would mean you'd at least
have to write:

if ($tagname eq 'h2') {

href only because I did not know what to use ... nonsense!
Can you explain what kind of data there is in the file total-260304.txt?
To me it looks a bit like a fraction of the arguments for function calls.

At this point I don't understand how the <option>, <h2> and <p> tags
relate to each other. Can you give a small sample of the data in the
input HTML file and also of the data in the total-*.txt files?
And finally, show us how the result made from these files is supposed to
look like.

My aim is to change a web site dependent on MySQL to a site which is
not. This is because the person who may take over the running of this
site would find MySQL etc too difficult...

So, there is a series of html files which have <h2> headings </h2>,
<p> text spread over several lines</p> and <options from which to
select a document. Once a particular document has been selected, the
summary text for the doc is obtained from the database, along with a
link to the zipped version of the full document for downloading.

So, I wish get the various headings, text, and summary texts from each
option in the order in whcih they appear in each html file and write
this info into a new html file.

Typical extract from html file

<h2 align="center">Business issues</h2>

<p align="center"><img src="images/image.jpg"</p>

<p>This section deals with jaklsdj kad klajksdl aksdk alksd jkla
asdkl ;aksd;lka;lskd ;lkal;sdk ;lkal;sdk; lal;sd</p>

<h2 align="center">Assignments</h2>

<p>asjd kjd asjdkh jaskdh jhasjdh ajsdhk jasd
djkadlkj lkajdkl akldj klasjd kljaksd kladjkla
akdl ;ak d;l ka;ldk l;a</p>

<form action="business.php" method="post" target="frameright">

<input type="hidden" name="sfield" value="name">
<input type="hidden" name="submit" value="1">/<b>Assignments</b><br>
<select name="term" size=2>
<option value="docs/adminops">Admin Operations</option>
<option value="docs/businesswork">Business at Work</option>
<option etc etc

I use the option value, which is the path to each doc, to search the
database for the document's summary text..

total-*.txt contains for example

INSERT INTO total VALUES ('docs/businesswork,'<h3>
gasdg asdhj ahjsd</h3>hajskdh ahjsd aksdj kash djk ahd sjkhajk
asdjk aj sdlkasd kals dkladj kjakldjk jakldjk aklsdjk lajsd
asldk aklsd;aksd ;ka;lsdkl a;lsdk.');

The result of all this will be html pages with various <h2> headings,
<p> text and summary texts for each document with links to the full
zipped document.

By the way I have just bought the O'Reilly Perl & LWP book. The Damian
Conway book costs £38.00! in London. It looks very good but perhaps
more than I need at the moment..


Cheers

Geoff
 
G

Geoff Cox

and i reiterate, get and read OOP. you will learn plenty and it won't be
a waste of your money or time.

Uri,

I have just been into London to look at Damian's OOP book. It does
look very good but costs £38 here and may be a little more than I need
at the moment. (On the other hand it may be just what I do need so may
end up buying it)...It does annoy me that we pay the same figure in UK
pounds as the US $ figure....is this a rip off somewhere along the
line?!

Cheers

Geoff
 
T

Tad McClellan

I was a little tired and frustrated when I posted above,


Have you seen the Posting Guidelines that are posted here frequently?

Be extra cautious when you get upset
Count to ten before composing a followup when you are upset
This is recommended in all Usenet newsgroups. Here in clpmisc, most
flaming sub-threads are not about any feature of Perl at all! They
are most often for what was seen as a breach of netiquette. If you
have lurked for a bit, then you will know what is expected and won't
make such posts in the first place.

But if you get upset, wait a while before writing your followup. I
recommend waiting at least 30 minutes.

Count to ten after composing and before posting when you are upset
After you have written your followup, wait *another* 30 minutes
before committing yourself by posting it. You cannot take it back
once it has been said.
 
G

Geoff Cox

You said you were looking for <h2> tags. That would mean you'd at least
have to write:

if ($tagname eq 'h2') {

Tassilo,

if above finds the <h2> text </h2>

how do I get at the text between the <h2> and the </h2> ? I know I
ought not to have to ask you but how do I find the answer? Is it in
the HTML::parser docs?

Geoff
 
T

Tassilo v. Parseval

Also sprach Geoff Cox:
Tassilo,

if above finds the <h2> text </h2>

It doesn't. It only finds <h2>. </h2> is handled by the end() method.
The part between those tags is handled by text() or again start() and
end() in case other tags are nested inside the header.
how do I get at the text between the <h2> and the </h2> ? I know I
ought not to have to ask you but how do I find the answer? Is it in
the HTML::parser docs?

You have to make your parser state-aware. The simplest solution would be
a global variable:

my $in_heading;

sub start {
my ($self, $tagname, undef, undef, $origtext) = @_;
if ($tagname eq 'h2') {
$in_heading = 1;
return;
}
print $origtext if $in_heading;
}

sub end {
my ($self, $tagname, $origtext) = @_;
if ($tagname eq 'h2') {
$in_heading = 0;
return;
}
print $origtext if $in_heading;
}

sub text {
my ($self, $origtext) = @_;
print $origtext if $in_heading;
}

The above will effectively print only the stuff that shows up in <h2>
tags, excluding them.

Another way is using a real stack. Three helper closures can be used for
that:

{ my @stack;
sub pushstack { push @stack, shift }
sub popstack { pop @stack }
sub peekstack { $stack[shift] }
}

Essentially, start() always does a pushstack($tagname), end() always a
popstack(). With peekstack() you can have a look at the elements on the
stack. peekstack(-1) would be the last element (the tag that was just
pushed onto it).

A stack is useful when you want to do more complicated things. You might
need additional functions for it, such as instack($tagname) that checks
whether $tagname is on the stack which would mean that you are currently
dealing with a tag below $tagname. It also requires that every starttag
has a corresponding endtag, or otherwise more complex handling is
necessary.

You just go for the $in_heading approach. It will perfectly do for your
task.

Tassilo
 
U

Uri Guttman

GC> I have just been into London to look at Damian's OOP book. It does
GC> look very good but costs £38 here and may be a little more than I
GC> need at the moment. (On the other hand it may be just what I do
GC> need so may end up buying it)...It does annoy me that we pay the
GC> same figure in UK pounds as the US $ figure....is this a rip off
GC> somewhere along the line?!

damn, you found out that i sekritly control book prices all over the
world. i set them high in london just so you wouldn't buy the one book
you really need! now i will have to find some other suckers^Wreaders for
damian's book. and i don't even get a kickback from him for any books
sales!

uri
 
T

Tassilo v. Parseval

Also sprach Uri Guttman:
GC> I have just been into London to look at Damian's OOP book. It does
GC> look very good but costs £38 here and may be a little more than I
GC> need at the moment. (On the other hand it may be just what I do
GC> need so may end up buying it)...It does annoy me that we pay the
GC> same figure in UK pounds as the US $ figure....is this a rip off
GC> somewhere along the line?!

damn, you found out that i sekritly control book prices all over the
world. i set them high in london just so you wouldn't buy the one book
you really need!

Putting aside the issue with prices, I wonder whether the OP really
needs this book. It's certainly nice when you want to learn how to do
fancy things in Perl fancily. A few chapters I found outright boring,
such as "Multiple Dispatch". Maybe it's because I don't write
widget-sets in Perl on a daily basis.

I'd rather suggest he has a look at Randal's "Learning Perl Objects,
References & Modules". And unlike Damian's book, this one comes with
excercises for the reader.

Tassilo
 
U

Uri Guttman

TvP> Also sprach Uri Guttman:

TvP> Putting aside the issue with prices, I wonder whether the OP really
TvP> needs this book. It's certainly nice when you want to learn how to do
TvP> fancy things in Perl fancily. A few chapters I found outright boring,
TvP> such as "Multiple Dispatch". Maybe it's because I don't write
TvP> widget-sets in Perl on a daily basis.

i agree some of OOP's wackier stuff is just too wacky for most uses. but
the way damian explains basic OO perl is better than anything i have
seen. and he builds on the basics when ho covers the more exotic
stuff. also the appendix comparing OO perl to the OO features of other
languages is very useful.

TvP> I'd rather suggest he has a look at Randal's "Learning Perl
TvP> Objects, References & Modules. And unlike Damian's book, this
TvP> one comes with excercises for the reader.

i would expect that to be good too. i haven't snarfed a copy yet :)

but given the pricing of us books in london, i doubt the OP will get
much of any discount though i think PORM has a cheaper list price than
OOP.

uri
 
G

Geoff Cox

It doesn't. It only finds <h2>. </h2> is handled by the end() method.
The part between those tags is handled by text() or again start() and
end() in case other tags are nested inside the header.

Tassilo

have used your code and my version works now! You will see that I have
extended it to work for <p> and that too works. I am not clear why the
following line appears in the start, end and text sub. I would have
thought it would appear just once...I am not following the logic??

print OUT ("<h2>$origtext</h2> \n") if $in_heading;

Cheers

Geoff

my $in_heading;
my $p;

sub start {

my ($self, $tagname, $attr, undef, undef, $origtext) = @_;
if ($tagname eq 'option') {
&getintro($attr->{ value });
}

if ($tagname eq 'h2') {
$in_heading = 1;
return;
}
print OUT ("<h2>$origtext</h2> \n") if $in_heading;

if ($tagname eq 'p') {
$p = 1;
return;
}
print OUT ("$origtext \n") if $p;

}

sub end {
my ($self, $tagname, $origtext) = @_;
if ($tagname eq 'h2') {
$in_heading = 0;
return;
}
print OUT ("<h2>$origtext</h2> \n") if $in_heading;

if ($tagname eq 'p') {
$p = 0;
return;
}
print OUT ("$origtext \n") if $p;

}

sub text {
my ($self, $origtext) = @_;
print OUT ("<h2>$origtext</h2> \n") if $in_heading;
print OUT ("$origtext \n") if $p;

}
 
G

Geoff Cox

damn, you found out that i sekritly control book prices all over the
world. i set them high in london just so you wouldn't buy the one book
you really need! now i will have to find some other suckers^Wreaders for
damian's book. and i don't even get a kickback from him for any books
sales!


Uri

wouldn't dream of suggesting you are personally responsible for this!!
if a book costs say $30 in the US and £30 here - does it really cost
£10 to get it into a shop the UK??

Cheers

Geoff
 
T

Tassilo v. Parseval

Also sprach Geoff Cox:
Tassilo

have used your code and my version works now! You will see that I have
extended it to work for <p> and that too works.

Glad to hear it.
I am not clear why the following line appears in the start, end and
text sub. I would have thought it would appear just once...I am not
following the logic??

print OUT ("<h2>$origtext</h2> \n") if $in_heading;

I don't think the above should appear like that in all three callbacks.
Cheers

Geoff

my $in_heading;
my $p;

sub start {

my ($self, $tagname, $attr, undef, undef, $origtext) = @_;

There is one undef too many. That means that $origtext will always be
undefined. Put 'use warnings;' in your code and perl will tell you that
you are printing an undefined value further below.
if ($tagname eq 'option') {
&getintro($attr->{ value });
}

if ($tagname eq 'h2') {
$in_heading = 1;
return;
}
print OUT ("<h2>$origtext</h2> \n") if $in_heading;

That's indeed not quite right. You create too many <h2>...</h2> pairs
with that. For this HTML snippet:

<h2><i>Heading</i></h2>

your parser spits out (assuming that you remove one of the above two
undefs):

<h2><i></h2><h2>Heading</h2><h2></i></h2>

This is because you wrap _everything_ inside <h2></h2> when $in_heading
is true.

If you want to include the heading tags in your output, then you have to do the
following:

sub start {
my ($self, $tagname, undef, undef, $origtext) = @_;
$in_heading = 1 if $tagname eq 'h2';
print $origtext if $in_heading;
}

sub text {
my ($self, $origtext) = @_;
print $origtext if $in_heading;
}

sub end {
my ($self, $tagname, $origtext) = @_;
print $origtext if $in_heading;
$in_heading = 0 if $tagname eq 'h2';
}

If you don't want to include them, you have to return from the
start/end functions without writing anything when a <h2> tag is
encountered.

As I wrote before: It takes a little time to get used to the way
HTML::parser does the job. You have to be clear about how HTML::parser
triggers the callbacks and what arguments are passed. Use a fixed font
for the following:

<tag attr="val"><tag1>some text</tag1></tag>
`--------------'`----'`-------'`-----'`----'
(1) (2) (3) (4) (5)

(1) start ($self,
'tag', # $tagname
{ attr => 'val' }, # $attr
[ 'attr' ], # $attrseq
'<tag attr="val">' # $origtext
);

(2) start ($self,
'tag1', # $tagname
{ }, # $attr
[ ], # $attrseq
'<tag1>' # $origtext
);

(3) text ($self,
'some text', # $origtext
0 # $is_cdata
;)

(4) end ($self,
'tag1', # $tagname
'</tag1>' # $origtext
);

(5) end ($self,
'tag', # $tagname
if ($tagname eq 'p') {
$p = 1;
return;
}

Unlike with the <h2> tags, here you do not print any <p> tags. So you
essentially just record when you are inside <p>, but you don't include
the <p> tags in your output.

Tassilo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top