palik
palik

Reputation: 2863

Why Perl ignores spaces between a sigil and variable name?

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?

Upvotes: 3

Views: 164

Answers (2)

Emil Perhinschi
Emil Perhinschi

Reputation: 1283

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

Upvotes: 4

Grinnz
Grinnz

Reputation: 9231

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.

Upvotes: 5

Related Questions