Re^7: chopping a string into slices - is there a more elegant way to do it?
by gone2015 (Deacon) on Nov 30, 2008 at 13:22 UTC
|
I'm sure I read somewhere that this form can be best understood in terms of the implied or transparent comma -- from memory, that would be a Unicode "ZERO WIDTH NO-BREAK COMMA". (Known to some as the fairy or pixie comma, spreading a little List Context magic while remaining tantalizingly just out of sight. I believe that in Ireland this is also known as one of the extensive family of O'Mission leprechauns.)
| [reply] |
Re^7: chopping a string into slices - is there a more elegant way to do it?
by ikegami (Patriarch) on Nov 30, 2008 at 15:37 UTC
|
To correct myself, there are very special situations where parens affect the result. eof vs eof() and (...)x... vs ...x..., for example. And of course ($x)=... vs $x=....
But while parens sometimes do more than control precedence, I can't think of anywhere where non-empty parens create a list context.
The parens in ($a)=@_ don't affect context. They cause a list assignment operator (aassign) to be used instead of a scalar assignment operator (sassign).
That said, it's very convenient to think that some parens create a list, even when it isn't technically true. But the code being discussed definitely does not contain parens which could be thought of as "list forming".
| [reply] [d/l] [select] |
|
|
I've just been tripped up by:
my @foo = (2, 3, 4 x 17, 3, 2, 0, 0)
being quite different from:
my @foo = (2, 3, (4) x 17, 3, 2, 0, 0)
which looks as if the '()' confer some quality of list-ness. The documentation says:
.... In scalar context or if the left operand is not enclosed in parentheses, it returns a string consisting of the left operand repeated the number of times specified by the right operand. In list context, if the left operand is enclosed in parentheses or is a list formed by qw/STRING/, it repeats the list.
so the brackets are part of the syntax... I confess that I was (for reasons unknown) expecting 'x' to do listy things in List Context and scalary things otherwise. (Many things take lists of arguments, so I guess it would be inconvenient for (e.g.) '-' x 24 to yield a list in List Context.)
Similarly, of course, foreach VAR(LIST) BLOCK reenforces the feeling that '()' have something to do with the making of lists.
| [reply] [d/l] [select] |
|
|
for (foo()) { bar() } # List
while (foo()) { bar() } # Scalar
if (foo()) { bar() } # Scalar
print(foo()) # List
length(foo()) # Scalar
I even use for as a topicalizer regularly.
for ($var) { # Scalar
s/^/[/;
s/$/]/;
}
The purpose of parens in flow control statements in Perl5 is purely decorative. However, their roots are in C, where their purpose is for precedence.
| [reply] [d/l] [select] |
|
|
DB<54> $a= join ",",(1..5)
DB<55> p $a
1,2,3,4,5
DB<56> print scalar split /,/,$a
5
DB<57> ($A)=split /,/,$a
DB<58> print $A
1
split is executed in listcontext!
| [reply] [d/l] |
|
|
Yes, operators determine the context in which their operands are evaluated, and the aassign operator unconditionally evaluates its operands in list context. It's not based on the presence or absence of parens.
You previously said that the operand ("($a)") determined the context. An expression never decides the context in which its evaluated. The context in which its evaluated is always imposed upon it by its caller.
Even at the abstract level, the reason for using "($a)=" instead of "$a=" isn't to evaluate $a in list context. After all, $a evaluates to the same thing in both list and scalar context. The goal is to use the list assignment operator.
| [reply] [d/l] [select] |
|
|
|
|
|
|
|
|
|
|