I think you meant
local $/; my $file = <IN>; while( $file=~/$regex/gm ) { ... }
There are three differences:
The first snippet reads the file a line at a time, whereas the above snippet reads the whole file at once. The latter uses more memory, but it might be a bit faster. In practice, this difference isn't usually noticeable.
The first snippet will only find matches that occur within the span of one line, whereas the above snippet might find matches that span multiple lines.
Using the first snippet, you can group matches by line. You cannot do so with the above snippet.
Update: Added third difference.
In reply to Re: Advantage of while(<>){while()}?
by ikegami
in thread Advantage of while(<>){while()}?
by Argel
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |