in reply to Replacing an entire line if a substring is found

Well, you don't say what your problem is, but you'll have a hard time reading the DATA section like that. The correct token name is __DATA__, not ___DATA___.

Try this:

use strict; use warnings; use feature 'say'; while ( my $line = <DATA> ) { chomp $line; say replace( $line, 'SUBSTR', 'FOO' ); } sub replace { my ( $input, $wanted, $replacement ) = @_; return $input =~ s/$wanted/$replacement/gr; } __DATA__ path/to/some/file path/to/some/other/file path/to/SUBSTR/file #replace entire line if SUBSTRING is found path/to/file
Note the use of the /r modifier to the substitution so that it returns the result rather than the count of matches. ( This requires Perl version 5.14 or newer. If your Perl installation is older than that, use $input =~ s/$wanted/$replacement/g; return $input; )

Output:

path/to/some/file path/to/some/other/file path/to/FOO/file #replace entire line if FOOING is found path/to/file

Hope this helps (but I am only guessing since you didn't pose a question)!

Edit: changed 'filehandle' to 'token', thanks shmem.

The way forward always starts with a minimal test.

Replies are listed 'Best First'.
Re^2: Replacing an entire line if a substring is found
by huck (Prior) on Apr 26, 2017 at 03:12 UTC

    While you made good points i suspect this is more what he was after

    use strict; use warnings; use feature 'say'; while ( my $line = <DATA> ) { chomp $line; say replace( $line, 'SUBSTR', 'FOO' ); } sub replace { my ( $input, $wanted, $replacement ) = @_; if ($input =~ m/$wanted/) { return $replacement;} else { return $input; } } __DATA__ path/to/some/file path/to/some/other/file path/to/SUBSTR/file #replace entire line if SUBSTRING is found path/to/file
    Result
    path/to/some/file path/to/some/other/file FOO path/to/file

      Lol, oh, is that what "entire line" means? I'm on a plane using my mobile, that's my excuse 😚

      The way forward always starts with a minimal test.

        O2 deprivation, thats it!

      Thanks for the help but how would i set up that while statement to iterate through a scalar variable rather than a file handle? I have all my data stored in a scalar variable called $fileContent. So in other words how would i set up the while block using $fileContent rather than <DATA>

      #This is how I read in the file my $fileContent = do { open(my $fileHandle, $inputFile) or die "Could not open file '$inp +utFile' $!"; local $/; <$fileHandle>; };

        Any particular reason it be to be in a single string? an array would be much easier to process and involve less character handling while doing the replacements.

        use strict; use warnings; use feature 'say'; my @lines=<DATA>; for my $line (@lines) { chomp $line; if (index($line, 'SUBSTR') > -1 ) {$line='FOO'; } } #for my $line (@lines) { say $line; } my $fileContent=join("\n",@lines); say $fileContent; __DATA__ path/to/some/file path/to/some/other/file path/to/SUBSTR/file #replace entire line if SUBSTRING is found path/to/file

        I imagine you have seen and copied syntax like:

        my $data = do { local $/, <DATA> };
        ... which Monks use a lot around here to demonstrate solutions using data contained in the script.

        But in your case, if you are interested in handling the lines one at a time, and they exist in a separate file, there's no good reason to combine them all in a scalar.

        open my $fh, '<', $filename or die $!; while ( <$fh> ) { ... # process one line at a time }

        Hope this helps!


        The way forward always starts with a minimal test.