This and other RFCs are available on the web at
  http://dev.perl.org/rfc/

=head1 TITLE

rindex and index should return true/false values

=head1 VERSION

  Maintainer: Nathan Torkington <[EMAIL PROTECTED]>
  Date: Sep 12 2000
  Last Modified: Sep 15 2000
  Mailing List: [EMAIL PROTECTED]
  Number: 213
  Version: 2
  Status: Developing

=head1 ABSTRACT

index() and rindex() should return C<undef> if their
second argument is not a substring of their first
argument.  If the substring occurs at the start of
the first argument, they should return "0 but true".

=head1 CHANGES

 * added "0 but true" suggestion

=head1 DESCRIPTION

In perl5, index() and rindex() return -1 if the
substring isn't found.  This seems out of step with
the rest of Perl's functions, which return C<undef>
on error.  I propose changing index() and rindex()
to return C<undef> if the substring isn't found.

This would also cause warnings to be issued when
programmers use the results of index() or rindex()
assuming the substring was found.

This suggestion doesn't rely on RFC 53, "Built-ins: Merge and
generalize C<index> and C<rindex>", and works regardless
of whether 53 is accepted or not.

When the substring occurs at the start of the larger
string, the functions should return "0 but true".  This
is exempt from "isn't numeric" warnings, and gives them
useful boolean return values:

  if (index($big_string, $little_string)) {
    print "$little_string is in $big_string";
  }

without breaking their existing numeric uses.

=head1 IMPLEMENTATION

The perl526 translator could turn index($a,$b) calls into

  do { my $tmp = index($a,$b);
       if (defined($tmp)) { 0 + $tmp }
       else               { -1       }
  }

=head1 REFERENCES

RFC 53: Built-ins: Merge and generalize C<index> and C<rindex>

perlfunc manpage for information on index() and rindex()

Reply via email to