To summarize

There is a list assignment and the difference between assigning to an
empty list and an non empty list is the container(s) inside the liste
that capture the different values (scalars or lists or hashes)

for example () ans ($a, @b, %c... )

when we assign to an ampty list the values assigned are not captured
but the elements assigned are counted

when we evaluate a list in scalar context, we are accessing its length.

So i could translate this expression as following

$s = () = get_hats_clone();

assigning the return value of get_hats_clone() to an empty list that
dont copy (capture) them but count them, and that assignement 
'() = sub()' have a return value.
that return value when in list context is the elements of the list it
self. When in scalar context, it's the number of elements of that list
(it is empty but stored the number of all the elements returned by
get_hats_clone()

since we are using scalar context with $s = (), then we get the number
of all the elements.

Is that correct.

Regards

On Tue, 2016-10-04 at 09:14 +0000, Chas. Owens wrote:
> 
> 
> On Mon, Oct 3, 2016 at 10:45 PM Lawrence Statton <lawre...@cluon.com>
> wrote:
> snip
> > the =()= "operator" just does that without creating a temporary
> > variable
> snip
> 
> Almost, but not quite.  You can see the difference when =()= is in
> list context:
> 
> my @a = (my @temp) = get_clown_hat;
> my @b = () = get_clown_hat;
> 
> @a will have the items returned by get_clown_hat and @b won't.  The
> assignment operators have a return value.  For scalar assignment, it
> is just the scalar being assigned, regardless of the context.  For
> list assignment in list context it is the list that was assigned:
> 
> $ perl -E '@a = ($a, $b) = 1 .. 100; say "a is ", join ", ", @a;'
> a is 1, 2
> $ perl -E '@a = () = 1 .. 100; say "a is ", join ", ", @a;'
> a is 
> 
> and the number of items on the right hand side of the assignment when
> it is in scalar context:
> 
> $ perl -E '$a = () = 1 .. 100; say "a is $a"'
> a is 100
> $ perl -E '$a = ($b) = 1 .. 100; say "a is $a"'
> a is 100
> 
> You can see how different approaches compare using the Benchmark
> module:
> 
> #!/usr/bin/perl
> 
> use strict;
> use Benchmark;
> use warnings;
> 
> my $max_hats = 100;
> 
> sub get_clown_hat {
>       return (0 .. $max_hats);
> }
> 
> # here, scalar context is causing the .. operator
> # to be the flip flop operator instead of the
> # range operator, so we get 1E0 instead of
> # 100 (the last item) or 101 (the count)
> # when not compared to anything, the range operator
> # compares against $. (the line number of the last
> # line read from the currently selected file handle)
> my $flip_flop_result = get_clown_hat;
> 
> print "wrong result: $flip_flop_result\n";
> 
> my %subs = (
>       array => sub {
>               my @a = get_clown_hat;
>               return scalar @a;
>       },
>       empty => sub {
>               my $count = () = get_clown_hat;
>               return $count;
>       },
> );
> 
> for my $sub (keys %subs) {
>     print "$sub: ", $subs{$sub}(), "\n";
> }
> 
> for my $n (1_000, 10_000, 100_000) {
>       $max_hats = $n;
> 
>       print "\n$n items\n\n";
>       Benchmark::cmpthese -2, \%subs;
> }
> 
> Avoiding copying the data is about twice as fast for counting items:
> 
> Use of uninitialized value $. in range (or flip) at bench.pl line 11.
> wrong result: 1E0
> array: 101
> empty: 101
> 
> 1000 items
> 
>          Rate array empty
> array 12620/s    --  -52%
> empty 26457/s  110%    --
> 
> 10000 items
> 
>         Rate array empty
> array 1310/s    --  -50%
> empty 2634/s  101%    --
> 
> 100000 items
> 
>        Rate array empty
> array 124/s    --  -49%
> empty 245/s   97%    --

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