Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

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 (Bishop) 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
[haukex]: Discipulus: which one do you mean?
[choroba]: Hermit

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2018-06-22 08:09 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (122 votes). Check out past polls.