| 1 | <HTML> |
| 2 | <HEAD> |
| 3 | <TITLE>How do I design my own classes?</TITLE> |
| 4 | </HEAD> |
| 5 | <BODY> |
| 6 | |
| 7 | <P><B>Links</B>: <A HREF=bugs.html>Next</A> <A HREF=example.html>Previous</A> <A HREF=sgmlspm.html>Up</A> <A HREF=sgmlspm.html>Top</A></P> |
| 8 | |
| 9 | <H1>How do I design my <EM>own</EM> classes?</H1> |
| 10 | |
| 11 | <P>In addition to the methods listed above, all of the classes used |
| 12 | in <A HREF=sgmlspm.html><TT>SGMLS.pm</TT></A> have an <TT>ext</TT> method which returns a |
| 13 | reference to an initially-empty hash table. You are free to use this |
| 14 | hash table to store <EM>anything</EM> you want -- it |
| 15 | should be especially useful if you are building your own, derived |
| 16 | classes from the ones provided here. The following example builds a |
| 17 | derived class <TT>My_Element</TT> from the <A HREF=sgmlselement.html><TT>SGMLS_Element</TT></A> |
| 18 | class, adding methods to set and get the current font:</P> |
| 19 | |
| 20 | <P> |
| 21 | <PRE>use SGMLS; |
| 22 | |
| 23 | package My_Element; |
| 24 | @ISA = qw(SGMLS_Element); |
| 25 | |
| 26 | sub new { |
| 27 | my ($class,$element,$font) = @_; |
| 28 | $element->ext->{'font'} = $font; |
| 29 | return bless $element; |
| 30 | } |
| 31 | |
| 32 | sub get_font { |
| 33 | my ($self) = @_; |
| 34 | return $self->ext->{'font'}; |
| 35 | } |
| 36 | |
| 37 | sub set_font { |
| 38 | my ($self,$font) = @_; |
| 39 | $self->ext->{'font'} = $font; |
| 40 | }</PRE> |
| 41 | </P> |
| 42 | <P>Note that the derived class does not need to have any knowledge |
| 43 | about the underlying structure of the <A HREF=sgmlselement.html><TT>SGMLS_Element</TT></A> |
| 44 | class, and need only avoid shadowing any of the methods currently |
| 45 | existing there.</P> |
| 46 | |
| 47 | <P>If you decide to create a derived class from the <A HREF=sgmls.html><TT>SGMLS</TT></A>, please note that in |
| 48 | addition to the methods listed above, that class uses internal methods |
| 49 | named <TT>element</TT>, <TT>line</TT>, and |
| 50 | <TT>file</TT>, similar to the same methods in <A HREF=sgmlsevent.html><TT>SGMLS_Event</TT></A> -- |
| 51 | it is essential that you not shadow these method names.</P> |
| 52 | |
| 53 | |
| 54 | <P><B>Links</B>: <A HREF=bugs.html>Next</A> <A HREF=example.html>Previous</A> <A HREF=sgmlspm.html>Up</A> <A HREF=sgmlspm.html>Top</A></P> |
| 55 | |
| 56 | |
| 57 | <ADDRESS>David Megginson <A HREF="mailto:[email protected]"><[email protected]></A></ADDRESS> |
| 58 | </BODY> |
| 59 | </HTML> |