Re: Perl 6 index

[email protected] (William R Ward) 20 Sep 2002 13:31:11 -0700
Newsgroups perl.perl6.stdlib
Message-ID <[email protected]>
I don't like changing the semantics without changing the syntax.
There's too much of that in perl6 already.  But shouldn't index() be a
method of the string object anyway?

--Bill.


[email protected] (Aaron Sherman) writes:
> Something that has long bothered me in C and Perl is the return from
> index. -1 is such a non-answer to me.
> 
> In Perl 6, I was wondering if it would make sense for functions that
> return a count or index to set the truth property as well. For example,
> allowing:
> 
> 	if index($subject,"**SPAM**") {
> 		warn "SpamAssassin doesn't like you :)";
> 	}
> 
> There are a lot of functions that do what index does or which return a
> success count for list of actions (e.g. unlink or chmod). Should all of
> these break with Perl 5's notion of boolean success and use Perl 6's
> model?
> 
> Here's a sample of some of those:
> 
>     # Slow matching for example purposes
>     sub index($string, $substr, int $pos //= 0) {
>     	my int $subl = length($substr);
>     	my int $strl = length($string);
>     	for(int $i = $pos; $i+$subl <= $strl; $i++) {
>     		return $i but true if substr($string,$i,$subl) eq $substr;
>     	}
>     	return -1 but false;
>     }
>     sub all_or_false(&code, @items) {
>     	my $ok = 0;
>     	for @items -> $_ {
>     		$ok++ if code($_);
>     	}
>     	if $ok == @items {
>     		return $ok but true;
>     	} else {
>     		return $ok but false;
>     	}
>     }
>     sub chmod(int $mode, *@paths){
>     	return all_or_false {syschmod($_,$mode)} @paths;
>     }
>     # ... also do chown, utime, kill, utime and unlink this way
> 
> -- 
> Aaron Sherman <[email protected]>
> http://www.ajs.com/~ajs
> 

-- 
William R Ward            [email protected]          http://www.wards.net/~bill/
-----------------------------------------------------------------------------
"Beware the leader who bangs the drums of war in order to whip the citizenry
into a patriotic fervour, for patriotism is indeed a double-edged sword."
        - Julius Caesar