Re^8: chopping a string into slices - is there a more elegant way to do it?
by gone2015 (Deacon) on Dec 01, 2008 at 22:44 UTC
|
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] |
Re^8: chopping a string into slices - is there a more elegant way to do it?
by LanX (Saint) on Nov 30, 2008 at 17:28 UTC
|
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] |
|
|
sub which
{
if(wantarray)
{
print "list\n"; ();
}
else
{
"scalar\n"; 0;
}
}
my $a = which();
my ($b) = which();
seems to confirm this suspicion. The first prints scalar and the second prints list. This seems to imply that the subroutine is evaluated in scalar context in the first assignment.
Is there something I'm missing?
| [reply] [d/l] [select] |
|
|
|
|
|
|
|
Sorry many words I don't understand...
($a)= puts the RHS in listcontext,
$a= puts the RHS in scalarcontext
it's alway the same "=" and of course $a is a scalar.
you said
> The parens in ($a)=@_ don't affect context
> Except when empty, parens never do more than control precedence. Except when empty, they never create a list context.
The code shows your wrong that perl syntax is more complex. But if I'm wrong, please give me a reference to a perldoc defining "listcontext" the way your understanding it!
UPDATE: to make it clear, I'm not saying that $a is evaluated in list-context, but I think it's reasonable to say that the LHS ($a) is a one element list, where commas were saved, like () is a null element list. If not please show me code that showes that ($a) is not a list.
| [reply] [d/l] [select] |
|
|
|
|
|
|
|
|