I sometimes see Perl code like this:
my ( $variable ) = blah....
What is the point of putting parentheses around a single variable? I thought parentheses were only used when declaring multiple variables, like:
my ( $var1, $var2, $var3 ) = blah...
There are several scenarios when there is a difference:
When array is on right side
my @array = ('a', 'b', 'c'); my $variable = @array; # 3 size of @array my ($variable) = @array; # 'a' $array[0]
When list is on right side
my $variable = qw/ a b c d /; # 'd' last item of the list my ($variable) = qw/ a b c d /; # 'a' first item of the list
Subroutine with variable (array/scalar) return value
sub myFunction { ... return (wantarray() ? @array : $scalar); } my $variable = myFunction(...); # $scalar from the subroutine my ($variable) = myFunction(...); # $array[0] from the subroutine
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With