25

What's the difference between my ($variableName) and my $variableName in Perl? What to the parentheses do?

brian d foy
  • 129,424
  • 31
  • 207
  • 592
John
  • 5,139
  • 19
  • 57
  • 62
  • 1
    Interesting Perl Monks answer [here](http://www.perlmonks.org/?node_id=693666) that ultimately boils down the use of parens in declaration-assignments as an end run around ungainly operation precedence. – ruffin Feb 09 '15 at 21:36
  • Full, detailed answer here: [Scalar vs List Assignment Operator](https://stackoverflow.com/q/54564428/589924). – ikegami Feb 07 '19 at 00:16

4 Answers4

21

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.

mob
  • 117,087
  • 18
  • 149
  • 283
  • 10
    So in essence: the brackets 1. provide list context, and 2. distribute the operator or function across multiple values. – Ether Jan 24 '10 at 08:20
  • 2
    #2 is technically incorrect and potentially misleading. It is incorrect in that the way the declaration with the parens works is by defining a lexical list rather than a lexical scalar. It is misleading in that a beginner may read "the brackets... distribute the operator or function across multiple values" and expect `($x, $y) = (1, 2) + 3` to assign the values 4 to `$x` and 5 to `$y` by "distributing the + operator across multiple values". (In actuality, that statement assigns 5 to `$x` and nothing to `$y`.) – Dave Sherohman Jan 24 '10 at 10:24
  • 3
    #1 is not completely correct either. The parens on the lefthand side of an assignment provide list context, but that doesn't mean they provide list context everywhere else. – brian d foy Jan 24 '10 at 17:26
  • Begin your rabbit hole on cargo cults [here](http://en.wikipedia.org/wiki/Cargo_cult_programming). – ruffin Feb 09 '15 at 21:31
5

Please look at perdoc perlsub for more information on the my operator. Here's a small excerpt:

Synopsis:

   my $foo;            # declare $foo lexically local
   my (@wid, %get);    # declare list of variables local
   my $foo = "flurp";  # declare $foo lexical, and init it
   my @oof = @bar;     # declare @oof lexical, and init it
   my $x : Foo = $y;   # similar, with an attribute applied
Thunder Rabbit
  • 5,405
  • 8
  • 44
  • 82
ghostdog74
  • 327,991
  • 56
  • 259
  • 343
5

The short answer is that parentheses force list context when used on the left side of an =.

Each of the other answers point out a specific case where this makes a difference. Really, you should read through perlfunc to get a better idea of how functions act differently when called in list context as opposed to scalar context.

brian d foy
  • 129,424
  • 31
  • 207
  • 592
EmFi
  • 23,435
  • 3
  • 57
  • 68
2

As the other answer and comments explain usage of brackets provide list context to the variable. Below is a code snippet that provides some more explanation by making use of the Perl function split.

use strict;

my $input = "one:two:three:four";

# split called in list context
my ($out) = split(/:/,$input);
# $out contains string 'one' 
#(zeroth element of the list created by split operation)
print $out,"\n";

# split called in scalar context
my $new_out = split(/:/,$input);
# $new_out contains 4 (number of fields found)
print $new_out,"\n";
Agostino
  • 2,723
  • 9
  • 48
  • 65
sateesh
  • 27,947
  • 7
  • 36
  • 45