Sigils and Definite Articles

tomhukins on 2005-02-06T19:23:09

Once upon a time, when I started using arrays in Perl, I found myself wanting to write things like my $house = @street[87]. It took me a while to understand that I should use a scalar, $street[87], instead of an array slice, @street[87]. I felt that as I was dealing with an array, I should use the @ sigil.

I've noticed others making the same mistake, and I've had trouble helping others learn the difference between @ and $ in this context. In English, we might translate my $house = $street[87] to my house is the 87th on the street. Or rather the 88th house if we count from zero, but that's another story for another day.

So, the $ in $street shares its meaning with the word the in English. If I also own the house next door, I should use an array slice to say my @houses = @street[87, 89]. Or in English, my houses are the 87th and 89th on the street. The @ in @street also translates to the. This doesn't help me understand the problem.

Translating this to French, or any language that inflects definite articles, provides a clearer understanding. Unless I'm horribly out of practice, we use la 87ème for the 87th and les 87ème et 89ème for the 87th and 89th. Just as French uses la (or le) for the singular and les for the plural, Perl uses $ for the singular and @ for the plural.

I've understood the linguistic concept of inflecting pronouns for over fifteen years, and the difference between Perl's array slices and scalars for almost ten, yet I'd never equated the two. Now that I've written this down it all seems so obvious.

I wonder whether native French speakers find this Perl idiom easier to grasp, and whether speakers of languages that don't use definite articles would prefer a programming language that lacks sigils.


It's only a foreign language

BooK on 2005-02-06T21:22:26

Well, I personaly feel lucky that the programming languages I use and my native language have very little in common.

English is a foreign language, Perl is a foreign language, C is a foreign language, Befunge is an alien language (from outer space)... I never try to "speak" Perl as I speak English. And I never ever translate Perl to French (or the other way around).

Re:It's only a foreign language

Maddingue on 2005-02-06T23:09:59

I second BooK in that I don't try to write Perl code as I would write English. It's only a programming language and not a human language. But I admit I often choose the name of functions and variables so that one can read what they do and what they are. However I didn't even thought of reading the sigils that way. But it might be useful for teaching the difference between $array[1] and @array[1,2,3] to beginners.

Definite Articles - Linguistics

n1vux on 2005-02-07T04:13:20

Hmm, Larry Wall is a linguist, this could explain the sigils.

Of course, Perl6 reverses it to @foo[87].

Bill

I'm wondering...

phillup on 2005-02-07T18:15:14

if you also want to say something like:
my $house = %house{87};
when the data is stored in a hash?

Here is why I ask...

I also had the problem (at first) with the array slice. I thought initially that it was a sigil issue and an issue with semantics. (As you have explained)

But, I noticed that I did not have the issue with a hash. It seemed to me that if the sigil was causing me to have a semantic problem that the problem would carry to other cases... but, in my case... it did not.

So, I thought about it some more. Then, I realized that I didn't have the issue with the hash because I would get an immediate error if I tried to do it with the hash.

But, I was NOT getting that kind of feedback with the array... because sometimes the thing "Just Works" like you want it to. Even tho you asked for something else... what you go was useful.

So, the "problem" got reinforced.

In the end, I solved it by thinking first about WHAT I wanted... I wanted a single scalar. Those are denoted by '$'. Next I think... OK, what is the name of the variable holding the data... then I'm done if the variable is in a scalar.

Or, I mentally treat the [] and {} as sigils to indicate that the variable HOLDING the data is an array or a hash.

(I'm sure someone is going to find a flaw in my thinking and really screw me up ;-))

Re:I'm wondering...

tomhukins on 2005-02-08T18:44:21

That's an interesting point: the immediate punishment of mistakes fixes habits quicker than things seeming to work. I certainly have seen the mistake made more often with arrays than hashes.

The warnings pragma catches this misuse nowadays, but many beginners either don't know about it or don't use it.

Unfortunately, I didn't learn hashes until I had already familiarised myself with arrays, so I can't answer your first question.

Perl, French and Japanese

Maddingue on 2005-02-07T19:40:09

This is a comment from another French monger which doesn't have an account on user.Perl yet, and asked for someone to post it. So here it is.

-----

Well, I'm also a French native speaker and I must admit that I too made this mistake when I first learnt Perl, but I had to accept that "$" had to be used to access a single element and "@" to access the whole array. Actually, Perl's scalar and lists contexts still aren't really clear to me but the more I write Perl, the more I get used to it. I don't even try to understand why I have to do so, which may be a bad behaviour, but I came to just know when I must use this or that.

But, now I consider what you wrote, I now *know* that I should use "@" to use a slice, but then, what is the difference between the single value $array[$i] and the single element slice @array[$i] ? This particular example shows the problem when trying to adapt a thought language to a naturally evolved language such as French. I can't think of such a plural in French. Or maybe "une paire de ciseaux" which would equal the English "a pair of trousers", but that's become a kind of evolution of an exception and isn't a general rule.

On the other hand, I also speak Japanese, which is a language without plural, and this "$/@" stuff might be the reason why a Japanese guy invented Ruby :) Moreover, learning such a different language such as Japanese has taught me to admit rules at first sight, and then to try to understand them if I ever see an etymological explaination or how Perl manipulates this in its core. But for this, one needs a lot of knowledge of the language....

I hope that it answers your question,

Sincerely, Axioplase

sigil and plural

properler_head on 2005-02-09T13:56:02

Many languages use some kind of mark (flexion?) on the word itself to denote the plural. Article like "les" in French are redundant as far as the plural or singular is concerned. It comes in definite form (les) and the indefinite one (des), but I don't think that distinction can translate usefully into Perl.

I consider that the @ sigil is the equivalent in Perl of plural in natural languages, so I use the sigil in place of plural instead of with it.

So I say @house and not @houses.

I think that other programmers, like Damian Conway, would advocate to write @houses. I don't remember the rationale