I am trying to find the # VERSION comment in the perl source file. Then I want to insert the version before the comment (or it doesn't matter instead). Can someone tell me the correct way to do this using PPI ?
before
use strict; use warnings; package My::Package;
after
use strict; use warnings; package My::Package; our $VERSION = 0.1;
saving # VERSION in the final result is optional
I actually have a couple of ideas on how to find # VERSION, but one of them is a regular expression of a serialized ppi document that doesn't seem to be correct, and the other uses find_first for comments, but if it's not the first, I'm not sure what to do.
Updated code . This seems like the right solution as it only considers comments. but I'm not sure how to use or really how to create a new variable.
#!/usr/bin/env perl use 5.012; use strict; use warnings; use PPI; my $ppi = PPI::Document->new('test.pm'); my $comments = $ppi->find('PPI::Token::Comment'); my $version = PPI::Statement::Variable->new; foreach ( @{$comments} ) { if ( /^\s*
UPDATE
The answer to this question became the basis for DZP :: OurPkgVersion
source share