The regex will (can only) return valid matches. It cannot return undef. You should not have to use grep.
Thus, it is either your adaption of the code, or your test that is wrong.
The only way you can generate undef's with your first implementation, is if $max is greater than the number of urls within the string, in which case the slice @captures[ 0 .. $max-1 ] will generate undefs.
Instead of greping out the extraneous undef's; don't generate them in the first place:
sub _browseruk_recognize_limited_urls {
my ($input, $max) = @_;
my @captures = $input =~ m[(https?://.+?)(?:,(?=http)|$)]g;
return [ @captures[ 0 .. @captures < $max ? $#captures : $max -1 ]
+];
}
But even that is rather silly. You have an array but you want to return an array ref (for some reason?), so you slice the array into a list, wrap it in another (anonymous) array and return a reference to that.
If the reason for returning a reference is "efficiency"; you completely blew any potential gain by splicing and listing (never mind the redundant greping). Better to simply return that list and assign it to an array in the caller.
But, if you really need a reference, then adjust the size of the array you already have and then return a reference to that: sub _browseruk_recognize_limited_urls {
my ($input, $max) = @_;
my @captures = $input =~ m[(https?://.+?)(?:,(?=http)|$)]g;
$#captures = $max -1 if @captures >= $max; ## Adjust size if necess
+ary
return \@captures.
}
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
|