Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Why parens?

by chazubell (Beadle)
on Feb 07, 2013 at 17:14 UTC ( #1017701=perlquestion: print w/replies, xml ) Need Help??
chazubell has asked for the wisdom of the Perl Monks concerning the following question:

I have been provided some code that declare a lexically scoped scalar variable enclosed with parenthesis’s.

my $a;


my ($a);

Is this just an aesthetic issue?

Replies are listed 'Best First'.
Re: Why parens?
by tobyink (Abbot) on Feb 07, 2013 at 17:22 UTC

    When declaring only one variable, it's mostly an aesthetic choice; but consider declaring multiple variables simultaneously...

    my ($a, $b, $c); # the good my $a, $b, $c; # the bad (i.e. broken) my $a, my $b, my $c; # and the ugly
    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
Re: Why parens?
by choroba (Chancellor) on Feb 07, 2013 at 17:21 UTC
    If the declaration is not followed by an assignment, it is purely (un)aesthetic. If there is an assignment, the difference is important - the parentheses introduce list context on the rvalue. For example:
    $_='....x....x....x...'; my $scalar = /x/g; my ($list) = /x/g; print $scalar, "\t", $list, ".\n";
    1 x.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Why parens?
by johngg (Abbot) on Feb 07, 2013 at 17:23 UTC

    This thread discusses the differences.




      Thanks for the pointer to the discussion of parens. In the context per my example, it would appear that the programmer may have cribbed it from some other code (as do we all).

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1017701]
Approved by igelkott
[stevieb]: ohhh, aaaand, I just learned that you can power an Arduino (Uno at least) by connecting any single analog pin to 5v on the Pi GPIO! That's bloody handy

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2017-06-22 23:03 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (531 votes). Check out past polls.