4 ms·
The sigils are a feature that underpin perl's roots from practical linguistics (unlike other languages which have roots in theoretical linguistics). $ - > the
by singingfish 6y ago
The sigils are a feature that underpin perl's roots from practical linguistics (unlike other languages which have roots in theoretical linguistics).
$ - > the value of
@ - > the values of
% -> the collection of key value pairs
$_ is pronounced "it". @_ is pronounced "them". Don't use either of them except if it would make sense in english - e.g. you wouldn't refer to "it" in anything more than a sentence, so don't use $_ in anything longer than a couple of lines of code
- azangru 6y ago> The sigils are a feature that underpin perl's roots from practical linguistics I am not sure this metaphor makes sense to me. Imagine that I have a pack of dogs. When I use a language to say: "Remember that pack of dogs that I mentioned previously? Go ahead and give it to me" — I do not expect to also need to specify whether I want the pack with all the dogs in it, or whether I just want some value associated with that pack, which, if I remember perl's behaviour here, would be its length. I could understand if a language wanted me to specify whether I want the thing itself as opposed to a reference to the thing (I suppose it would make total sense as a Saussurian opposition of a signifier vs the signified); but the ternary opposition that perl enforces is neither here nor there linguistically — it's just bizarre.
- daotoad 6y agoThe the distinction in Perl is context. If I want to flatten that pack of dogs into a collection of individual dogs, I'd use the `@` sigil. If I want to operate on the pack as a whole, I'd use the `$`. Context and list flattening are probably the most foreign parts of Perl to someone who doesn't know it. Everyone fixates on the sigils, but the weirdness in the way sigils operate stems from context. When you call a function, the arguments are flattened into a single list. So `foo(1,2)` is the same as `my @args = (1,2,3); foo(@args, 3)`. Every expression can be evaluated in one or more contexts. So taking the value of an array in scalar context gives you the length, which is handy but also means that, since 0 is falsy, you can just do logical tests on arrays to check if they are empty. Because the behaviors are relatively intuitive, it's possible to quite a bit of work in Perl before you even understand what context is or how it operates. `if (@foo) { do_stuff(@foo) }` does what you'd think it does, even if you don't know about context. But it also means that the distinction between `my $match = $foo=~/(foo)/g;` and `my ($match) = $foo=~/(foo)/g;` is not at all obvious if you don't understand context. (The first example gives you the count of matches, the second example gives you the first match because the parens put you in a list context which means you are effectively assigning into a slice.)
- singingfish 6y agoIt's not a metaphor - it's the actual design. The fact that you use two sentences don't make sense together in order to try to illustrate your point seems to me to demonstrate that.
- singingfish 6y agohere's a different one for you. This one is definitely a metaphor: Python helps you to think more like the computer does. Perl helps the computer think more like you do.