Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

need an explanation

by Lennotoecom (Pilgrim)
on Sep 22, 2013 at 21:50 UTC ( #1055202=perlquestion: print w/ replies, xml ) Need Help??
Lennotoecom has asked for the wisdom of the Perl Monks concerning the following question:

Hi, can you explain please
how exactly this snippet works,
and if possible context transformation:
@a = qw(a b c d e); $b = () = @a; # $b now is 5
Thanks!

update:
$b = @a;
would do the same effect,
so basically there is no need for ();
but still, the question remains if someone got
any explanation

update update:
thank you for answer Mr. Choroba
yes indeed that was just the wrong example that was used by some fellow
your last example was helpful
thank a lot

Comment on need an explanation
Download Code
Re: need an explanation
by choroba (Abbot) on Sep 22, 2013 at 21:55 UTC
    The first line is equivalent to
    @a = ('a', 'b', 'c', 'd', 'e');

    qw just "quotes words".

    The second line is usually written as

    $b = @a;

    or, for more clarity,

    $b = scalar @a;

    Perl is sensitive to context. By assigning to a scalar value, you impose the scalar context on the right hand side of the assignment. Arrays return their size (number of elements) in scalar context. Using = () = here has no effect and is only confusing. Update: It is usually used to force list context, for example

    $x = () = "abcdABCabA" =~ /a/ig; # Count A's regardless of case.
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1055202]
Approved by Albannach
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2014-09-23 03:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (210 votes), past polls