raghuprasad241 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

I am new to perl, however I have other programming experience in python and java from school.
I picked up "programming perl" book recently and while going through it encountered some thing like below.

$a=()=(1,2,3,4,5)

It does not have much explanation as to what is happening under the hood.
To my eyes it looks like list on the right hand side is being assigned to empty list and are discarded. When I print $a its printing 5.
Can any monk shed some light on this please ?

Thanks!
Rookie Monk

Replies are listed 'Best First'.
Re: perl () magic and how does it work ?
by toolic (Bishop) on Jan 13, 2016 at 15:40 UTC
Re: perl () magic and how does it work ?
by stevieb (Canon) on Jan 13, 2016 at 15:59 UTC
Re: perl () magic and how does it work ?
by Anonymous Monk on Jan 13, 2016 at 17:19 UTC

    Tip: Don't test with lists whose last item is the same as the number of elements.

    my $x = (1,2,3); my $y =()= (1,2,3); print "$x / $y\n"; # prints "3 / 3" $x = ("a","b","c"); $y =()= ("a","b","c"); print "$x / $y\n"; # prints "c / 3"

    See also Comma Operator

Re: perl () magic and how does it work ?
by hotchiwawa (Scribe) on Jan 13, 2016 at 15:59 UTC
    It gives the number of elements ;)
Re: perl () magic and how does it work ?
by Discipulus (Canon) on Jan 13, 2016 at 22:24 UTC
    just a side note raghuprasad241 remember to NOT use $a as variable name

    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: perl () magic and how does it work ?
by raghuprasad241 (Beadle) on Jan 13, 2016 at 16:57 UTC
    Thank you all the monks for the responses. It helped a lot and cleared my confusion :-)

    Thanks!