Hacker News new | comments | show | ask | jobs | submit login
On Sigils (Perl) (perltricks.com)
3 points by ylmtggxuke 10 months ago | hide | past | web | favorite | 3 comments

I'm so used to how sigils in perl5 work that I don't think I could convert to perl6 very easily.

I've always wondered though, under use strict, why couldn't the behavior of sigils be modified. Your declaring the variable ahead of time, you could give hints to the compiler about what kind of variable it is. If it clashes with a reserved word, strict could cause it to fail.

Example 1: my $string = "this is a string"; # works like past versions

$string .= "!"; # this is a string!

Example 2: use 5.30;

use strict;

my string($) = "this is a string";

string .= "!"; # this is a string!

One of the functions of sigils is to serve as mini namespaces. While your example might be technically possible, it means that variable names can now clash with function and type names (something that I, as a long-term Perl programmer, find very inconvenient in Python).

Another complication is that Perl uses predictive parsing. It always knows whether the next thing it expects is an operator or a term. That's what allows the use of / for both the division operator and a delimiter for regexes. After a variable, the parser always expects an operator. After a function name it expects a term (because you can leave out the parens in a function call, foo($bar); being the same as foo $bar;).

While there are a few exceptions to that rule (like subroutines with empty prototypes), introducing many exceptions through unsigiled variables might lead to code that's not very intuitive to parse anymore. (Or might not; might an interesting experiment).

    my %ordered-hash is OrderedHash;
    my @ordered-hash := %ordered-hash;
If you don't care for sigils:

    my \ordered-hash = OrderedHash.new;

    say ordered-hash<foo>;
    for ordered-hash {…}
Note that you can only assign to sigilless variables when you declare them.

You could assign an anonymous scalar to it to allow you to assign to it later.

    my \sigilless = my Int:D $ is default(0) = 5;
    sigilless = 4;

Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact