Hacker News new | past | comments | ask | show | jobs | submit login

So variables in Perl 6 can be sigil-less...by prefixing a backslash like my \p = [];

Umm.. backslash is a sigil. Why do we need to add this ?




You only need the sigil during definition. So, for example (in the Perl 6 REPL):

    > my \x = 1
    1
    > my \y = 2
    2
    > say "Look Ma, no punctuation!" if x + y == 3
    Look Ma, no punctuation!
There are some implications of this; namely: x and y are now immutable. Back in the REPL:

    > my \x = 1
    1
    > x = 2
    Cannot modify an immutable Int (1)
      in block <unit> at <unknown file> line 1
This is because in Perl6 the sigil is a container for the value represented by that variable, so since you don't have a sigil, you don't have a container; that is: the names "x" and "y" are now literal synonyms for "1" and "2" (respectively). "x = 2" fails here for the same exact reason (and with the same exact error message) you'd get if you tried to do "1 = 2".


Sweet explanation ;-)!

For those interested on learning more about containers in Raku, lizmat wrote a nice article about them [1]. There's also this documentation page [2] that gives a detailed overview of them, where you can find more about sigilless variables.

[1] https://opensource.com/article/18/8/containers-perl-6

[2] https://docs.perl6.org/language/containers


See the examples. The backslash appears only in the function signature, but not in the function itself. You have to declare that you are using sigil-less variables somehow!




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

Search: