in reply to Matching against $_ behaves differently than matching against a named scalar?

choroba answered the why, I'll give a different way to do things.

Instead of checking the condition of the variables after the fact, do it before hand:

use warnings; use strict; open my $fh, '<', 'text.txt' or die $!; while (<$fh>) { if (/^([^ ]+) ([^ ]+)/) { # Skip this if $1 and $2 weren't populated print "$1 $2\n"; } }

Also note the die() statement if the file can't be opened, and the use of 3-arg open().

One last thing... in your former example, you're missing the + in the regex.