Up to now, if I wanted to group together several regex's within an if statement, I did it this way:
my $data =...
if ($data =~ m/regex/ && $data =~ m/secondregex/) {...}
Is there a shortcut (and I'm sure there is; it's Perl!) to avoid repeating $data, something like:
if ($data =~ m/regex/ && m/secondregex/) {..}
??
Use the default variable $_
like this:
$_ = $data;
if ( m/regex/ && m/secondregex/ ) {..}
as regular expressions act on $_
by default (as many other things in Perl).
Just be certain that you are not in a block where $_ is automatically populated and you will need to use it later in this block. Once it's overwritten it's gone.
for ($data) {
if (/regex/ && /secondregex/) {...}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With