What’s the difference between my ($variableName) and my $variableName in Perl?

The important effect is when you initialize the variable at the same time that you declare it:

my ($a) = @b;   # assigns  $a = $b[0]
my $a = @b;     # assigns  $a = scalar @b (length of @b)

The other time it is important is when you declare multiple variables.

my ($a,$b,$c);  # correct, all variables are lexically scoped now
my $a,$b,$c;    # $a is now lexically scoped, but $b and $c are not

The last statement will give you an error if you use strict.

Leave a Comment