Adam Kennedy > PPI-1.206 > PPI::Token::Word

Download:
PPI-1.206.tar.gz

Dependencies

Annotate this POD

CPAN RT

New  40
Open  28
View Bugs
Report a bug
Module Version: 1.206   Source  

NAME ^

PPI::Token::Word - The generic "word" Token

INHERITANCE ^

  PPI::Token::Word
  isa PPI::Token
      isa PPI::Element

DESCRIPTION ^

A PPI::Token::Word object is a PPI-specific representation of several different types of word-like things, and is one of the most common Token classes found in typical documents.

Specifically, it includes not only barewords, but also any other valid Perl identifier including non-operator keywords and core functions, and any include :: separators inside it, as long as it fits the format of a class, function, etc.

METHODS ^

There are no methods available for PPI::Token::Word beyond those provided by its PPI::Token and PPI::Element parent classes.

We expect to add additional methods to help further resolve a Word as a function, method, etc over time. If you need such a thing right now, look at Perl::Critic::Utils.

literal

Returns the value of the Word as a string. This assumes (often incorrectly) that the Word is a bareword and not a function, method, keyword, etc. This differs from content because Foo'Bar expands to Foo::Bar.

my @pairs = ( "F", 'F', "Foo::Bar", 'Foo::Bar', "Foo'Bar", 'Foo::Bar', ); while ( @pairs ) { my $from = shift @pairs; my $to = shift @pairs; my $doc = PPI::Document->new( \"$from;" ); isa_ok( $doc, 'PPI::Document' ); my $word = $doc->find_first('Token::Word'); isa_ok( $word, 'PPI::Token::Word' ); is( $word->literal, $to, "The source $from becomes $to ok" ); }

method_call

Answers whether this is the name of a method in a method call. Returns true if yes, false if no, and nothing if unknown.

my $Document = PPI::Document->new(\<<'END_PERL'); indirect $foo; indirect_class_with_colon Foo::; $bar->method_with_parentheses; print SomeClass->method_without_parentheses + 1; sub_call(); $baz->chained_from->chained_to; a_first_thing a_middle_thing a_last_thing; (first_list_element, second_list_element, third_list_element); first_comma_separated_word, second_comma_separated_word, third_comma_separated_word; single_bareword_statement; { bareword_no_semicolon_end_of_block } $buz{hash_key}; fat_comma_left_side => $thingy; END_PERL

isa_ok( $Document, 'PPI::Document' ); my $words = $Document->find('Token::Word'); is( scalar @{$words}, 23, 'Found the 23 test words' ); my %words = map { $_ => $_ } @{$words}; is( scalar $words{indirect}->method_call, undef, 'Indirect notation is unknown.', ); is( scalar $words{indirect_class_with_colon}->method_call, 1, 'Indirect notation with following word ending with colons is true.', ); is( scalar $words{method_with_parentheses}->method_call, 1, 'Method with parentheses is true.', ); is( scalar $words{method_without_parentheses}->method_call, 1, 'Method without parentheses is true.', ); is( scalar $words{print}->method_call, undef, 'Plain print is unknown.', ); is( scalar $words{SomeClass}->method_call, undef, 'Class in class method call is unknown.', ); is( scalar $words{sub_call}->method_call, 0, 'Subroutine call is false.', ); is( scalar $words{chained_from}->method_call, 1, 'Method that is chained from is true.', ); is( scalar $words{chained_to}->method_call, 1, 'Method that is chained to is true.', ); is( scalar $words{a_first_thing}->method_call, undef, 'First bareword is unknown.', ); is( scalar $words{a_middle_thing}->method_call, undef, 'Bareword in the middle is unknown.', ); is( scalar $words{a_last_thing}->method_call, 0, 'Bareword at the end is false.', ); foreach my $false_word ( qw< first_list_element second_list_element third_list_element first_comma_separated_word second_comma_separated_word third_comma_separated_word single_bareword_statement bareword_no_semicolon_end_of_block hash_key fat_comma_left_side > ) { is( scalar $words{$false_word}->method_call, 0, "$false_word is false.", ); }

TO DO ^

- Add function, method etc detector methods

SUPPORT ^

See the support section in the main module.

AUTHOR ^

Adam Kennedy <adamk@cpan.org>

COPYRIGHT ^

Copyright 2001 - 2009 Adam Kennedy.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

The full text of the license can be found in the LICENSE file included with this module.