Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Declaring my $var versus declaring my($var)

by dh1760 (Acolyte)
on Jun 10, 2011 at 13:51 UTC ( [id://909123]=perlquestion: print w/replies, xml ) Need Help??

dh1760 has asked for the wisdom of the Perl Monks concerning the following question:

I've been coding Perl since LWall posted in comp.lang.perl (a long time), and years ago I settled on a consistent coding style where modifiers are always followed by a parenthetical list, regardless of the number of variables being declared:

my($a) = 1; my($b,c,d); our(%list) = (); local(*p);

Now, in my old age, I'm wondering if I've been making the parser work extra hard, or even causing less efficient code to be generated. So, better late than never, I'm wondering: is there any practical difference between using parenthesis for a single variable declaration (my($var);) and omitting them (my $var;)?

--
DaveH (dh1760)

Replies are listed 'Best First'.
Re: Declaring my $var versus declaring my($var)
by moritz (Cardinal) on Jun 10, 2011 at 14:08 UTC
    If you initialize your variables, the () makes a difference: context.
    use strict; use warnings; sub print_context { say wantarray ? 'list' : 'scalar'; } my $x = print_context; my ($y) = print_context; __END__ scalar list

    Examples where this makes a difference:

    my $time1 = localtime; my ($time2) = localtime; my $rev1 = reverse $some_string; my ($rev2) = reverse $some_string;

      Now I understand why "in scalar context returns ..." has sometimes not returned what I expected. Amazing that I've probably coded tens of thousands of lines of perl over the years and have rarely been bitten by this difference. I suspect it's because any time it has caused an unexpected result, I've just figured out a different way to get what I needed.

      I guess we're never too old to learn ... thanks for the lesson!

      --
      DaveH (dh1760)

Re: Declaring my $var versus declaring my($var)
by jwkrahn (Abbot) on Jun 10, 2011 at 14:13 UTC

    In your example my($a) = 1; the parentheses impose list context on the assignment, which in this case is OK because you are only assigning a list of one element, but it will make a difference on functions that distinguish between scalar and list context (i.e. localtime).

    In your example our(%list) = (); the parentheses around %list are redundant because %list by itseft already enforces list context.

Re: Declaring my $var versus declaring my($var)
by Animator (Hermit) on Jun 10, 2011 at 14:20 UTC
    The following message is a combination of the output of
    $ perl -MO=Deparse -wle 'my ($a) = 1;my $b = 2; (my $c) = 3;my ($d, $e +) = (4, 5); (my $f, my $g) = (6, 7); (my($h, $i) = (8, 9));my $j; my +($k);' $ perl -MO=Concise -wle 'my ($a) = 1;my $b = 2; (my $c) = 3;my ($d, $e +) = (4, 5); (my $f, my $g) = (6, 7); (my($h, $i) = (8, 9));my $j; my +($k);'
    • my ($a) = 1;
      deparse: my($a) = 1; concise: 7 <2> aassign[t2] vKS ->8 - <1> ex-list lK ->5 3 <0> pushmark s ->4 4 <$> const(IV 1) s ->5 - <1> ex-list lK ->7 5 <0> pushmark s ->6 6 <0> padsv[$a:1,7] lPRM*/LVINTRO ->7
    • my $b = 2;
      deparse: my $b = 2; concise: b <2> sassign vKS/2 ->c 9 <$> const(IV 2) s ->a a <0> padsv[$b:2,7] sRM*/LVINTRO ->b
    • (my $c) = 3;
      deparse: my($c) = 3; concise: h <2> aassign[t5] vKS ->i - <1> ex-list lK ->f d <0> pushmark s ->e e <$> const(IV 3) s ->f - <1> ex-list lK ->h f <0> pushmark s ->g g <0> padsv[$c:3,7] lPRM*/LVINTRO ->h
    • my ($d, $e) = (4, 5);
      deparse: my($d, $e) = (4, 5); concise: p <2> aassign[t8] vKS ->q - <1> ex-list lKP ->m j <0> pushmark s ->k k <$> const(IV 4) s ->l l <$> const(IV 5) s ->m - <1> ex-list lKPRM*/128 ->p m <0> pushmark sRM*/128 ->n n <0> padsv[$d:4,7] lRM*/LVINTRO ->o o <0> padsv[$e:4,7] lRM*/LVINTRO ->p
    • (my $f, my $g) = (6, 7);
      deparse: my($f, $g) = (6, 7); consise: x <2> aassign[t11] vKS ->y - <1> ex-list lKP ->u r <0> pushmark s ->s s <$> const(IV 6) s ->t t <$> const(IV 7) s ->u - <1> ex-list lKPRM* ->x u <0> pushmark sRM* ->v v <0> padsv[$f:5,7] lRM*/LVINTRO ->w w <0> padsv[$g:5,7] lRM*/LVINTRO ->x
    • (my($h, $i) = (8, 9);
      deparse: my($h, $i) = (8, 9); concise: 15 <2> aassign[t14] vKPS ->16 - <1> ex-list lKP ->12 z <0> pushmark s ->10 10 <$> const(IV 8) s ->11 11 <$> const(IV 9) s ->12 - <1> ex-list lKPRM*/128 ->15 12 <0> pushmark sRM*/128 ->13 13 <0> padsv[$h:6,7] lRM*/LVINTRO ->14 14 <0> padsv[$i:6,7] lRM*/LVINTRO ->15
    • my $j;
      deparse: my $j; concise: 17 <0> padsv[$j:7,9] vM/LVINTRO ->18
    • my ($k);
      deparse: my $k; concise: 19 <0> padsv[$k:8,9] vPM/LVINTRO ->1a

    Summary:

    • my ($a) = 1; and (my $c) = 3; are identical in the OPs it generates. Thus there is no difference in which one you use
    • my ($a) = 1; and my $b = 2; are not identical. The generates OPs are different and so is the behaviour (as pointed out already)
    • my ($d, $e) = (4, 5); and (my $f, my $g) = (6, 7); and (my($h, $i) = (8, 9)); are identical
    • my $j; and my ($k); are identical in the OPs it generates. Thus there is no difference in which one you use

    Updated: updated to include my $j; and my ($k);

      This is fascinating, thanks for the pointer to Deparse and Concise!

      --
      DaveH (dh1760)

Re: Declaring my $var versus declaring my($var)
by johngg (Canon) on Jun 10, 2011 at 14:25 UTC

    This node also discussed the issue.

    Cheers,

    JohnGG

Re: Declaring my $var versus declaring my($var)
by ikegami (Patriarch) on Jun 10, 2011 at 14:48 UTC

    The only difference between my $x and my ($x) is when it's found on the LHS of a "=". See Mini-Tutorial: Scalar vs List Assignment Operator for how it affects the assignment.

    There is no difference between my ($x) and (my $x) except that my ($x, $y) is shorter than (my $x, my $y).

Re: Declaring my $var versus declaring my($var)
by chromatic (Archbishop) on Jun 10, 2011 at 18:11 UTC
    I'm wondering if I've been making the parser work extra hard...

    Yes, but the practical effect is irrelevant. I doubt you can even measure it to any degree of accuracy.

    Apart from deliberate grouping or context enforcing behavior, the only reason to use parentheses is to produce clarity for human readers. The parser has to do much more work figuring out other constructs anyhow.

Re: Declaring my $var versus declaring my($var)
by biohisham (Priest) on Jun 10, 2011 at 14:16 UTC
    is there any practical difference between using parenthesis for a single variable declaration (my($var);) and omitting them (my $var;)?
    I can't test if there's any benchmark performance difference or if Perl::Critic has something to say about either styles, parenthesis impose list context in this scenario, and a list can be made of one or more elements.

    This Post is in line and discusses a similar matter. The assignment to a scalar variable of a return value from a function (i.e such as stat()) or from an array gives the variable a value depending on the context in which the variable has been assigned, quoting an example below:

    my ($var) = @array ; my $var = @array;


    Excellence is an Endeavor of Persistence. A Year-Old Monk :D .

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://909123]
Approved by ww
Front-paged by biohisham
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-04-25 10:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found