1 package PPI::Token::Attribute;
7 PPI::Token::Attribute - A token for a subroutine attribute
17 In Perl, attributes are a relatively recent addition to the language.
19 Given the code C< sub foo : bar(something) {} >, the C<bar(something)>
20 part is the attribute.
22 A C<PPI::Token::Attribute> token represents the entire of the attribute,
23 as the braces and its contents are not parsed into the tree, and are
24 treated by Perl (and thus by us) as a single string.
28 This class provides some additional methods beyond those provided by its
29 L<PPI::Token> and L<PPI::Element> parent classes.
31 Got any ideas for methods? Submit a report to rt.cpan.org!
38 use vars qw{$VERSION @ISA};
47 #####################################################################
48 # PPI::Token::Attribute Methods
54 The C<identifier> attribute returns the identifier part of the attribute.
56 That is, for the attribute C<foo(bar)>, the C<identifier> method would
63 $self->{content} =~ /^(.+?)\(/ ? $1 : $self->{content};
70 The C<parameters> method returns the parameter strong for the attribute.
72 That is, for the attribute C<foo(bar)>, the C<parameters> method would
75 Returns the parameters as a string (including the null string C<''> for
76 the case of an attribute such as C<foo()>.
78 Returns C<undef> if the attribute does not have parameters.
84 $self->{content} =~ /\((.+)\)$/ ? $1 : undef;
91 #####################################################################
94 sub __TOKENIZER__on_char {
97 my $char = substr( $t->{line}, $t->{line_cursor}, 1 );
99 # Unless this is a '(', we are finished.
100 unless ( $char eq '(' ) {
101 # Finalise and recheck
102 return $t->_finalize_token->__TOKENIZER__on_char( $t );
105 # This is a bar(...) style attribute.
106 # We are currently on the ( so scan in until the end.
107 # We finish on the character AFTER our end
108 my $string = $class->__TOKENIZER__scan_for_end( $t );
111 $t->{token}->{content} .= $$string;
116 # Found the end of the attribute
117 $t->{token}->{content} .= $string;
118 $t->_finalize_token->__TOKENIZER__on_char( $t );
121 # Scan for a close braced, and take into account both escaping,
122 # and open close bracket pairs in the string. When complete, the
123 # method leaves the line cursor on the LAST character found.
124 sub __TOKENIZER__scan_for_end {
127 # Loop as long as we can get new lines
130 while ( exists $t->{line} ) {
131 # Get the search area
132 my $search = $t->{line_cursor}
133 ? substr( $t->{line}, $t->{line_cursor} )
137 unless ( $search =~ /^((?:\\.|[^()])*?[()])/ ) {
138 # Load in the next line
140 $t->_fill_line(1) or return \$string;
146 $t->{line_cursor} += length $1;
148 # Alter the depth and continue if we arn't at the end
149 $depth += ($1 =~ /\($/) ? 1 : -1 and next;
155 # Returning the string as a reference indicates EOF
165 See the L<support section|PPI/SUPPORT> in the main module.
169 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
173 Copyright 2001 - 2009 Adam Kennedy.
175 This program is free software; you can redistribute
176 it and/or modify it under the same terms as Perl itself.
178 The full text of the license can be found in the
179 LICENSE file included with this module.