in reply to Inserting Text into Files within a Directory

Maybe your open is failing because you're not looking in $dir? Try this:

open(OLD,"<$dir/$file") or die "can't open $dir/$file: $!";

As for your substitutions, here's one way to do it (note: untested code):

my $data = do {local $/; <OLD>}; # slurp the whole file in to $data my @replace_list = ('','Notes:','Director:','Actors:'); while ($data =~ s/(?<=$search)/shift @replace_list/egs) {};

BTW, I can understand doing it the quick-and-dirty way with s///, but if this is more than just a one-off thing, you might consider doing it with XML::Parser.

-b

Replies are listed 'Best First'.
Re^2: Inserting Text into Files within a Directory
by LF (Initiate) on Aug 24, 2004 at 15:03 UTC
    How do I create a temporary file? It cannot open $new because it is not defined...

      Your best bet is to step through your code with the debugger (run it with perl -d) and see what the values of $dir and $file are before the call to open. See perldebug if you're unfamiliar with the debugger.

      Also, you have use warnings and use strict commented out in your program. Uncomment them and see what happens.

      -b

      First of all, don't ask a new question by editing your previous one and replacing the text. It makes my above reply look completely irrelevant.

      As for creating a temporary file, there are lots of ways to do it. One that might work for you is to just add an extention on the name of the existing file:

      open(NEW,">$dir/$file.new") or die "Can't open $dir/$file.new: $!"

      -b

        Sorry, I'm new at this..I just updated my entire post again(with new code, etc.). Should I just create a completely new post when updating my questions?