There’s an example snippet in Mail::POP3Client in which theres a piece of syntax that I don’t understand why or how it’s working:
foreach ( $pop->Head( $i ) ) {
/^(From|Subject):\s+/i and print $_, "\n";
}
The regex bit in particular. $_ remains the same after that line but only the match is printed.
An additional question; How could I assign the match of that regex to a scalar of my own so I can use that instead of just print it?
This is actually pretty tricky. What it’s doing is making use of perl’s short circuiting feature to make a conditional statement. it is the same as saying this.
It works because perl stops evaluating and statements after something evaluates to 0. and unless otherwise specified a regex in the form
/regex/instead of$somevar =~ /regex/will apply the regex to the default variable,$_you can store it like this
or you could use a capture group
which will store the whole thing into
$1