Shlomi Fish <shlo...@shlomifish.org> writes:

>> >
>> > In scalar context the comma operator evaluates its left-hand side,
>> > throws it away and returns the right-hand side.  
>> 
>> What is the useful use for this operator?
>> 
>
> Well, I believe its use was originally inherited from
> https://en.wikipedia.org/wiki/C_%28programming_language%29 where one can do
> something like:
>
>       x = (y++, y+2);
>
> In Perl 5 though it is preferable to use do { ... } instead:
>
>       $x = do { $y++; $y+2; };
>
> See http://perldoc.perl.org/functions/do.html . GCC and compatible compilers
> have a similar feature to Perl 5's do {...} called statement expressions:
> https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html .

How's that useful?  Isn't that equivalent to

$x = $y + 3;

?

I'm surprised that this apparently is supposed to evaluate to something,
though.  I wouldn't expect that from a control structure.

>> > This means that the value of (1, 2, 3) in scalar context is 3, and this
>> > is what gets assigned to $list.
>> >
>> > What is not happening at all is the creation of a list of numbers and a
>> > calculation of its length.
>> >
>> > See also perldoc -q 'difference between a list and an array'  
>> 
>> How do you convert an array into a list?
>> 
>
> You just put it in list context. For example (untested):
>
> sub f
> {
>       print (@_);
> }
>
> my @input = (3, 44, 505, 6.6);
>
> f(@input);
>
> my @other_list = (5,@input,24);

I'm not sure where the conversion of an array (non-static) into a list
(static) would take place in this example.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to