Front page | perl.perl6.stdlib |
Postings from September 2002
Re: Perl 6 index
Thread Previous
From:
William R Ward
Date:
September 21, 2002 02:03
Subject:
Re: Perl 6 index
Message ID:
m2u1kkxv1s.fsf@komodo.home.wards.net
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.
ajs@ajs.com (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 <ajs@ajs.com>
> http://www.ajs.com/~ajs
>
--
William R Ward bill@wards.net 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
Thread Previous
-
Perl 6 index
by Aaron Sherman
-
Re: Perl 6 index
by William R Ward