1

I have an unusual requirement. I have a big config /perl file in which I would like to change the value of one variable before my run. To avoid manually finding the variable and changing it's value, I would like to write a perl script to change the name of the variable. Is that possible to do this without parsing every single line of big perl file, creating a temporary copy and overwriting old file.

xaxxon
  • 19,189
  • 5
  • 50
  • 80
Raghav
  • 766
  • 16
  • 24
  • 2
    Why not pass arguments to the script? ie [`Getopt::Long`](http://perldoc.perl.org/Getopt/Long.html) – kjprice Aug 06 '13 at 21:26
  • Why is your post about changing the name of a variable but the title is about changing the value of a variable? – mob Aug 06 '13 at 21:28
  • @kjprice - The big perl file is more like a configuration file. Not exactly a perl file. Sorry I should have mentioned it – Raghav Aug 06 '13 at 21:31
  • @mob - It is changing the value of a variable. I have rephrased in a better way. Thanks! – Raghav Aug 06 '13 at 21:31
  • 3
    For a dozen different ways to do this, see http://stackoverflow.com/a/2308092/176646 – ThisSuitIsBlackNot Aug 06 '13 at 21:33

1 Answers1

1

Something is parsing this file at some point, right? Give it a list of things to substitute and you can have it only do the substitutions when it needs it. This avoids a big pre-startup overhead and if the config file is sparsely used, will result in a faster overall run.

So just make the thing reading it look for certain patterns to substitute in and a file (or passed in on the command line or environment variables, or...) for the values it should use and go from there.

If you don't have control over the parser, then there's not much to do. You could one-time pre-process the config file to determine EXACTLY where the substitutions need to be and write a faster processor, since it won't have to do any string parsing for regular expressions, just moving a bunch of bytes as fast as your computer can move them to the new file with the substitutions in place.

xaxxon
  • 19,189
  • 5
  • 50
  • 80