Using my with parentheses and only one variable

前端 未结 5 805
梦谈多话
梦谈多话 2020-12-01 00:37

I sometimes see Perl code like this:

my ( $variable ) = blah....

What is the point of putting parentheses around a single variable? I thou

5条回答
  •  自闭症患者
    2020-12-01 01:36

    I'm not a Perl pro (by all means, I'm not), but AFAIK it has to do with lists. Perl has different contexts (scalar, list). Using ($var) switches to list context, $var is scalar context.

    my $var = (1, 2, 4); # $var = 4 (last element)
    my ($var) = (1, 2, 4); # $var = 1
    

    Please downvote this answer, if it is totally wrong :)

提交回复
热议问题