Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CAF::FileEditor: add a replace() variant where the newstring can reference the linere pattern #125

Open
jouvin opened this issue Dec 30, 2015 · 1 comment

Comments

@jouvin
Copy link
Contributor

jouvin commented Dec 30, 2015

As explained in https://github.com/quattor/configuration-modules-grid/pull/86/files#diff-1ed12f02554370943ea6fe0a5944b63f, there are circumstances where you want to match lines in a file using a regexp and to modify something in the matched lines but without knowing the exact contents of the matched lines. An example is when you want to comment some lines in a configuration file that matches a certain pattern. For this you need to be able to pass variables like $1 or $+{groupname} in the newline argument and get them evaluated. I don't have the feeling that the current replace() allows this...

@stdweird
Copy link
Member

seems like a useful addition.
one question/proposal: the newvalue is currently a string, we could make a it an arrayref such that the matched position groups are zipped (like python zip) with the newvalue arrayref, like
$linere = qr{a(B)c(D)} and newvalue ['x', 'y', 'z'] would retrun newvalue string xByDz
(in your example, this would become linre = qr{^(.*the line i want.*)$} and newvalue ['#',''])

does that seem userfriendly enough? (keeping newvalue as a string and eval'ing it would be another solution, but that sounds dangerous to do)

some remarks
a. word of caution on $+{groupname}, that is not supported in perl 5.8.8 (ie EL5)
b. you can already insert characters in text on arbitrary locations using fileeditor, but it's not userfriendly

  1. locate postition(s) you want to insert https://github.com/quattor/CAF/blob/master/src/test/perl/test-caffileeditor_positions.t#L49
  2. seek to those locations and insert text with non-matching regexpos and SEEK_CUR https://github.com/quattor/CAF/blob/master/src/test/perl/test-caffileeditor_add_or_replace.t#L84

@jouvin jouvin added this to the 16.2 milestone Jan 12, 2016
@jrha jrha removed this from the 16.2 milestone Feb 12, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants