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

In reply to Re: variable declaration question by Tommy
in thread variable declaration question by dicty

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.