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:
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; )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
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.
In reply to Re: Replacing an entire line if a substring is found
by 1nickt
in thread Replacing an entire line if a substring is found
by victorz22
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |