Hi lee,

I should note that it is my impression that you are far too needy in your
enquiring, and I'm starting to lose patience.

On Mon, 25 Jan 2016 00:11:43 +0100
lee <l...@yagibdah.de> wrote:

> 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;
> 
> ?

Well, it also changes the value of $y. You can use more complicated examples,
and this was just for the sake of demonstration.

One common pattern I used is to do:

my %cache;
sub f
{

        return $cache{$result} //= do {
                # Calculate the cached result here.
        };
}

> 
> 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.
> 

(5,@input,24) becomes (5,3,44,505,6.6,24).

Regards,

        Shlomi Fish

-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
List of Text Processing Tools - http://shlom.in/text-proc

Inigo Montoya: You seem a decent fellow. I hate to kill you.
The Man in Black: You seem a decent fellow. I hate to die.
    — http://en.wikiquote.org/wiki/The_Princess_Bride_%28film%29

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
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