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:
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.
|
|---|
| 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 | |
by 1nickt (Canon) on Apr 26, 2017 at 03:27 UTC | |
by huck (Prior) on Apr 26, 2017 at 03:32 UTC | |
by victorz22 (Sexton) on Apr 26, 2017 at 06:11 UTC | |
by huck (Prior) on Apr 26, 2017 at 06:56 UTC | |
by 1nickt (Canon) on Apr 26, 2017 at 15:23 UTC |