Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Programmatically Updating Code with PPI

by Khen1950fx (Canon)
on Jan 04, 2010 at 22:36 UTC ( [id://815654]=note: print w/replies, xml ) Need Help??


in reply to Programmatically Updating Code with PPI

I tried this:
#! /usr/bin/perl use strict; use warnings; use diagnostics -verbose; use PPI; my $perlcode = << "END_PERLCODE"; package My::L10N::en; use base 'My::L10N'; our %Lexicon = ( 'Some [_1] text' => 'Some [_1] text' 'The [_1] is in the [_2]' => 'The [_1] is in the [_2]' ); END_PERLCODE my $doc = PPI::Document->new(\$perlcode); my $lexicon = $doc->find(sub { $_[1]->content eq '%Lexicon' }); $lexicon = $lexicon->[0]; # returns an array ref, but in my case, ther +e's only ever one. my $newmsg = q['The [_1] is in the [_2]']; my $newtext = sprintf(' %s =>\\n %s,', $newmsg, $newmsg, $newmsg, $newmsg); my $insert_this = PPI::Document->new(\$newtext); $lexicon->insert_after($insert_this->tokens()); print $doc->serialize; 1;

Replies are listed 'Best First'.
Re^2: Programmatically Updating Code with PPI
by Tanktalus (Canon) on Jan 04, 2010 at 22:40 UTC

    You cheated ;-) The $perlcode you started with was modified, which isn't the problem I'm having. You presumed the work was already done to insert that code, whereas I want to write the code to do that insertion. Thanks, though.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://815654]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-03-29 09:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found