Subject: | sub charAt should^B^B^B^B^B^Bmust use substr |
sub charAt should^B^B^B^B^B^Bmust use substr
sub charAt {
my ($string, $n) = @_;
unless ( defined($n) ) {
croak "Usage: chartAt(n)";
}
return substr $string, $n, 1;
}
Why?
1 -- that's what it's for (and it's more efficient )
2 -- If the index is out of bounds, and warnings are on, you get
"substr outside of string ..."
3 -- what's next, map in void context? ;)
Also, sub indexOf has the following statement
my $rv = CORE::index($$string, $substring, $start||0);
if ( $rv < 0 ) {
return -1;
}
return $rv;
This is utterly ridiculous.
$rv can only be -1 .. length($$string) - 1
perldoc perldoc
perldoc -f index
perldoc -f substr