What would be the benefit?
None that I can think of. Just showing that TIMTOWTDI.
It makes needless and baseless assumption ...
Agreed. The OP asks to split on first occurance of an equals sign. It doesn't say what to do if there's more than two equals signs so assumptions are invalid, even the assumption that splitting on first occurance is still the requirement when there are three or more of them.
It's less readable and/or maintainable ...
my ($key, $val) = 'MYKEY=MYVAL=2' =~ /^([^=]*)=(.*)/s;
my ($key, $val) = split m{=(?=.*=)}, q{MYKEY=MYVAL=2};
Six of one, a half dozen of the other, I'd say.
It's probably slower ...
Yes, about 8% slower if my benchmark code is correct
use strict;
use warnings;
use Benchmark q{cmpthese};
my $rsMatch = sub
{
my ($key, $val) = q{MYKEY=MYVAL=2} =~ /^([^=]*)=(.*)/s;
return [$key, $val];
};
my $rsSplit = sub
{
my ($key, $val) = split m{=(?=.*=)}, q{MYKEY=MYVAL=2};
return [$key, $val];
};
cmpthese(-5,
{
Match => $rsMatch,
Split => $rsSplit
});
Rate Split Match
Split 26064/s -- -8%
Match 28250/s 8% --
Cheers, JohnGG |