| 1 | <HTML> |
| 2 | <HEAD> |
| 3 | <TITLE>How about a simple example?</TITLE> |
| 4 | </HEAD> |
| 5 | <BODY> |
| 6 | |
| 7 | <P><B>Links</B>: <A HREF=extend.html>Next</A> <A HREF=xtrainfo.html>Previous</A> <A HREF=sgmlspm.html>Up</A> <A HREF=sgmlspm.html>Top</A></P> |
| 8 | |
| 9 | <H1>How about a simple example?</H1> |
| 10 | |
| 11 | <P>OK. The following script simply reports its events:</P> |
| 12 | |
| 13 | <P> |
| 14 | <PRE>#!/usr/bin/perl |
| 15 | |
| 16 | use SGMLS; |
| 17 | |
| 18 | $this_parse = new SGMLS(STDIN); # Read from standard input. |
| 19 | |
| 20 | while ($this_event = $this_parse->next_event) { |
| 21 | my $type = $this_event->type; |
| 22 | my $data = $this_event->data; |
| 23 | SWITCH: { |
| 24 | $type eq 'start_element' && do { |
| 25 | print "Beginning element: " . $data->name . "\n"; |
| 26 | last SWITCH; |
| 27 | }; |
| 28 | $type eq 'end_element' && do { |
| 29 | print "Ending element: " . $data->name . "\n"; |
| 30 | last SWITCH; |
| 31 | }; |
| 32 | $type eq 'cdata' && do { |
| 33 | print "Character data: " . $data . "\n"; |
| 34 | last SWITCH; |
| 35 | }; |
| 36 | $type eq 'sdata' && do { |
| 37 | print "Special data: " . $data . "\n"; |
| 38 | last SWITCH; |
| 39 | }; |
| 40 | $type eq 're' && do { |
| 41 | print "Record End\n"; |
| 42 | last SWITCH; |
| 43 | }; |
| 44 | $type eq 'pi' && do { |
| 45 | print "Processing Instruction: " . $data . "\n"; |
| 46 | last SWITCH; |
| 47 | }; |
| 48 | $type eq 'entity' && do { |
| 49 | print "External Data Entity: " . $data->name . |
| 50 | " with notation " . $data->notation->name . "\n"; |
| 51 | last SWITCH; |
| 52 | }; |
| 53 | $type eq 'start_subdoc' && do { |
| 54 | print "Beginning Subdocument Entity: " . $data->name . "\n"; |
| 55 | last SWITCH; |
| 56 | }; |
| 57 | $type eq 'end_subdoc' && do { |
| 58 | print "Ending Subdocument Entity: " . $data->name . "\n"; |
| 59 | last SWITCH; |
| 60 | }; |
| 61 | $type eq 'conforming' && do { |
| 62 | print "This is a conforming SGML document\n"; |
| 63 | last SWITCH; |
| 64 | }; |
| 65 | } |
| 66 | } |
| 67 | </PRE> |
| 68 | </P> |
| 69 | <P>To use it under Unix, try something like</P> |
| 70 | |
| 71 | <P> |
| 72 | <PRE>sgmls document.sgml | perl sample.pl</PRE> |
| 73 | </P> |
| 74 | <P>and watch the output scroll down.</P> |
| 75 | |
| 76 | |
| 77 | <P><B>Links</B>: <A HREF=extend.html>Next</A> <A HREF=xtrainfo.html>Previous</A> <A HREF=sgmlspm.html>Up</A> <A HREF=sgmlspm.html>Top</A></P> |
| 78 | |
| 79 | |
| 80 | <ADDRESS>David Megginson <A HREF="mailto:[email protected]"><[email protected]></A></ADDRESS> |
| 81 | </BODY> |
| 82 | </HTML> |