Commit | Line | Data |
---|---|---|
351625bd SP |
1 | |
2 | require 5; | |
3 | package Pod::Simple::PullParserStartToken; | |
4 | use Pod::Simple::PullParserToken (); | |
5 | @ISA = ('Pod::Simple::PullParserToken'); | |
6 | use strict; | |
7 | ||
8 | sub new { # Class->new(tagname, optional_attrhash); | |
9 | my $class = shift; | |
10 | return bless ['start', @_], ref($class) || $class; | |
11 | } | |
12 | ||
13 | # Purely accessors: | |
14 | ||
15 | sub tagname { (@_ == 2) ? ($_[0][1] = $_[1]) : $_[0][1] } | |
16 | sub tag { shift->tagname(@_) } | |
17 | ||
18 | sub is_tagname { $_[0][1] eq $_[1] } | |
19 | sub is_tag { shift->is_tagname(@_) } | |
20 | ||
21 | ||
22 | sub attr_hash { $_[0][2] ||= {} } | |
23 | ||
24 | sub attr { | |
25 | if(@_ == 2) { # Reading: $token->attr('attrname') | |
26 | ${$_[0][2] || return undef}{ $_[1] }; | |
27 | } elsif(@_ > 2) { # Writing: $token->attr('attrname', 'newval') | |
28 | ${$_[0][2] ||= {}}{ $_[1] } = $_[2]; | |
29 | } else { | |
30 | require Carp; | |
31 | Carp::croak( | |
32 | 'usage: $object->attr("val") or $object->attr("key", "newval")'); | |
33 | return undef; | |
34 | } | |
35 | } | |
36 | ||
37 | 1; | |
38 | ||
39 | ||
40 | __END__ | |
41 | ||
42 | =head1 NAME | |
43 | ||
44 | Pod::Simple::PullParserStartToken -- start-tokens from Pod::Simple::PullParser | |
45 | ||
46 | =head1 SYNOPSIS | |
47 | ||
48 | (See L<Pod::Simple::PullParser>) | |
49 | ||
50 | =head1 DESCRIPTION | |
51 | ||
52 | When you do $parser->get_token on a L<Pod::Simple::PullParser> object, you might | |
53 | get an object of this class. | |
54 | ||
55 | This is a subclass of L<Pod::Simple::PullParserToken> and inherits all its methods, | |
56 | and adds these methods: | |
57 | ||
58 | =over | |
59 | ||
60 | =item $token->tagname | |
61 | ||
62 | This returns the tagname for this start-token object. | |
63 | For example, parsing a "=head1 ..." line will give you | |
64 | a start-token with the tagname of "head1", token(s) for its | |
65 | content, and then an end-token with the tagname of "head1". | |
66 | ||
67 | =item $token->tagname(I<somestring>) | |
68 | ||
69 | This changes the tagname for this start-token object. | |
70 | You probably won't need | |
71 | to do this. | |
72 | ||
73 | =item $token->tag(...) | |
74 | ||
75 | A shortcut for $token->tagname(...) | |
76 | ||
77 | =item $token->is_tag(I<somestring>) or $token->is_tagname(I<somestring>) | |
78 | ||
79 | These are shortcuts for C<< $token->tag() eq I<somestring> >> | |
80 | ||
81 | =item $token->attr(I<attrname>) | |
82 | ||
83 | This returns the value of the I<attrname> attribute for this start-token | |
84 | object, or undef. | |
85 | ||
86 | For example, parsing a LZ<><Foo/"Bar"> link will produce a start-token | |
87 | with a "to" attribute with the value "Foo", a "type" attribute with the | |
88 | value "pod", and a "section" attribute with the value "Bar". | |
89 | ||
90 | =item $token->attr(I<attrname>, I<newvalue>) | |
91 | ||
92 | This sets the I<attrname> attribute for this start-token object to | |
93 | I<newvalue>. You probably won't need to do this. | |
94 | ||
95 | =item $token->attr_hash | |
96 | ||
97 | This returns the hashref that is the attribute set for this start-token. | |
98 | This is useful if (for example) you want to ask what all the attributes | |
99 | are -- you can just do C<< keys %{$token->attr_hash} >> | |
100 | ||
101 | =back | |
102 | ||
103 | ||
104 | You're unlikely to ever need to construct an object of this class for | |
105 | yourself, but if you want to, call | |
106 | C<< | |
107 | Pod::Simple::PullParserStartToken->new( I<tagname>, I<attrhash> ) | |
108 | >> | |
109 | ||
110 | =head1 SEE ALSO | |
111 | ||
112 | L<Pod::Simple::PullParserToken>, L<Pod::Simple>, L<Pod::Simple::Subclassing> | |
113 | ||
114 | =head1 COPYRIGHT AND DISCLAIMERS | |
115 | ||
116 | Copyright (c) 2002 Sean M. Burke. All rights reserved. | |
117 | ||
118 | This library is free software; you can redistribute it and/or modify it | |
119 | under the same terms as Perl itself. | |
120 | ||
121 | This program is distributed in the hope that it will be useful, but | |
122 | without any warranty; without even the implied warranty of | |
123 | merchantability or fitness for a particular purpose. | |
124 | ||
125 | =head1 AUTHOR | |
126 | ||
127 | Sean M. Burke C<sburke@cpan.org> | |
128 | ||
129 | =cut | |
130 |