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.
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.
Of course, Perl6 reverses it to @foo[87].
Bill
when the data is stored in a hash?my $house = %house{87};
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.
Thewarnings
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.
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
I hope that it answers your question,
Sincerely, Axioplase
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