Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

variable declaration question

by dicty (Sexton)
on Jan 18, 2013 at 20:53 UTC ( #1014122=perlquestion: print w/ replies, xml ) Need Help??
dicty has asked for the wisdom of the Perl Monks concerning the following question:

What's the difference between "my $var;" and "my ($var);" in variable declaration?

Thanks, Dicty

Comment on variable declaration question
Re: variable declaration question
by tobyink (Abbot) on Jan 18, 2013 at 21:00 UTC

    None.

    If you're declaring and assigning multiple variables at the same time, the version with the parentheses is more useful...

    my ($one, $two, $three) = 1 .. 3;
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: variable declaration question
by MidLifeXis (Prior) on Jan 18, 2013 at 21:04 UTC

    To add to what tobyink said, "none" - in context.

    my $foo = @some_array_value and my ($foo) = @some_array_value will give two very different results.

    print(my $foo = (1, 2, 3), "\n"); print(my ($foo) = (1, 2, 3), "\n"); __END__ 3 1

    Update: Alright, either QuickDraw McGraw updated his node, or I skipped over the second and following line when I read it. :-) We stated basically the same thing.

    --MidLifeXis

      I updated. It was an afterthought as it didn't seem relevant to the question. (The question included semicolons after the declaration, so the variable is not being initialized.)

      perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
      print(my $foo = (1, 2, 3), "\n"); print(my ($foo) = (1, 2, 3), "\n"); __END__ 3 1

      Ah, I see. The line:

      print(my $foo = (1, 2, 3), "\n");

      is a convenient way of assigning the last element of an array to a variable.

        Try:
        print(my $foo = ('a', 'b', 'c'), "\n");
        vs:
        my @foo = ('a', 'b', 'c'); my $bar = @foo; print $bar, "\n";
        I like using letters vs numbers to avoid confusion...
Re: variable declaration question
by BillKSmith (Chaplain) on Jan 18, 2013 at 21:09 UTC

    The parenthesies can also be used to force list context.

    my $string = 'aaa1234bbb'; my ($number) = $string =~ /(\d+)/;
    Bill
Re: variable declaration question
by blue_cowdawg (Prior) on Jan 18, 2013 at 21:34 UTC
        What's the difference between "my $var;" and "my ($var);" in variable declaration?

    Strictly speaking, the way you've presented the above not much except you fingers are slightly more tired. That said consider the following code:

    sub marine { my ($fore,$mid,$aft) = @_; }
    In the above example I have an array on the LHS and an array on the RHS. This is equivalent to:
    sub marine { my $fore = $_[0]; my $mid = $_[1]; my $aft = $_[2]; }
    When coding parens, just like in written English, are used to group things together.

    Clear as mud?


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
      In the above example I have an array on the LHS and an array on the RHS. This is equivalent to:

      Strictly speaking... this not true. You have a list on the LHS, as per perlfaq4: What is the difference between a list and an array:

      A list is a fixed collection of scalars. An array is a variable that holds a variable collection of scalars. An array can supply its collection for list operations, so list operations also work on arrays

      ...

      Array operations, which change the scalars, rearranges them, or adds or subtracts some scalars, only work on arrays. These can't work on a list, which is fixed. Array operations include shift, unshift, push, pop, and splice.

Re: variable declaration question
by Tommy (Chaplain) on Jan 18, 2013 at 21:49 UTC
    What's the difference between "my $var;" and "my ($var);" in variable declaration?

    The difference is that the first version enforces scalar context on the assignment, and the second imposes list context. (Mark Jason Dominus explains this better.)

    These may seem like insignificant differences, but can lead to nasty bugs in code if, say, you have a manager who decides to make it a "coding practice" to put parens (around) (all) (variable) (declarations) (everywhere) in the name of "consistency". *see footnote1

    Parens() aren't a matter of style. For the most part, parenthesis serve three purposes in perl: they impose list context, they denote/force sub/method invocation, and assist in forcing precedence in order of operations on an expressions with multiple invocants.

    For single variable assignment, you usually want scalar context, which is why it should _NEVER_ be a coding practice to use one form or the other because it isn't an issue of style -- it's an issue of behavior.

    Furthermore, when you use my($foo) = $obj->method() as a matter of practice, then it completely breaks upstream interfaces using the wantarray() builtin to produce polymorphic results for you. And if you're a real jerk, you'll blame the designer of the upstream interface for your own lack of understanding. *facepalm*

    This wouldn't be such a big deal to me if it hadn't been such a bad experience to learn it the hard way. I'm pretty sure that the perils of misunderstanding scalar vs. list context are a big reason why it is one of the first things many Perl books and instructors cover nowadays, and if they don't, they #$%@! well should.

    1: my($sad_experience) = (it, has, happened, to, me, ;_;)

    Tommy
    A mistake can be valuable or costly, depending on how faithfully you pursue correction

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (10)
As of 2014-07-28 18:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (206 votes), past polls