2 ms·
Perl lets you do this too: my $foo = 5; my $bar = 'x'; my $quux = "I have $foo $bar\'s: @{[$bar x $foo]}"; print "$quux\n"; This prints out:
by autarch 2y ago
Perl lets you do this too:
my $foo = 5;
my $bar = 'x';
my $quux = "I have $foo $bar\'s: @{[$bar x $foo]}";
print "$quux\n";
This prints out:
I have 5 x's: xxxxx
The "@{[...]}" syntax is abusing Perl's ability to interpolate an _array_ as well as a scalar. The inner "[...]" creates an array reference and the outer "@{...}" dereferences it.
For reasons I don't remember, the Perl interpreter allows arbitrary code in the inner "[...]" expression that creates the array reference.
- Izkata 2y ago> For reasons I don't remember, the Perl interpreter allows arbitrary code in the inner "[...]" expression that creates the array reference. ...because it's an array value? Aside from how the languages handle references, how is that part any different from, for example, this in python: >>> [5 * 'x'] ['xxxxx'] You can put (almost) anything there, as long as it's an expression that evaluates to a value. The resulting value is what goes into the array.
- autarch 2y agoI understand that's constructing an array. What's a bit odd is that the interpreter allows you to string interpolate any expression when constructing the array reference inside the string.
- Izkata 2y agoIt's not...? Well, not directly: It's string interpolating an array of values, and the array is constructed using values from the results of expressions. These are separate features that compose nicely.
- JadeNB 2y ago> What's a bit odd is that the interpreter allows you to string interpolate any expression when constructing the array reference inside the string. Why? Surely it is easier for both the language and the programmer to have a rule for what you can do when constructing references to anonymous arrays, without having to special case whether that anonymous array is or is not in a string (or in any one of the many other contexts in which such a construct may appear in Perl).
- weinzierl 2y agoYou also don't need quotes around strings (barewords). So my $bar = x; should give the same result. Good luck with lexing that properly. https://perlmaven.com/barewords-in-perl https://perlmaven.com/barewords-in-perl
- shawn_w 2y agoIf you're writing anything approaching decent perl that won't be accepted.
- emmelaich 2y ago"use strict" will prevent it and I think strict will be assumed/default soon.
- JadeNB 2y agoAs of Perl 5.12, `use`ing a version (necessary to ensure availability of some of the newer features) automatically implies `use strict`. https://perldoc.perl.org/strict#HISTORY https://perldoc.perl.org/strict#HISTORY
- weinzierl 2y agoDoesn't really matter for a syntax highlighter, because it is out of your control what you get. For the llamafile highlighter even more so since it supports other legacy quirks, like C trigraphs as well.