7 PPI::Token - A single token of Perl source code
16 C<PPI::Token> is the abstract base class for all Tokens. In PPI terms, a "Token" is
17 a L<PPI::Element> that directly represents bytes of source code.
24 use Params::Util qw{_INSTANCE};
26 use PPI::Exception ();
28 use vars qw{$VERSION @ISA};
31 @ISA = 'PPI::Element';
34 # We don't load the abstracts, they are loaded
35 # as part of the inheritance process.
37 # Load the token classes
38 use PPI::Token::BOM ();
39 use PPI::Token::Whitespace ();
40 use PPI::Token::Comment ();
41 use PPI::Token::Pod ();
42 use PPI::Token::Number ();
43 use PPI::Token::Number::Binary ();
44 use PPI::Token::Number::Octal ();
45 use PPI::Token::Number::Hex ();
46 use PPI::Token::Number::Float ();
47 use PPI::Token::Number::Exp ();
48 use PPI::Token::Number::Version ();
49 use PPI::Token::Word ();
50 use PPI::Token::DashedWord ();
51 use PPI::Token::Symbol ();
52 use PPI::Token::ArrayIndex ();
53 use PPI::Token::Magic ();
54 use PPI::Token::Quote::Single ();
55 use PPI::Token::Quote::Double ();
56 use PPI::Token::Quote::Literal ();
57 use PPI::Token::Quote::Interpolate ();
58 use PPI::Token::QuoteLike::Backtick ();
59 use PPI::Token::QuoteLike::Command ();
60 use PPI::Token::QuoteLike::Regexp ();
61 use PPI::Token::QuoteLike::Words ();
62 use PPI::Token::QuoteLike::Readline ();
63 use PPI::Token::Regexp::Match ();
64 use PPI::Token::Regexp::Substitute ();
65 use PPI::Token::Regexp::Transliterate ();
66 use PPI::Token::Operator ();
67 use PPI::Token::Cast ();
68 use PPI::Token::Structure ();
69 use PPI::Token::Label ();
70 use PPI::Token::HereDoc ();
71 use PPI::Token::Separator ();
72 use PPI::Token::Data ();
73 use PPI::Token::End ();
74 use PPI::Token::Prototype ();
75 use PPI::Token::Attribute ();
76 use PPI::Token::Unknown ();
82 #####################################################################
83 # Constructor and Related
86 bless { content => (defined $_[1] ? "$_[1]" : '') }, $_[0];
91 # @_ or throw Exception("No arguments to set_class");
92 my $class = substr( $_[0], 0, 12 ) eq 'PPI::Token::' ? shift : 'PPI::Token::' . shift;
94 # Find out if the current and new classes are complex
95 my $old_quote = (ref($self) =~ /\b(?:Quote|Regex)\b/o) ? 1 : 0;
96 my $new_quote = ($class =~ /\b(?:Quote|Regex)\b/o) ? 1 : 0;
98 # No matter what happens, we will have to rebless
101 # If we are changing to or from a Quote style token, we
102 # can't just rebless and need to do some extra thing
103 # Otherwise, we have done enough
104 return $class if ($old_quote - $new_quote) == 0;
106 # Make a new token from the old content, and overwrite the current
107 # token's attributes with the new token's attributes.
108 my $token = $class->new( $self->{content} );
111 # Return the class as a convenience
119 #####################################################################
124 =head2 set_content $string
126 The C<set_content> method allows to set/change the string that the
127 C<PPI::Token> object represents.
129 Returns the string you set the Token to
134 $_[0]->{content} = $_[1];
139 =head2 add_content $string
141 The C<add_content> method allows you to add additional bytes of code
142 to the end of the Token.
144 Returns the new full string after the bytes have been added.
148 sub add_content { $_[0]->{content} .= $_[1] }
154 The C<length> method returns the length of the string in a Token.
158 sub length { CORE::length($_[0]->{content}) }
164 #####################################################################
165 # Overloaded PPI::Element methods
171 # You can insert either a statement, or a non-significant token.
174 my $Element = _INSTANCE(shift, 'PPI::Element') or return undef;
175 if ( $Element->isa('PPI::Structure') ) {
176 return $self->__insert_before($Element);
177 } elsif ( $Element->isa('PPI::Token') ) {
178 return $self->__insert_before($Element);
183 # As above, you can insert a statement, or a non-significant token
186 my $Element = _INSTANCE(shift, 'PPI::Element') or return undef;
187 if ( $Element->isa('PPI::Structure') ) {
188 return $self->__insert_after($Element);
189 } elsif ( $Element->isa('PPI::Token') ) {
190 return $self->__insert_after($Element);
199 #####################################################################
202 sub __TOKENIZER__on_line_start { 1 }
203 sub __TOKENIZER__on_line_end { 1 }
204 sub __TOKENIZER__on_char { 'Unknown' }
210 #####################################################################
214 ref($_[0]) eq 'PPI::Token::Structure'
216 $_[0]->{content} =~ /(?:\(|\[|\{)/
219 sub __LEXER__closes {
220 ref($_[0]) eq 'PPI::Token::Structure'
222 $_[0]->{content} =~ /(?:\)|\]|\})/
231 See the L<support section|PPI/SUPPORT> in the main module.
235 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
239 Copyright 2001 - 2009 Adam Kennedy.
241 This program is free software; you can redistribute
242 it and/or modify it under the same terms as Perl itself.
244 The full text of the license can be found in the
245 LICENSE file included with this module.