Raku sigils denote the nature of the underlying variable (e.g., $scalar, @positional, %associative, &code).
It's possible to declare a variable as sigil-less with a backslash (e.g., \some-variable) and then later refer to it without a sigil (i.e., some-variable).
Just wondering in what circumstance it's preferred to use a sigil-less variable?
Declarations of the form
my \a = expr
introduce an aliasa
to the expressionexpr
without enforcing any kind of context on it, as assignment into a sigiled variable would. Thus their primary use is when you don't want to have any of the semantics associated with any sigil.Personally, I use them most when I am building up lazy processing pipelines and want to name the parts. As a simple example:
A
grep
returns aSeq
that, if iterated, will perform the operation. If I were to instead use an@
-sigil variable:Then while the results would be the same, the memory performance would be very different: assignment is eager unless it encounters something explicitly marked lazy, and so this would store all the non-comment lines into
@no-comments
and then iterate over them. Thus all those lines stay around in memory, whereas in the sigilless version then processed lines - so long as they are not stored elsewhere - can be garbage collected.I could use a
$
sigil, but that implies a single item, meaning that if I do this:It won't work (it would do one iteration of the loop, binding the
Seq
into$sig-line
); I'd have to overcome the item nature of it somehow:A related use is when writing generic code that does not want to enforce any context:
Again, if we used
$
we could add an item wrapper and potentially impact upon the behavior offoo
.Other uses I've seen: