Glenn Linderman wrote:
> 
> The idea of a _normal_ situation being considered exceptional is raised when the
> code written inappropriately handles some of the normal return values.

You would throw exceptions at the problem of bad coding practice.  
I think it's better to correct the bad coding practice.



> > >       $foo = "flabergasted";
> > >       substr($foo, index($foo, 'abc'), 20);   # Returns undef
> 
> contains errors.  Clearly the current index, or even index modified to return
> undef, doesn't produce the desired results.  The inappropriate value of -1 (or
> undef) passed as the 2nd parameter to substr will produce erroneous results.

Right. But this is not so much an argument for making index throw, as for 
encouraging programmers to write good code, i.e.

        $foo = "flabergasted";
        if ( defined my $i = index($foo, 'abc') ) {
                substr( $foo, $i, 20 );
        }
        else {
                # do what you want with this condition.
        }

The whole point, IMHO, is that index() should return a value which cannot
be used as an index.  -1 clearly does not meet this criterion.

If it returns undef, that can be used since it will get coerced to 0, but
at least it will elicit a warning from perl.  Perhaps under some kind of
very-strict it will elicit an error instead.

-- 
John Porter

        We're building the house of the future together.

Reply via email to