Years ago, I learned Applesoft Basic. Strings were always suffixed with $
and arrays had a suffix of %
. Thats just how the language worked. You looked at something, you knew what it was. I never did delve too far into the interpreter to understand why this was the case or the design decisions that made it so.
The sigil in php comes from its perl influence (which was influenced by awk
and sh
). The sigil in perl is quite a bit more than just $
as it can identify many different types:
$
scalar
@
list
%
hash
&
codeblock
*
typeglob
The sigil identifies what part of the symbol table structure you are looking at. Behind the scenes, the symbol table entry for foo (accessed via *foo
- the typeglob) has everything that may be a foo. There is $foo
, @foo
, %foo
, the format foo
, &foo
, the filehandle foo, etc...
This also allows making an alias of one variable to another:
#!/usr/bin/perl
$foo = "foo";
@qux = (1,2);
*bar = \$foo;
*bar = \@qux;
print "$bar @bar\n";
This prints foo 1 2
- in perl, this is what the sigils are really for, not that you should do this but rather that there is this behind the scenes thing that they do.
The sigils aren't there so much for readability, but rather so that one can have $foo
and @foo
without having a collision in the namespace (compare other languages where one cannot have both int foo; int[] foo;
)
Sigils for readability are something that is learned as part of any language - reading the syntax. You could, hypothetically, enforce the type itself (as hungarian notation) to be part of the identifier.
Something in lex along the lines of:
typeChar [is]
capLetter [A-Z]
letter [a-z]
digit [0-9]
%%
{typeChar}{capLetter}(letter}|{digit})* { prientif("iddentifier");}
%%
And then you could have code like
iFoo = 42;
sFoo = "a string";
iBar = iFoo * 2;
I'm not saying this is a good idea, but rather that someone who is accustom to the language will be able to read this natively and think that it enhances readability while someone who isn't familiar with the language may think that it just adds a bunch of noise to the language.
However, after working with a language defined this way, I could probably read it without trouble.
Some people like them, some people don't. There are great holy wars in various forums debating this and it really boils down to how much you've used them.
One could design a new language for non-programmers that uses sigils and anyone who has never programed before will never complain one bit about them. On the other hand, you could not have them as part of the language and then have ruby or perl programers complain that they're missing out on some key information.
It really doesn't matter. What does matter is how sigils would fit into the language if you use them or not. Do you want to be able to do "123 $foo 456"
or must you do "123 " + foo + " 456"
? This is where the decision should be made.
sigils
three times today in conversations.var $x = ...
ortype $x = ...
then I think $ is overkill. If you just had$x = ...
then it could be worth doing. Especially if you don't want to support syntax highlighting in common editors. However, as a preference, I don't likesigils