On Sat Sep 04 22:51:55 2010, RJBS wrote:
Show quoted text> The current behavior of reverse is unfortunate. We've been making
> slow progress to make
> Scalar items act like one-element lists. That makes having distinct
> behavior for reverse
> annoying. I'd rather do something to fix that than make it worse with
> repeat.
Is this something like the single element lists from Perl 6 that
magically act like scalars if you use them as scalars and lists if you
use them as lists? I forget what they're called, but I liked the idea
and it solved a bunch of interface issues. A cleverly overloaded
blessed ARRAY ref seemed to do the trick, unless you check if its blessed.
Show quoted text> "the team" mostly agrees. So we want this:
>
> @array->repeat(3); # qw(a b a b a b)
>
> $string->repeat(3); # qw(foo foo foo);
>
> $string->repeat_str(3); # foofoofoo
>
> The problem is that we haven't found any method names we like for
> repeat and repeat_str.
I'd take advantage of having methods, rather than catch all keywords,
and have ARRAY::repeat return an array and SCALAR::repeat return a scalar.
@array->repeat(3); qw(a b a b);
$string->repeat(3); "foofoofoo";
[$string]->repeat(3); qw(foo foo foo);
I don't think the single-element list solution applies here because this
isn't a "sometimes I expect a list and sometimes I expect a scalar"
situation. I have been wanting it for things like ARRAY->pop.
my $item = @array->pop;
my @items = @array->pop(3);
Also, you'll find you want to return refs. Why? Flattened arrays don't
chain and you wind up having to mix metaphors.
# If it returns a flat array
say join ", ", @array->repeat(3);
vs
# If it returns an array ref
@array->repeat(3)->join(", ")->say;