SGMLS(3pm)
β¨ NAME β¨
bestie, welcome to SGMLS, which is the Perl class (3pm β yes she lives in the Perl module pages, not the command pages, iykyk) that handles postprocessing the output from the onsgmls, sgmls, and nsgmls parsers.
like, those three parsers do the parsing thing, and then SGMLS picks up the output and lets you vibe with it in Perl. π
πΈ SYNOPSIS πΈ
okay so this is literally the whole flow. you make a parse object, you keep asking it for next_event, and you get events back until you don't. no cap. π¦
use SGMLS;
my $parse = new SGMLS(STDIN);
my $event = $parse->next_event;
while ($event) {
SWITCH: {
($event->type eq 'start_element') && do {
my $element = $event->data; # An object of class SGMLS_Element
[[your code for the beginning of an element]]
last SWITCH;
};
($event->type eq 'end_element') && do {
my $element = $event->data; # An object of class SGMLS_Element
[[your code for the end of an element]]
last SWITCH;
};
($event->type eq 'cdata') && do {
my $cdata = $event->data; # A string
[[your code for character data]]
last SWITCH;
};
($event->type eq 'sdata') && do {
my $sdata = $event->data; # A string
[[your code for system data]]
last SWITCH;
};
($event->type eq 're') && do {
[[your code for a record end]]
last SWITCH;
};
($event->type eq 'pi') && do {
my $pi = $event->data; # A string
[[your code for a processing instruction]]
last SWITCH;
};
($event->type eq 'entity') && do {
my $entity = $event->data; # An object of class SGMLS_Entity
[[your code for an external entity]]
last SWITCH;
};
($event->type eq 'start_subdoc') && do {
my $entity = $event->data; # An object of class SGMLS_Entity
[[your code for the beginning of a subdoc entity]]
last SWITCH;
};
($event->type eq 'end_subdoc') && do {
my $entity = $event->data; # An object of class SGMLS_Entity
[[your code for the end of a subdoc entity]]
last SWITCH;
};
($event->type eq 'conforming') && do {
[[your code for a conforming document]]
last SWITCH;
};
die "Internal error: unknown event type " . $event->type . "\n";
}
$event = $parse->next_event;
}
so the tea on that loop: you build your parse object with new SGMLS(STDIN), then you call $parse->next_event over and over. each event has a ->type you switch on, and a ->data you reach for β and what ->data is totally depends on the type. sometimes it's an object (the element ones, the entity ones β those come back as SGMLS_Element and SGMLS_Entity respectively), sometimes it's just a string.
and notice the <red>die "Internal error: unknown event type " . $event->type . "\n";</red> sitting there as the catch-all β if an event type shows up that isn't one of the ones listed, that's the moment you find out. π
theSWITCH:block pluslast SWITCHis just Perl's classic fall-through trick, so each branch bounces out cleanly instead of falling into the next one. iconic, honestly. π«Ά
π DESCRIPTION π
Okay bestie, so the SGMLS package is literally a whole squad of related classes β no cap! You got SGMLS, SGMLS_Event, SGMLS_Element, SGMLS_Attribute, SGMLS_Notation, and SGMLS_Entity. And the way they all just show up the moment you type:
use SGMLS;
It's giving iconic. Usually, the only object you'll ever create yourself is the SGMLS one, and the rest just manifest automatically over the course of the parse. Lowkey magical. If you want the full lore, there's much fuller documentation chilling in the .sgml files in the DOC/ directory of the SGMLS.pm distribution. β¨
π The SGMLS class π
This class holds a single parse. When you make an instance of it, you pass a file handle as an argument (if you're reading the output of onsgmls, sgmls or nsgmls from a pipe, that file handle is usually STDIN):
my $parse = new SGMLS(STDIN);
The most important method here is next_event, which reads and returns the next major event from the input stream. And ugh, it's literally so smart β the SGMLS class deals with most ESIS events itself. Attributes and entity definitions? Collected, stored, handled automatically, and completely invisible to you. It's the main character so you don't have to be. π
Here's every method the SGMLS class has:
next_event()β Return anSGMLS_Eventobject containing the next major event from the SGML parse.element()β Return anSGMLS_Elementobject containing the current element in the document.file()β Return a string containing the name of the current SGML source file (this will work only if the-loption was given toonsgmls,sgmlsornsgmls).line()β Return a string containing the current line number from the source file (this will work only if the-loption was given toonsgmls,sgmlsornsgmls).appinfo()β Return a string containing theAPPINFOparameter (if any) from the SGML declaration.notation(NNAME)β Return anSGMLS_Notationobject representing the notation namedNNAME. With newer versions ofnsgmls, all notations are available; otherwise, only the notations which are actually used will be available.entity(ENAME)β Return anSGMLS_Entityobject representing the entity namedENAME. With newer versions ofnsgmls, all entities are available; otherwise, only external data entities and internal entities used as attribute values will be available.ext()β Return a reference to an associative array for user-defined extensions.
πΈ The SGMLS_Event class πΈ
This class holds a single major event, generated by the next_event method in the SGMLS class. It uses these methods:
type()β Return a string describing the type of event:start_element,end_element,cdata,sdata,re,pi,entity,start_subdoc,end_subdoc, andconforming. SeeSYNOPSIS, above, for the values associated with each of these.data()β Return the data associated with the current event (if any). Forstart_elementandend_element, returns anSGMLS_ELementobject; forentity,start_subdoc, andend_subdoc, returns anSGMLS_Entityobject; forcdata,sdata, andpi, returns a string; and forreandconforming, returns the empty string. SeeSYNOPSIS, above, for an example of this method's use.key()β Return a string key to the event, such as an element or entity name (otherwise, the same asdata()).file()β Return the current file name, as in theSGMLSclass.line()β Return the current line number, as in theSGMLSclass.element()β Return the current element, as in theSGMLSclass.parse()β Return theSGMLSobject which generated the event.entity(ENAME)β Look up an entity, as in theSGMLSclass.notation(ENAME)β Look up a notation, as in theSGMLSclass.ext()β Return a reference to an associative array for user-defined extensions.
π¦ The SGMLS_Element class π¦
This class is used for elements, and it holds all the associated info (like the element's attributes, bestie). It recognises the following methods:
name()β Return a string containing the name, or Generic Identifier, of the element, in upper case.parent()β Return theSGMLS_Elementobject for the element's parent (if any).parse()β Return theSGMLSobject for the current parse.attributes()β Return a reference to an associative array of attribute names andSGMLS_Attributestructures. Attribute names will be all in upper case.attribute_names()β Return an array of strings containing the names of all attributes defined for the current element, in upper case.attribute(ANAME)β Return theSGMLS_Attributestructure for the attributeANAME.set_attribute(ATTRIB)β Add theSGMLS_AttributeobjectATTRIBto the current element, replacing any other attribute structure with the same name.in(GI)β Returntrue(ie. 1) if the stringGIis the name of the current element's parent, orfalse(ie. 0) if it is not.within(GI)β Returntrue(ie. 1) if the stringGIis the name of any of the ancestors of the current element, orfalse(ie. 0) if it is not.ext()β Return a reference to an associative array for user-defined extensions.
π The SGMLS_Attribute class π
Every single attribute for every SGMLS_Element is a whole object of this class. It recognises these methods:
name()β Return a string containing the name of the current attribute, all in upper case.type()β Return a string containing the type of the current attribute, all in upper case. Available types areIMPLIED,CDATA,NOTATION,ENTITY, andTOKEN.value()β Return the value of the current attribute, if any. This will be an empty string if the type isIMPLIED, a string of some sort if the type isCDATAorTOKEN(if it isTOKEN, you may want to split the string into a series of separate tokens), anSGMLS_Notationobject if the type isNOTATION, or anSGMLS_Entityobject if the type isENTITY.
Not me dropping the biggest warning ever: if the value is CDATA, it will not have escape sequences for 8-bit characters, record ends, or SDATA processed β that will be your responsibility. You have been warned, bestie! π
is_implied()β Returntrue(ie. 1) if the value of the attribute is implied, orfalse(ie. 0) if it is specified in the document.set_type(TYPE)β Change the type of the attribute to the stringTYPE(which should be all in upper case). Available types areIMPLIED,CDATA,NOTATION,ENTITY, andTOKEN.set_value(VALUE)β Change the value of the attribute toVALUE, which may be a string, anSGMLS_Entityobject, or anSGMLS_Notationsubject, depending on the attribute's type.ext()β Return a reference to an associative array available for user-defined extensions.
π§ The SGMLS_Notation class π§
All declared notations appear as objects belonging to this class, which recognises these methods:
name()β Return a string containing the name of the notation.sysid()β Return a string containing the system identifier of the notation, if any.pubid()β Return a string containing the public identifier of the notation, if any.ext()β Return a reference to an associative array available for user-defined extensions.
π₯ The SGMLS_Entity class π₯
All declared entities appear as objects belonging to this class, which recognises these methods:
name()β Return a string containing the name of the entity, in mixed case.type()β Return a string containing the type of the entity, in upper case. Available types areCDATA,SDATA,NDATA(external entities only),SUBDOC,PI(newer versions ofnsgmlsonly), orTEXT(newer versions ofnsgmlsonly).value()β Return a string containing the value of the entity, if it is internal.sysid()β Return a string containing the system identifier of the entity (if any), if it is external.pubid()β Return a string containing the public identifier of the entity (if any), if it is external.filenames()β Return an array of strings containing any file names generated from the identifiers, if the entity is external.notation()β Return theSGMLS_Notationobject associated with the entity, if it is external.data_attributes()β Return a reference to an associative array of data attribute names (in upper case) and the associatedSGMLS_Attributeobjects for the current entity.data_attribute_names()β Return an array of data attribute names (in upper case) for the current entity.data_attribute(ANAME)β Return theSGMLS_Attributeobject for the data attribute namedANAMEfor the current entity.set_data_attribute(ATTRIB)β Add theSGMLS_AttributeobjectATTRIBto the current entity, replacing any other data attribute with the same name.ext()β Return a reference to an associative array for user-defined extensions.
π AUTHOR AND COPYRIGHT π
Copyright 1994 and 1995 by David Megginson, "dmeggins@aix1.uottawa.ca". Distributed under the terms of the Gnu General Public License (version 2, 1991) β see the file "COPYING" which is included in the SGMLS.pm distribution.
π SEE ALSO: π
SGMLS::Output and SGMLS::Refs.
And that's the whole vibe, bestie! This was brought to you by perl v5.36.0 on 2022-10-13, serving SGMLS(3pm) realness. Stay slaying those parses! π
πβ¨ππ¦πΈπ«Ά
π¦ where this page comes from
- ubuntu noble: libsgmls-perl 1.03ii-38 Β· GPL-2.0-or-later
- ubuntu resolute: libsgmls-perl 1.03ii-38 Β· GPL-2.0-or-later
- ubuntu stonking: libsgmls-perl 1.03ii-38 Β· GPL-2.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms