| Pod-Parser documentation | Contained in the Pod-Parser distribution. |
Pod::InputObjects - objects representing POD input paragraphs, commands, etc.
use Pod::InputObjects;
perl5.004, Carp
Nothing.
This module defines some basic input objects used by Pod::Parser when reading and parsing POD text from an input source. The following objects are defined:
An object corresponding to a paragraph of POD input text. It may be a plain paragraph, a verbatim paragraph, or a command paragraph (see perlpod).
An object corresponding to an interior sequence command from the POD input text (see perlpod).
An object corresponding to a tree of parsed POD text. Each "node" in a parse-tree (or ptree) is either a text-string or a reference to a Pod::InteriorSequence object. The nodes appear in the parse-tree in the order in which they were parsed from left-to-right.
Each of these input objects are described in further detail in the sections which follow.
An object representing a paragraph of POD input text. It has the following methods/attributes:
my $pod_para1 = Pod::Paragraph->new(-text => $text);
my $pod_para2 = Pod::Paragraph->new(-name => $cmd,
-text => $text);
my $pod_para3 = new Pod::Paragraph(-text => $text);
my $pod_para4 = new Pod::Paragraph(-name => $cmd,
-text => $text);
my $pod_para5 = Pod::Paragraph->new(-name => $cmd,
-text => $text,
-file => $filename,
-line => $line_number);
This is a class method that constructs a Pod::Paragraph object and
returns a reference to the new paragraph object. It may be given one or
two keyword arguments. The -text keyword indicates the corresponding
text of the POD paragraph. The -name keyword indicates the name of
the corresponding POD command, such as head1 or item (it should
not contain the = prefix); this is needed only if the POD
paragraph corresponds to a command paragraph. The -file and -line
keywords indicate the filename and line number corresponding to the
beginning of the paragraph
my $para_cmd = $pod_para->cmd_name();
If this paragraph is a command paragraph, then this method will return
the name of the command (without any leading = prefix).
my $para_text = $pod_para->text();
This method will return the corresponding text of the paragraph.
my $raw_pod_para = $pod_para->raw_text();
This method will return the raw text of the POD paragraph, exactly as it appeared in the input.
my $prefix = $pod_para->cmd_prefix();
If this paragraph is a command paragraph, then this method will return the prefix used to denote the command (which should be the string "=" or "==").
my $separator = $pod_para->cmd_separator();
If this paragraph is a command paragraph, then this method will return the text used to separate the command name from the rest of the paragraph (if any).
my $ptree = $pod_parser->parse_text( $pod_para->text() );
$pod_para->parse_tree( $ptree );
$ptree = $pod_para->parse_tree();
This method will get/set the corresponding parse-tree of the paragraph's text.
my ($filename, $line_number) = $pod_para->file_line();
my $position = $pod_para->file_line();
Returns the current filename and line number for the paragraph object. If called in a list context, it returns a list of two elements: first the filename, then the line number. If called in a scalar context, it returns a string containing the filename, followed by a colon (':'), followed by the line number.
An object representing a POD interior sequence command. It has the following methods/attributes:
my $pod_seq1 = Pod::InteriorSequence->new(-name => $cmd
-ldelim => $delimiter);
my $pod_seq2 = new Pod::InteriorSequence(-name => $cmd,
-ldelim => $delimiter);
my $pod_seq3 = new Pod::InteriorSequence(-name => $cmd,
-ldelim => $delimiter,
-file => $filename,
-line => $line_number);
my $pod_seq4 = new Pod::InteriorSequence(-name => $cmd, $ptree);
my $pod_seq5 = new Pod::InteriorSequence($cmd, $ptree);
This is a class method that constructs a Pod::InteriorSequence object
and returns a reference to the new interior sequence object. It should
be given two keyword arguments. The -ldelim keyword indicates the
corresponding left-delimiter of the interior sequence (e.g. '<').
The -name keyword indicates the name of the corresponding interior
sequence command, such as I or B or C. The -file and
-line keywords indicate the filename and line number corresponding
to the beginning of the interior sequence. If the $ptree argument is
given, it must be the last argument, and it must be either string, or
else an array-ref suitable for passing to Pod::ParseTree::new (or
it may be a reference to a Pod::ParseTree object).
my $seq_cmd = $pod_seq->cmd_name();
The name of the interior sequence command.
$pod_seq->prepend($text);
$pod_seq1->prepend($pod_seq2);
Prepends the given string or parse-tree or sequence object to the parse-tree of this interior sequence.
$pod_seq->append($text);
$pod_seq1->append($pod_seq2);
Appends the given string or parse-tree or sequence object to the parse-tree of this interior sequence.
$outer_seq = $pod_seq->nested || print "not nested";
If this interior sequence is nested inside of another interior
sequence, then the outer/parent sequence that contains it is
returned. Otherwise undef is returned.
my $seq_raw_text = $pod_seq->raw_text();
This method will return the raw text of the POD interior sequence, exactly as it appeared in the input.
my $ldelim = $pod_seq->left_delimiter();
The leftmost delimiter beginning the argument text to the interior sequence (should be "<").
The rightmost delimiter beginning the argument text to the interior sequence (should be ">").
my $ptree = $pod_parser->parse_text($paragraph_text);
$pod_seq->parse_tree( $ptree );
$ptree = $pod_seq->parse_tree();
This method will get/set the corresponding parse-tree of the interior sequence's text.
my ($filename, $line_number) = $pod_seq->file_line();
my $position = $pod_seq->file_line();
Returns the current filename and line number for the interior sequence object. If called in a list context, it returns a list of two elements: first the filename, then the line number. If called in a scalar context, it returns a string containing the filename, followed by a colon (':'), followed by the line number.
This method performs any necessary cleanup for the interior-sequence. If you override this method then it is imperative that you invoke the parent method from within your own method, otherwise interior-sequence storage will not be reclaimed upon destruction!
This object corresponds to a tree of parsed POD text. As POD text is scanned from left to right, it is parsed into an ordered list of text-strings and Pod::InteriorSequence objects (in order of appearance). A Pod::ParseTree object corresponds to this list of strings and sequences. Each interior sequence in the parse-tree may itself contain a parse-tree (since interior sequences may be nested).
my $ptree1 = Pod::ParseTree->new;
my $ptree2 = new Pod::ParseTree;
my $ptree4 = Pod::ParseTree->new($array_ref);
my $ptree3 = new Pod::ParseTree($array_ref);
This is a class method that constructs a Pod::Parse_tree object and
returns a reference to the new parse-tree. If a single-argument is given,
it must be a reference to an array, and is used to initialize the root
(top) of the parse tree.
my $top_node = $ptree->top();
$ptree->top( $top_node );
$ptree->top( @children );
This method gets/sets the top node of the parse-tree. If no arguments are given, it returns the topmost node in the tree (the root), which is also a Pod::ParseTree. If it is given a single argument that is a reference, then the reference is assumed to a parse-tree and becomes the new top node. Otherwise, if arguments are given, they are treated as the new list of children for the top node.
This method gets/sets the children of the top node in the parse-tree. If no arguments are given, it returns the list (array) of children (each of which should be either a string or a Pod::InteriorSequence. Otherwise, if arguments are given, they are treated as the new list of children for the top node.
This method prepends the given text or parse-tree to the current parse-tree. If the first item on the parse-tree is text and the argument is also text, then the text is prepended to the first item (not added as a separate string). Otherwise the argument is added as a new string or parse-tree before the current one.
This method appends the given text or parse-tree to the current parse-tree. If the last item on the parse-tree is text and the argument is also text, then the text is appended to the last item (not added as a separate string). Otherwise the argument is added as a new string or parse-tree after the current one.
my $ptree_raw_text = $ptree->raw_text();
This method will return the raw text of the POD parse-tree exactly as it appeared in the input.
This method performs any necessary cleanup for the parse-tree. If you override this method then it is imperative that you invoke the parent method from within your own method, otherwise parse-tree storage will not be reclaimed upon destruction!
See Pod::Parser, Pod::Select
Please report bugs using http://rt.cpan.org.
Brad Appleton <bradapp@enteract.com>
| Pod-Parser documentation | Contained in the Pod-Parser distribution. |
############################################################################# # Pod/InputObjects.pm -- package which defines objects for input streams # and paragraphs and commands when parsing POD docs. # # Copyright (C) 1996-2000 by Bradford Appleton. All rights reserved. # This file is part of "PodParser". PodParser is free software; # you can redistribute it and/or modify it under the same terms # as Perl itself. ############################################################################# package Pod::InputObjects; use strict; use vars qw($VERSION); $VERSION = '1.31'; ## Current version of this package require 5.005; ## requires this Perl version or later #############################################################################
############################################################################# package Pod::InputSource; ##---------------------------------------------------------------------------
##---------------------------------------------------------------------------
sub new { ## Determine if we were called via an object-ref or a classname my $this = shift; my $class = ref($this) || $this; ## Any remaining arguments are treated as initial values for the ## hash that is used to represent this object. Note that we default ## certain values by specifying them *before* the arguments passed. ## If they are in the argument list, they will override the defaults. my $self = { -name => '(unknown)', -handle => undef, -was_cutting => 0, @_ }; ## Bless ourselves into the desired class and perform any initialization bless $self, $class; return $self; } ##---------------------------------------------------------------------------
sub name { (@_ > 1) and $_[0]->{'-name'} = $_[1]; return $_[0]->{'-name'}; } ## allow 'filename' as an alias for 'name' *filename = \&name; ##---------------------------------------------------------------------------
sub handle { return $_[0]->{'-handle'}; } ##---------------------------------------------------------------------------
sub was_cutting { (@_ > 1) and $_[0]->{-was_cutting} = $_[1]; return $_[0]->{-was_cutting}; } ##--------------------------------------------------------------------------- ############################################################################# package Pod::Paragraph; ##---------------------------------------------------------------------------
##---------------------------------------------------------------------------
sub new { ## Determine if we were called via an object-ref or a classname my $this = shift; my $class = ref($this) || $this; ## Any remaining arguments are treated as initial values for the ## hash that is used to represent this object. Note that we default ## certain values by specifying them *before* the arguments passed. ## If they are in the argument list, they will override the defaults. my $self = { -name => undef, -text => (@_ == 1) ? shift : undef, -file => '<unknown-file>', -line => 0, -prefix => '=', -separator => ' ', -ptree => [], @_ }; ## Bless ourselves into the desired class and perform any initialization bless $self, $class; return $self; } ##---------------------------------------------------------------------------
sub cmd_name { (@_ > 1) and $_[0]->{'-name'} = $_[1]; return $_[0]->{'-name'}; } ## let name() be an alias for cmd_name() *name = \&cmd_name; ##---------------------------------------------------------------------------
sub text { (@_ > 1) and $_[0]->{'-text'} = $_[1]; return $_[0]->{'-text'}; } ##---------------------------------------------------------------------------
sub raw_text { return $_[0]->{'-text'} unless (defined $_[0]->{'-name'}); return $_[0]->{'-prefix'} . $_[0]->{'-name'} . $_[0]->{'-separator'} . $_[0]->{'-text'}; } ##---------------------------------------------------------------------------
sub cmd_prefix { return $_[0]->{'-prefix'}; } ##---------------------------------------------------------------------------
sub cmd_separator { return $_[0]->{'-separator'}; } ##---------------------------------------------------------------------------
sub parse_tree { (@_ > 1) and $_[0]->{'-ptree'} = $_[1]; return $_[0]->{'-ptree'}; } ## let ptree() be an alias for parse_tree() *ptree = \&parse_tree; ##---------------------------------------------------------------------------
sub file_line { my @loc = ($_[0]->{'-file'} || '<unknown-file>', $_[0]->{'-line'} || 0); return (wantarray) ? @loc : join(':', @loc); } ##--------------------------------------------------------------------------- ############################################################################# package Pod::InteriorSequence; ##---------------------------------------------------------------------------
##---------------------------------------------------------------------------
sub new { ## Determine if we were called via an object-ref or a classname my $this = shift; my $class = ref($this) || $this; ## See if first argument has no keyword if (((@_ <= 2) or (@_ % 2)) and $_[0] !~ /^-\w/) { ## Yup - need an implicit '-name' before first parameter unshift @_, '-name'; } ## See if odd number of args if ((@_ % 2) != 0) { ## Yup - need an implicit '-ptree' before the last parameter splice @_, $#_, 0, '-ptree'; } ## Any remaining arguments are treated as initial values for the ## hash that is used to represent this object. Note that we default ## certain values by specifying them *before* the arguments passed. ## If they are in the argument list, they will override the defaults. my $self = { -name => (@_ == 1) ? $_[0] : undef, -file => '<unknown-file>', -line => 0, -ldelim => '<', -rdelim => '>', @_ }; ## Initialize contents if they havent been already my $ptree = $self->{'-ptree'} || new Pod::ParseTree(); if ( ref $ptree =~ /^(ARRAY)?$/ ) { ## We have an array-ref, or a normal scalar. Pass it as an ## an argument to the ptree-constructor $ptree = new Pod::ParseTree($1 ? [$ptree] : $ptree); } $self->{'-ptree'} = $ptree; ## Bless ourselves into the desired class and perform any initialization bless $self, $class; return $self; } ##---------------------------------------------------------------------------
sub cmd_name { (@_ > 1) and $_[0]->{'-name'} = $_[1]; return $_[0]->{'-name'}; } ## let name() be an alias for cmd_name() *name = \&cmd_name; ##--------------------------------------------------------------------------- ## Private subroutine to set the parent pointer of all the given ## children that are interior-sequences to be $self sub _set_child2parent_links { my ($self, @children) = @_; ## Make sure any sequences know who their parent is for (@children) { next unless (length and ref and ref ne 'SCALAR'); if (UNIVERSAL::isa($_, 'Pod::InteriorSequence') or UNIVERSAL::can($_, 'nested')) { $_->nested($self); } } } ## Private subroutine to unset child->parent links sub _unset_child2parent_links { my $self = shift; $self->{'-parent_sequence'} = undef; my $ptree = $self->{'-ptree'}; for (@$ptree) { next unless (length and ref and ref ne 'SCALAR'); $_->_unset_child2parent_links() if UNIVERSAL::isa($_, 'Pod::InteriorSequence'); } } ##---------------------------------------------------------------------------
sub prepend { my $self = shift; $self->{'-ptree'}->prepend(@_); _set_child2parent_links($self, @_); return $self; } ##---------------------------------------------------------------------------
sub append { my $self = shift; $self->{'-ptree'}->append(@_); _set_child2parent_links($self, @_); return $self; } ##---------------------------------------------------------------------------
sub nested { my $self = shift; (@_ == 1) and $self->{'-parent_sequence'} = shift; return $self->{'-parent_sequence'} || undef; } ##---------------------------------------------------------------------------
sub raw_text { my $self = shift; my $text = $self->{'-name'} . $self->{'-ldelim'}; for ( $self->{'-ptree'}->children ) { $text .= (ref $_) ? $_->raw_text : $_; } $text .= $self->{'-rdelim'}; return $text; } ##---------------------------------------------------------------------------
sub left_delimiter { (@_ > 1) and $_[0]->{'-ldelim'} = $_[1]; return $_[0]->{'-ldelim'}; } ## let ldelim() be an alias for left_delimiter() *ldelim = \&left_delimiter; ##---------------------------------------------------------------------------
sub right_delimiter { (@_ > 1) and $_[0]->{'-rdelim'} = $_[1]; return $_[0]->{'-rdelim'}; } ## let rdelim() be an alias for right_delimiter() *rdelim = \&right_delimiter; ##---------------------------------------------------------------------------
sub parse_tree { (@_ > 1) and $_[0]->{'-ptree'} = $_[1]; return $_[0]->{'-ptree'}; } ## let ptree() be an alias for parse_tree() *ptree = \&parse_tree; ##---------------------------------------------------------------------------
sub file_line { my @loc = ($_[0]->{'-file'} || '<unknown-file>', $_[0]->{'-line'} || 0); return (wantarray) ? @loc : join(':', @loc); } ##---------------------------------------------------------------------------
sub DESTROY { ## We need to get rid of all child->parent pointers throughout the ## tree so their reference counts will go to zero and they can be ## garbage-collected _unset_child2parent_links(@_); } ##--------------------------------------------------------------------------- ############################################################################# package Pod::ParseTree; ##---------------------------------------------------------------------------
##---------------------------------------------------------------------------
sub new { ## Determine if we were called via an object-ref or a classname my $this = shift; my $class = ref($this) || $this; my $self = (@_ == 1 and ref $_[0]) ? $_[0] : []; ## Bless ourselves into the desired class and perform any initialization bless $self, $class; return $self; } ##---------------------------------------------------------------------------
sub top { my $self = shift; if (@_ > 0) { @{ $self } = (@_ == 1 and ref $_[0]) ? ${ @_ } : @_; } return $self; } ## let parse_tree() & ptree() be aliases for the 'top' method *parse_tree = *ptree = \⊤ ##---------------------------------------------------------------------------
sub children { my $self = shift; if (@_ > 0) { @{ $self } = (@_ == 1 and ref $_[0]) ? ${ @_ } : @_; } return @{ $self }; } ##---------------------------------------------------------------------------
use vars qw(@ptree); ## an alias used for performance reasons sub prepend { my $self = shift; local *ptree = $self; for (@_) { next unless length; if (@ptree && !(ref $ptree[0]) && !(ref $_)) { $ptree[0] = $_ . $ptree[0]; } else { unshift @ptree, $_; } } } ##---------------------------------------------------------------------------
sub append { my $self = shift; local *ptree = $self; my $can_append = @ptree && !(ref $ptree[-1]); for (@_) { if (ref) { push @ptree, $_; } elsif(!length) { next; } elsif ($can_append) { $ptree[-1] .= $_; } else { push @ptree, $_; } } }
sub raw_text { my $self = shift; my $text = ''; for ( @$self ) { $text .= (ref $_) ? $_->raw_text : $_; } return $text; } ##--------------------------------------------------------------------------- ## Private routines to set/unset child->parent links sub _unset_child2parent_links { my $self = shift; local *ptree = $self; for (@ptree) { next unless (defined and length and ref and ref ne 'SCALAR'); $_->_unset_child2parent_links() if UNIVERSAL::isa($_, 'Pod::InteriorSequence'); } } sub _set_child2parent_links { ## nothing to do, Pod::ParseTrees cant have parent pointers }
sub DESTROY { ## We need to get rid of all child->parent pointers throughout the ## tree so their reference counts will go to zero and they can be ## garbage-collected _unset_child2parent_links(@_); } #############################################################################
1;