Matt Diephouse writes:
> Is it possible to assign to an array slice?
>   @array[0..4] = ( 0..4 ); # splice @array, 0, 5, 0..4

Of course.  You could in Perl 5, right?

> If so (and I'm hoping it is), is there an equivalent of Ruby's `[]=`
> method? (Is there a way to define this behavior within my own
> array-like classes?)

Well, when you really get down to it, []= would be implementing the []
method to return something tied.  A lot of things do that.  We'll likely
provide roles to make doing that not as prohibitive conceptually.

> Can I use slice notation when dealing with strings?
>    say $string[-1]; # say substr($string, -1);
>    $string[0..2] = "Hello";

No.  I'm pretty sure that's the Right Thing, too.  First, the "sixth
element" in a string depends on how you're defining "element": byte,
codepoint, grapheme, etc.  Second, if you think you're using an array,
and it's actually a string (even ""), then you'll be missing an error
(though I understand that this is a weak argument).  Third, we already
have substr.

Perhaps as an alternative to substr, Str could provide a slice method:

    $string.slice(0..2) = "Hello";

Which could massage everything to work as if it were an array of chars,
whatever the heck chars are.


Reply via email to