开发者

Why doesn't Perl support the normal [] operator to index a string?

Why doesn't Perl support the normal [] operator to index a string?

Almost all major programming languages support this开发者_如何学C operator, especially the other two 'P': Python and PHP. Moreover, I do think it should be easy to implement this little syntax. Also, as the philosophy of the Perl programming language -- as lazy as we could, so why do we bother to use a function to index a string?

Will Perl 6 support this syntax?


I like all the answers so far, but the real answer is, "because Larry wants it that way". Really. Larry came up with a set of idioms and tools that worked for him, and he shared that with us in the form of Perl. If you don't think the way Larry thinks, then there are plenty of other tools to use. We don't need the whole world using Perl... just the people who "get it" the way Larry does.


Using [] to index into a string is a side effect of the way many programming languages treat strings: as arrays of characters (or wide characters, in the case of Unicode). In Perl, strings are first-class entities. Perl provides a wealth of ways for working with whole strings as a single value. If you're trying to index into a string you're probably doing something wrong. (e.g. writing C in Perl rather than using Perl idioms.) For the cases where you really do need to index into a string, use substr.


Do you want to index by bytes, characters, or graphemes?

This is why in Perl 6 length is "banned", Instead you use one of the following:

  • bytes
    Exactly one byte at a time
  • chars
    Depending on the source text this can be a single byte, or several bytes.
  • graphs
    This is similar to chars, but combines multiple "combining" characters together.

If you really want it you can do something similar, using split.

( split '', $str )[$index];

It is probably better to just use substr though.

substr $str, $index, 1;


Perl has a way to index a string with index or substr. It supports the operation. That it does it with another syntax shouldn't matter. There's a reason we have more than one programming language. :)

I wouldn't say that [] is a "normal" operator. I'm sure people can list many languages that don't do that either.


If you really want to treat scalars as objects, you can use autobox.

I don't use autobox, but this should work:

my $indexed = ('foo'->list)[1];

autobox has hooks for defining objects to use for wrapping various data types.

So, if you really, really want it, you should be able to use autobox to create your own string class that will allow code like this:

my $indexed = 'foo'->[3];

So, I think the answer to your question, "Why doesn't Perl have [] syntax for string indexing?" is "Nobody has wanted it enough to implement it."

As to Perl 6, I haven't followed closely enough to give an answer beyond, "If it isn't present and you really want it, you will be able to add it yourself."


In Perl, strings are scalars and thus are not subscriptable by default. You can use functions like substr() or index() to access specific characters in a string.

Unless Perl 6 breaks with this concept by changing strings to an array of char's I don't think there will be any changes to this.


What's the name of the string? It's scalar, so the sigil will obviously be $. The rest of it follows standard variable naming standards; let's say $abc.

my $abc = 'A string';

As sigils signify contexts of the expression, and are not a part of the name, we have a collision.

my $def = $abc[2];

is not the 3rd letter of the scalar $abc, but the third element from an array--sharing the same symbol (but with a different sigil) : @abc.

So that expression, which was probably designed early for script-like symbol resolution, already has a meaning assigned to it.

Of course, as Brad's answer points out, there can only be a meaning to that if we make implicit assumptions as to what makes a part of the string an "item" in the "list". The more encodings you have to use, the worse those default assumptions work.

You might find a syntax you prefer more using autobox:

$string->ch( 2 );

(You would have to write ch yourself.) But that is necessarily more verbose than simply putting bracket's onto strings.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜