I was trying to duplicate what the one liner does, so I wanted to keep it as a string.
I finally figured it out: preserve $1 and use "if" instead of "while":
#!/usr/bin/perl # full script the following one liner: # $ echo -e "abc\ndef\nghi\n" | perl -wlne '! /def/ and print "$_";' # abc # ghi use strict; use warnings; use diagnostics; my $C; my $B = ""; my $A = "abcdef fhijkl mnopqr "; while ( $A =~ m{(.+)}g ) { $B .= "$1\n" unless $1 =~ /ijk/; # $B .= "$1\n" unless $1 =~ /xyz/; } print "\$B = \n$B\n"; $B = ""; while ( $A =~ m{(.+)}g ) { $C = $1; $B .= "$C\n" if $C =~ /ijk/; } print "\n\$B = \n$B\n";
$B =
abcdef
mnopqr
$B =
fhijkl
Thank you for all the help! -T
In reply to Re^3: Regex match on implicit default variable ($_) in a script, not a one liner
by Todd Chester
in thread Regex match on implicit default variable ($_) in a script, not a one liner
by Todd Chester
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |