Caldarale, Charles R wrote:
From: Christopher Schultz [mailto:ch...@christopherschultz.net]
Subject: Re: [OT] of the different methods to get a user-id

I don't understand that, either. I suppose this works differently in
different languages, though:

return i++;

return (i++);

Not any that I'm aware of; the value of the i++ expression is the same, 
regardless of the number of parentheses you wrap it in.

I tried in C and Java and got the same result (both
return the original value of i), though I would have
expected something different.

Nope; the parentheses in such a statement control only operator precedence, not 
anything else.  Don't confuse parentheses used in an expression with those used 
for function calls or if/while statements - they're syntactically different, 
even if they share the same code point.

I was going to let that one pass, but now I'm provoked.
The expressions above do work differently in Perl.

1) caller side :

a) my $var = getRemoteUser();
sets a /scalar/ context for the call. Means that the caller expects a single value in return.

b) my @var = getRemoteUser();
sets a /list/ context for the call. Indicates that the caller expects a list (an array) in return.

2) callee side :

a) return i++;
Returns the value of i prior to the post-increment, as a scalar value.
So it "fits" with 1a.

b) return (i++);
Returns a list composed of 1 element : the value of i before the post-increment.
So it "fits" with 1b.

But..
if you combine the call 1a
my $var = getRemoteUser();
with the version 2b : return (i++)
then when you get in $var is 1, always.

That is because since
my $var = getRemoteUser();
expects a scalar, but what it gets is a list, it does an implicit
my $var = (list), which equivalent to
my $var = scalar(list)
which returns the number of elements present in the array/list.

However, the calle sub can be cleverer and anticipate this, by doing :
return wantarray ? (i++) : i++;
thus checking in which context it is being called, and returning the appropriate form of response.

Perl is great !



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org

Reply via email to