Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

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;

Vs.

my ($a);

Is this just an aesthetic issue?

Comment on Why parens?
Select or Download Code
Re: Why parens?
by choroba (Abbot) 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";
    Output:
    1 x.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
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 johngg (Abbot) on Feb 07, 2013 at 17:23 UTC

    This thread discusses the differences.

    Cheers,

    JohnGG

      JohnGG:

      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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2014-07-29 21:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (228 votes), past polls