in reply to Masters of Loops and Filehandles

pbyfire:

Here's a quickie example that should get you on your way:

#!/usr/bin/perl my $Gibberish=<<EOGibberish; This is a sentence. It's not a particularly great sentence, but it's a sentence nonetheless. It would suck if someone accidentally changed it! EOGibberish use strict; use warnings; use autodie; my $search; open my $FH, '<', $0; binmode $FH; seek $FH, 55, 0; read $FH, $search, 8; close $FH; $Gibberish =~ s/$search/XXXXXXXX/g; print $Gibberish;

When I run it here, I get:

marco@Boink:~ $ perl splorch.pl This is a XXXXXXXX. It's not a particularly great XXXXXXXX, but it's a XXXXXXXX nonetheless. It would suck if someone accidentally changed it!

...roboticus

When your only tool is a hammer, all problems look like your thumb.

Replies are listed 'Best First'.
Re^2: Masters of Loops and Filehandles
by pbyfire (Novice) on May 10, 2012 at 14:40 UTC

    roboticus - Thanks for your reply and the code. I will see if I can work this into my loop since I need to process several files this way. I have already succeeded in replacing the pattern at a given location within a loop but replacing it globally doesnt seem to work even using variations of the $Gibberish sed line included in your example. I did attach an example of my existing code in this thread if you care to review it below. Thanks Again - pbyfire

      pbyfire:

      You can slurp the entire file into a variable by localizing the $/ variable:

      { local $/; $Gibberish = <$FH>; }

      If you do that after the open, and before the binmode & seek, you should be able to do a global search & replace on the entire file. Then just put it in a loop...

      ...roboticus

      When your only tool is a hammer, all problems look like your thumb.