foreach statement's parentheses

X

xyoavx

Hello,
I would like to understand why, in the foreach statement, the array's
name must be written within parentheses. The foreach "waits" for a
list, so I expect it to interpret the array's name in a list context
(like function sort for example) without needing the parentheses.
Thanks in advance,
xyoavx
 
E

Eric Bohlman

@g43g2000cwa.googlegroups.com:

I would like to understand why, in the foreach statement, the array's
name must be written within parentheses. The foreach "waits" for a
list, so I expect it to interpret the array's name in a list context
(like function sort for example) without needing the parentheses.

The parentheses are simply part of the statement's syntax, just as they are
for a while() loop or a c-style for(;;) loop.

I think you've fallen into the common misconception that parentheses act as
a list constructor in Perl. They don't. In Perl, the context of an
assignment is determined by the *left* side of the assignment. The
parentheses around a literal list on the right side of an assignment in
list context are only there to override the usual precedence of the comma
operator; they in no way "create" the list. In @array=(1,2,3) the *only*
thing the parentheses do is keep the assignment operator "=" from being
evaluated before the first comma.
 
I

it_says_BALLS_on_your forehead

Tim said:
Eric Bohlman said:
I think you've fallen into the common misconception that
parentheses act as a list constructor in Perl. They don't.
In Perl, the context of an assignment is determined by the
*left* side of the assignment. The parentheses around
a literal list on the right side of an assignment in list
context are only there to override the usual precedence of the
comma operator; they in no way "create" the list. In
@array=(1,2,3) the *only* thing the parentheses do is keep the
assignment operator "=" from being evaluated before the first
comma.

I've seen this response quite a lot, and I understand what the
parens do in the above examples.

Maybe someone can put into words the job of the parens in the
statement below?

my $size = (stat)[7];

Are they just grouping? Are they temporary containers for the
results of stat() so the parser doesn't get confused at the
[]'s?

and what about:
my $name = $word =~ m/^\b(.*)\b/;

vs.

my ($name) = $word =~ m/^\b(.*)\b/;
 
I

it_says_BALLS_on_your forehead

it_says_BALLS_on_your forehead said:
Tim said:
Eric Bohlman said:
I think you've fallen into the common misconception that
parentheses act as a list constructor in Perl. They don't.
In Perl, the context of an assignment is determined by the
*left* side of the assignment. The parentheses around
a literal list on the right side of an assignment in list
context are only there to override the usual precedence of the
comma operator; they in no way "create" the list. In
@array=(1,2,3) the *only* thing the parentheses do is keep the
assignment operator "=" from being evaluated before the first
comma.

I've seen this response quite a lot, and I understand what the
parens do in the above examples.

Maybe someone can put into words the job of the parens in the
statement below?

my $size = (stat)[7];

Are they just grouping? Are they temporary containers for the
results of stat() so the parser doesn't get confused at the
[]'s?

and what about:
my $name = $word =~ m/^\b(.*)\b/;
woops! make those...
my $word = "Headly";
my $name = $word =~ m/^\b(\w+)\b/;

$name contains 1
vs.

my ($name) = $word =~ m/^\b(.*)\b/;
my ($name) = $word =~ m/^\b(\w+)\b/;
$name contains Heady ;-). (It's Headly!!)
 
I

it_says_BALLS_on_your forehead

Tim said:
Eric Bohlman said:
I think you've fallen into the common misconception that
parentheses act as a list constructor in Perl. They don't.
In Perl, the context of an assignment is determined by the
*left* side of the assignment. The parentheses around
a literal list on the right side of an assignment in list
context are only there to override the usual precedence of the
comma operator; they in no way "create" the list. In
@array=(1,2,3) the *only* thing the parentheses do is keep the
assignment operator "=" from being evaluated before the first
comma.

I've seen this response quite a lot, and I understand what the
parens do in the above examples.

Maybe someone can put into words the job of the parens in the
statement below?

my $size = (stat)[7];

Are they just grouping? Are they temporary containers for the
results of stat() so the parser doesn't get confused at the
[]'s?

per Programming Perl 3rd ed. pg. 74.

"A list value may also be subscripted like a normal array. You must put
the list in parentheses (real ones) to avoid amibiguity. Though it's
often used to fetch a single value out of a list, it's really a slice
of the list, so the syntax is:"

(LIST)
 
J

John Bokma

xyoavx said:
Hello,
I would like to understand why, in the foreach statement, the array's
name must be written within parentheses.

@foo = 1..3;
print "is that true?\n" foreach @foo;
 
I

it_says_BALLS_on_your forehead

Eric said:
@g43g2000cwa.googlegroups.com:



The parentheses are simply part of the statement's syntax, just as they are
for a while() loop or a c-style for(;;) loop.

I think you've fallen into the common misconception that parentheses act as
a list constructor in Perl. They don't. In Perl, the context of an
assignment is determined by the *left* side of the assignment.

However, parens on the left side *do* force list context. check out
Programming Perl 3rd ed. pg. 69.
 
I

it_says_BALLS_on_your_forehead

Tim said:
"A list value may also be subscripted like a normal array. You must put
the list in parentheses (real ones) to avoid amibiguity. Though it's
often used to fetch a single value out of a list, it's really a slice
of the list, so the syntax is:"

(LIST)


  • Thanks. I knew it was good perl. I'm just looking for a term
    to describe what the parens do in that instance.

    Thanks again,
    Tim Hammerquist


  • no prob. i guess the term would be 'disambiguate'.
 
E

Eric J. Roode

woops! make those...
my $word = "Headly";
my $name = $word =~ m/^\b(\w+)\b/;

$name contains 1

Recall what Eric Bohlman said in an earlier post in this thread:
In Perl, the context of an
assignment is determined by the *left* side of the assignment.

In the above assignment, "my $name" is a scalar expression, and
as 'perldoc perlop' says, the m// operator returns true upon
success if it is used in a scalar context.
my ($name) = $word =~ m/^\b(\w+)\b/;
$name contains Heady ;-). (It's Headly!!)

"my ($name)" is a list expression. In list context, m// returns
the parethesized matches upon success.

--
Eric
`$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
$!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
$_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`
 
I

it_says_BALLS_on_your forehead

Eric said:
Recall what Eric Bohlman said in an earlier post in this thread:

In the above assignment, "my $name" is a scalar expression, and
as 'perldoc perlop' says, the m// operator returns true upon
success if it is used in a scalar context.


"my ($name)" is a list expression. In list context, m// returns
the parethesized matches upon success.

absolutely right. i was just making the distinction that there are
cases when parens *do* signify a list context.
 
X

xyoavx

Hi,
Thanks for your profound answers.
Regards,
xyoavx

it_says_BALLS_on_your_forehead said:
Tim said:
"A list value may also be subscripted like a normal array. You must put
the list in parentheses (real ones) to avoid amibiguity. Though it's
often used to fetch a single value out of a list, it's really a slice
of the list, so the syntax is:"

(LIST)


  • Thanks. I knew it was good perl. I'm just looking for a term
    to describe what the parens do in that instance.

    Thanks again,
    Tim Hammerquist


  • no prob. i guess the term would be 'disambiguate'.
 
X

xyoavx

Hi John Bokma
Thanks for your excellent answer.
Now I understand that not always, in the foreach statement, the array's
name must be written within parentheses. So, in what cases it must ?
Regards,
xyoavx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top