Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Matching against $_ behaves differently than matching against a named scalar?

by stevieb (Canon)
on Apr 20, 2020 at 16:44 UTC ( [id://11115837]=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11115837]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (10)
As of 2024-04-18 10:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found