0

Possible Duplicate:
Why do Perl variables need to start with $, %,@?

Other scripting languages seems to get along just fine without this or something similiar.
I guess it has something to do with memory allocation and helping the interpreter in order to speed things up, but I couldn't find anything specific on it. $scalar would probably be put into stack, @array into heap and %hash? Into heap as well? And what about ?subroutine?
Could someone help me figure this out or point me to some documentation? I am still trying to grasp some fundamentals and understand how everything works under the hood...

Community
  • 1
  • 1
shutefan
  • 6,156
  • 8
  • 23
  • 23
  • 3
    These symbols are called [sigils](http://en.wikipedia.org/wiki/Sigil_(computer_programming)) by the way. – BoltClock Aug 03 '11 at 16:14
  • 2
    Whitespace and Brainf*ck seem to get along just fine without using any alphanumeric symbols. Just saying. – mob Aug 03 '11 at 16:22
  • 1
    This has to be a homework question. In three months I have seen this question exactly one time, yesterday, here on SO: [here](http://stackoverflow.com/questions/6922245/dollar-notation-in-script-languages-why/6922313#6922313). I provided one of the answers, and there were several other good answers. The thread got closed for being off-topic. I smell homework; the questions are too similar, too close together, and too student-oriented. – DavidO Aug 03 '11 at 16:33
  • (Correction: Two other times; one being yesterday and the other identified by @BoltClock in a previous comment here.) – DavidO Aug 03 '11 at 16:39
  • 1
    The best explanation is because $calar and @rray. – sidyll Aug 03 '11 at 16:39
  • @DavidO Thanks for the answer, I saw the other question you are referring to and concluded that it did not ask for the same thing. – shutefan Aug 03 '11 at 18:04
  • No, they have nothing to do with memory allocation or helping optimize for speed. Internally a scalar, hash, or array have different data-structures for storage, but that has nothing to do with what the language looks like. – ysth Aug 04 '11 at 00:31

2 Answers2

5

Because it makes it easier to read.

You know which identifiers are nouns, and whether they're singular or plural, because of the sigaldry. It's the same reason in English we have singular and plural determiners and agreement, as in this species is vs these species are. It's nice to know which is which.

tchrist
  • 78,834
  • 30
  • 123
  • 180
  • So you'd say that there is no reason behind it except for readability? It's entirely directed towards the programmer? – shutefan Aug 03 '11 at 18:06
  • Just wanted to make sure that I understand your answer correctly. As I pointed out in my question, it might well be that the sigils are an identifier for the interpreter, giving order how to allocate the variable in the memory. – shutefan Aug 03 '11 at 18:11
  • How is that different? Anyway, if you don’t use a sigil, it would look like a bareword string, forbidden under `use strict`. Why does the shell have sigils? – tchrist Aug 03 '11 at 18:41
  • Why do you need int, str etc. in e.g. C? _You_ certainly don't need them and most "scripting languages" don't have them. You put them there for the compiler. Again: the question is not "what do the sigils stand for" but if they have any meaning for the interpreter. If you can assure that they are for the programmer and for the sake of readability only then I am content with that. That is why I asked you if you were absolutely sure if there is nothing else to it. I never questioned the importance of readability nor your maturness. – shutefan Aug 03 '11 at 19:30
  • 1
    @shutefan: Sigils are used by the reader to gain a quick understanding of code. But they make it possible for the compiler to easily allow things like variable interpolation, reference dereferencing, and differentiation between `@abc` and `$abc` which are two different variables in Perl. Without sigils, variable interpolation would require some other metacharacters, dereferencing would require other metacharacters, and abc would not be able to both be a string and a completely separate, independent array, hash, and function. So to answer your question, yes. The compiler uses sigils too. – DavidO Aug 03 '11 at 20:34
  • @DavidO: Thanks a lot! Sorry for not being able to check your answer to be the right one, though... – shutefan Aug 03 '11 at 20:41
4

Perl stores all data associated with a name in a single symbol table entry. The structure stored there is called a typeglob. The values for $foo, @foo, %foo, and &foo (subroutine reference) are all stored in the typeglob for "foo". The entire typeglob is denoted *foo (where * indicates "all sigils"). All this is explained in the perldata section of the Perl documentation.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • 2
    Yes, but that doesn't really explain *why* it's done that way. The existing system means that there's a (mostly invisible) association between $foo and @foo, and that you can use the same identifier for a scalar and an array in the same scope -- but the language could easily have been designed differently, without sigils, and with a different symbol table organization (see Python, for example). I think the real reason is so that you can tell at a glance, by looking at a variable's name, what kind of variable it is. – Keith Thompson Aug 03 '11 at 16:40
  • The symbol table only applies to package global variables. Lexicals (`my` variables) use lexical pads which are buried deeper into the Perl internals. perlguts – DavidO Aug 03 '11 at 16:56
  • 1
    The real reason is to separate variable names from keywords. You don't have to know all of the keywords to choose a name. – brian d foy Aug 03 '11 at 23:03