Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I need to change the way a few hundred Perl modules end. Currently they end as

1;

However I need to modify this so that they all have this on their last few lines.

BEGIN {
    # VERSION
}
1;
# ABSTRACT: table definition

Of course I'm sure I could do this with Perl probably using PPI, but it also seems that I might just be able to sed or awk the end of these files. Can anyone provide any suggestions as to how this might be accomplished?

share|improve this question
add comment

1 Answer 1

up vote 3 down vote accepted

This is actually quite conveniently done with GNU sed, thanks to the combination of $ to act on the last line and -i to modify files in place. This assumes that the 1; is on the very last line, otherwise it would be a lot more complicated.

sed -i -e '$s/^1;$/BEGIN {\
    # VERSION\
}\
1;\
# ABSTRACT: table definition/' *.pm

With Perl, since the files are small, just load them fully in memory. This way it's easy to be more flexible, e.g. allow spaces and comments and __DATA__ after 1; (allowing POD is left as an exercise to the reader).

perl -0777 -pe 's/\n\s*1\s*;(?=(?:\s*(?:#.*)?\n)*(?:\s*__(?:DATA|END)__\b)?\Z)/\nBEGIN…definition/' *.pm
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.