editing challenge: Perl vs. cfengine

T

Ted Zlatanov

This question was recently posted to the cfengine mailing list by a
cfengine user. Cfengine's 'editfiles' functionality in this
particular case is quite succinct. Can anyone do this in Perl to meet
the challenge? Of course it can be done in one line, but will it be
as elegant?

The actual sequence of commands is pretty obvious - the outer
BeginGroup/EndGroup is an if() statement, and
LocateLineMatching/IncrementPointer move the editing cursor; -3 means
to move 3 lines back in the file.

My own attempt used an array-file tie and was definitely not
as elegant.

Ted

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

How can I write a script that would perform something like the
following and would remain as elegant?

BeginGroupIfNoLineMatching "# Addition by EMC ended."
LocateLineMatching ".*. /etc/init.d/functions.*"
IncrementPointer "-3"
InsertLine "# Addition by EMC started."
InsertLine 'DHCP_HOSTNAME=${dollar}{HOSTNAME}'
InsertLine "# Addition by EMC ended."
InsertLine ""
EndGroup

Off course, this can be done, but I do not know of any scripting tool
that allows one to express the above changes so elegantly. Anybody
can translate this into sed/awk in about the same number of lines?
 
M

Matija Papec

X-Ftn-To: Ted Zlatanov

Ted Zlatanov said:
The actual sequence of commands is pretty obvious - the outer
BeginGroup/EndGroup is an if() statement, and
LocateLineMatching/IncrementPointer move the editing cursor; -3 means
to move 3 lines back in the file.

My own attempt used an array-file tie and was definitely not
as elegant.

IMHO, you don't have much choice; you'll need some module with nice
interface to stay elegant.
 
T

Ted Zlatanov

X-Ftn-To: Ted Zlatanov



IMHO, you don't have much choice; you'll need some module with nice
interface to stay elegant.

I think a module would be OK to answer this challenge, as long as
it's not written just to answer this challenge, but is really useful :)

In particular, Perl does not have good facilities for a logical
line-based cursor for insertion and deletion of lines. Emacs Lisp,
for example, is very good at this. Are there any CPAN modules that
would do this? I know only of the one that ties a file to an array,
but that doesn't really have a logical cursor.

Ted
 
M

Matija Papec

X-Ftn-To: Ted Zlatanov

Ted Zlatanov said:
I think a module would be OK to answer this challenge, as long as
it's not written just to answer this challenge, but is really useful :)

Well, one could start with building methods specific to cfengine,

my $e = Text::pEditor->open($file);
$e->LocateLineMatching(".*. /etc/init.d/functions.*");
$e->IncrementPointer(-3);
....

and extend things later (if there is a strong will :))
In particular, Perl does not have good facilities for a logical
line-based cursor for insertion and deletion of lines. Emacs Lisp,
for example, is very good at this. Are there any CPAN modules that
would do this? I know only of the one that ties a file to an array,
but that doesn't really have a logical cursor.

I did some search on CPAN but no luck, maybe I didn't look hard enough.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top