3

The question Is space supposed to be ignored between a sigil and its variable name? was answered positively.

What is the reason Perl interprets $ foo as $foo?

perl -w -E 'my $  foo = $$; say  "Perl $]\n\$ foo = ", $foo'
Perl 5.028001
$ foo = 3492

Isn't it against The Syntax of Variable Names documentation?

palik
  • 2,425
  • 23
  • 31
  • I can't say why, but I can say it's most definitely not an encouraged practice. That would explain the lack of documentation of this "feature". – ikegami Jun 05 '19 at 17:25

2 Answers2

5

That documentation only discusses the name, not the sigil. The sigil can always be separated from the name by space characters. It is definitely underdocumented and I would not suggest ever making use of it, but it is used.

Grinnz
  • 9,093
  • 11
  • 18
4

Perl does not have sigils, it has "dereference" operators:

$test[1] means 'give me the scalar at the index 1 of the array called "test" from this scope'. That is why you can put spaces after the "sigil".

I don't understand why everybody keeps calling them sigils, it makes things very confusing. BASIC had sigils, PHP has sigils, but Perl 5 does not even if it looks like it has. I wish I had realized the "sigils" are in fact operators when I was learning Perl, understanding and parsing references and derefferencing would have been a lot easier, not to mention grokking symbol tree manipulation.

The "sigils" are not documented as "operators" in perldoc, but it is much easier to parse Perl code if you think of them as being operators.

Later, after discussion in the comments: here is how Perl 5 uses "sigils": https://www.oreilly.com/library/view/advanced-perl-programming/0596004567/ch01.html

zdim
  • 64,580
  • 5
  • 52
  • 81
Emil Perhinschi
  • 1,185
  • 11
  • 14
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/194566/discussion-on-answer-by-emil-perhinschi-why-perl-ignores-spaces-between-a-sigil). – Bhargav Rao Jun 07 '19 at 01:46
  • "I don't understand why everybody keeps calling them sigils" [This answer](https://stackoverflow.com/a/1091635/1073003) mentions origins of the name. – cl0ne Oct 03 '22 at 10:28