in reply to Replacing text inside lots of files

: platively new to real Perl programming, and I'm sure, as always, there are many ways, but one way:
#!/usr/bin/perl -wT use strict; #my $filename is somehow parsed before this $filename =~ /^(\w+)\.\w+$/; my $replacewith = $1; open (FILE, "<$filename") || die ("Error: $!"); my $line = <FILE>; chomp $line; my $toreplace = substr($line,-10); close FILE; $line =~ s/$toreplace/$replacewith/;
You did not mention whether the new file name will be 10 characters.

Update
Thanks hena for the elegant rewriting of the new file. Forgot that part!

Replies are listed 'Best First'.
Re: Re: Replacing text inside lots of files
by Taulmarill (Deacon) on Oct 28, 2003 at 14:37 UTC
    I would prefere the solution from Hena, because yours can fail if the last 10 chars repeat anywhere in the string, s/// takes the first match. s/.{10}$/$filename/ is safer here.
      Thanks Taulmarill. After reviewing hena's code, I see the more accurate regexp. But thanks to hena for that regexp--that's going in my arsenal of cool replacement regexps.