victorz22 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, I am trying to create a function that will replace an entire line in a scalar variable if a certain sub string is found. The variable contains a bunch of paths to different files and what I want to do is replace the entire line if that sub string is found. Thank you monks for your endless wisdom!

The input data looks kinda like this ___DATA___ path/to/some/file path/to/some/other/file path/to/SUBTSTRING/file #replace entire line if SUBSTRING is found path/to/file
sub scanForSubstringReplaceLine{ my($inputText, $subStringToScan, $lineReplacement) = @_; my $replacedText; $inputText =~ s/$subStringToScan/$lineReplacement/g; $replacedText = $inputText; return $replacedText; }

Replies are listed 'Best First'.
Re: Replacing an entire line if a substring is found
by 1nickt (Canon) on Apr 26, 2017 at 02:37 UTC

    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.

      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.

        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>; };
Re: Replacing an entire line if a substring is found
by kcott (Archbishop) on Apr 26, 2017 at 03:36 UTC

    G'day victorz22,

    When you simply want to test for a substring, use index: you'll find it's measurably faster than a regex. [You can use Benchmark to measure it.]

    Here's the technique you need:

    #!/usr/bin/env perl use strict; use warnings; while (<DATA>) { print index($_, 'SUBSTR') > -1 ? "REPLACEMENT\n" : $_; } __DATA__ path/to/some/file path/to/some/other/file path/to/SUBTSTRING/file #replace entire line if SUBSTRING is found path/to/file

    Output:

    path/to/some/file path/to/some/other/file REPLACEMENT path/to/file

    — Ken

Re: Replacing an entire line if a substring is found
by Anonymous Monk on Apr 26, 2017 at 02:22 UTC