Is there a way to do this without getting an infinite loop?
while((my $var) = $string =~ /regexline(.+?)end/g) {
print $var;
}
This results in an infinite loop, probably because the assigning of a var directly from a regex inside the while returns “true” every time?
I know I can do this:
while($string =~ /regexline(.+?)end/g) {
my $var = $1;
print $var;
}
But I was hoping I could save a line. Is there a regex modifier I can use or something like that?
(Also, what is this notation/trick actually called, if I want to search for it:
(my $var) = $string =~ /regex/;
Thanks!!
Yes. Use a foreach() instead of a while() loop:
It is called a match in list context. It is described in “perldoc perlop”: