Subject: | How to create a PPI::Structure::Condition |
Since PPI leaves of the Structure::Condition and Statement::Expression
when parsing things like "if $a == 9" (note the lack of "()" around the
expression!), I wanted to re-insert them into the tree, so that my code
deals with the tree later is simplified to only one variant. Aka I want
to turn:
PPI::Document
PPI::Statement::Break
PPI::Token::Word 'return'
PPI::Token::Word 'if'
PPI::Token::Symbol '$a'
PPI::Token::Operator '=='
PPI::Token::Number '0'
Into this:
PPI::Document
PPI::Statement::Break
PPI::Token::Word 'return'
PPI::Token::Word 'if'
PPI::Structure::Condition ( ... )
PPI::Statement::Expression
PPI::Token::Symbol '$a'
PPI::Token::Operator '=='
PPI::Token::Number '0'
For this, I need to create a "PPI::Structure::Condition" and insert it
after the "if".
So, naturally you do:
# Add a PPI::Structure::Condition
my $Token = PPI::Structure::Condition->new();
and try to insert it. Except it doesn't work.
After some very very long struggles with creating this object, I found
exactly one way (partially by reading the source of Lexer.pm) this seems
to work:
# Add a PPI::Structure::Condition
my $Token = PPI::Structure::Condition->new(
PPI::Token::Structure->new('('),
);
$Token->{finish} = PPI::Token::Structure->new(')');
This however violates the OO convention because it access internal data
(the "->{finish}" part).
So I'd like to have an easier way to create PPI::Structure::Condition
elements, and this should also be documented (the POD for
PPI::Structure::Condition is useless in its current form).