Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Why parens?

by chazubell (Sexton)
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?

Comment on Why parens?
Select or Download Code
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 (Canon) 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
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (13)
As of 2015-11-30 11:57 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (769 votes), past polls