Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

how to add a PPI:Statement to a document?

by blahblahblah (Priest)
on Dec 28, 2007 at 20:23 UTC ( #659394=perlquestion: print w/ replies, xml ) Need Help??
blahblahblah has asked for the wisdom of the Perl Monks concerning the following question:

I have an open PPI document, and I want to insert an assignment statement at a certain point. $lastVar represents the statement after which I want to insert my new line.

I can successfully add a comment with the following:

my $newStatement = PPI::Token::Comment->new(); $newStatement->set_content(qq{\n# $newVar = ${quote}$newValue${quote +};\n}); $lastVar->insert_after($newStatement);
However, I want to add a full statement. If PPI::Statement had a set_content method, this is sort of what I'd like to do:
my $newStatement = PPI::Statement->new(); $newStatement->set_content(qq{\n1; #testing\n});
Can anyone point me in the right direction? Do I need to create a new PPI::Something and the build it up piece by piece from a bunch of other PPI::Something's, or is there a simpler way to add a block of code to a document?

Thanks,
Joe

Comment on how to add a PPI:Statement to a document?
Select or Download Code
Re: how to add a PPI:Statement to a document?
by Anonymous Monk on Dec 29, 2007 at 11:39 UTC
    Watch carefully
    $Document = PPI::Document->new(\'print "Hello World!\n"'); $lastVar->insert_after( $Document );
      $Document = PPI::Document->new(\'print "Hello World!\n"'); $lastVar->insert_after( $Document->find_first('Statement') );
      A little more verbose, but much clearer in intent.
        In fact, you also should just throw away the other document immediately.
        $lastVar->insert_after( PPI::Document->new('print "Hello World!\n")->f +ind_first('Statement') );

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://659394]
Approved by Limbic~Region
Front-paged by grinder
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (15)
As of 2014-07-30 12:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (231 votes), past polls