In article <[EMAIL PROTECTED],
 [EMAIL PROTECTED] (Chris McMahon) writes:
>
>Hello...=20
>       I need to add some boilerplate text to the very top of about
>17,000 files.  I can get to the files easily enough (File::Find), but
>there's a special syntax to edit a file in place, and it's not specified
>in Programming Perl, and it's a tough search on Google and PerlMonks. =20
>       Could anyone on the list supply a link or a fast overview of
>editing files in place?  I think I need to use the "-i" flag, but I'm
>not sure I know what I'm doing...

If you want to activate the -i flag from inside a program, the
instructions for doing that are somewhat rarer.  Basically, you
set $^I to the value you'd give to the -i flag, and then whatever
you print while looping over <> replaces your files.  Here's one
way to do what you want to do:

use File::Find;

# Make sure @ARGV is empty at this point
find(\&finder, @starting_directories);
$^I = '.bak';
{
  local $/;  # So we get a whole file at a time
  while (<>) {
   print $BOILERPLATE, $_;
  }
}

sub finder {
  if (I_want_this_file) { push @ARGV, $File::Find::name }
}


Yes, you can do it without either populating the whole array
before processing any of the files, or without reading the
whole contents of each file in at once; I just wanted to 
keep this as simple as possible.

-- 
Peter Scott
http://www.perldebugged.com/
*** NEW *** http://www.perlmedic.com/

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to