in reply to A Better Way

Here's a less cumbersome way :-)

use strict; use warnings; use Tie::File; tie my @xfil, 'Tie::File', 'var.txt' or die $!; @xfil = map { s/the line to be replaced/this is the new line/; $_ } @x +fil; untie @xfil;

-- Ken

Replies are listed 'Best First'.
Re^2: A Better Way
by toniax (Scribe) on Nov 04, 2010 at 17:34 UTC
    Hi Ken Thanks for your reply and the info on the mod tie One more question. Does that replace all of the instances or just the first one it gets to ? Thanks again

      It operates on every line. On each line, there will be, at most, one substitution. If you want multiple substitutions on a line, add the 'g' modifier (see perlre for details) like this:

      s/the line to be replaced/this is the new line/g

      -- Ken

        Thanks Ken for your below link to tie and your answers. The reason I created my program in the above way Is so it only replaces the first line in the loop not every line it loop . It seems crazy but I need this in the program I am creating.
        Hi Ken, Your example of tie was awesome . I figured out how to fix my code with your example. I did not use tie But you really helped me and I sure do appreciate it
        Thanks Ken for your below link to tie and your answers. The reason I created my program in the above way Is so it only replaces the first line in the loop not every line it loop . It seems crazy but I need this in the program I am creating.
        Thanks ken for the code . I really appreciate it.