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

I have a question on the use of 'my' that i haven't been able to find answers to elsewhere.
sub function{ my ($a,$b,$c,$d); $a = 1; $b = 2; $c = 3; $d = 4; }
vs
sub function{ my $a = 1; my $b = 2; my $c = 3; my $d = 4; }
Is one more appropriate than the other? Does it matter? Should I declare local variables early on?

Yes, I know its a silly question -- its just bothering me.

Replies are listed 'Best First'.
Re: Question on use of 'my'
by Zaxo (Archbishop) on May 14, 2004 at 00:38 UTC

    There's a more compact third form which does it all in one line,

    sub function { my ($a,$b,$c,$d) = (1,2,3,4); }
    (with the oblig firework about $a and $b sacred to sort).

    All of them are functionally equivalent, perhaps differing a mite in how they are optimized.

    There are two schools about where to declare. The C school puts declarations right after their scope is opened. That is necessary in C, and people used to that like having all the variables listed in one place. The other way is to postpone until you have enough information to declare and initialize at the same time.

    It is largely a matter of style. Choose one you like and stick with it.

    Update: Fletch++, good point!

    After Compline,
    Zaxo

      Ditto's that it's mostly a matter of style, but there are contextual differences between them so they're not strictly equivalent. Keep in mind that my( $x, $y ) = EXPR provides list context to the expression which can affect the value to which it evaluates.

      Update: Just to clarify with a common gotcha, my $foo = /(bar|baz)/; is not the same as my( $foo ) = /(bar|baz)/ (but the later is the same as my $foo = ( /(bar|baz)/ )[0]).

      Update: Or another one: my $x = ( 3, 2, 1 ) versus my( $x ) = ( 3, 2, 1 ).

Re: Question on use of 'my'
by NetWallah (Canon) on May 14, 2004 at 00:32 UTC
    No significant performance difference between your to examples. However I would do it thus:
    sub function{ my ($a,$b,$c,$d) = qw (1 2 3 4); }

    Offense, like beauty, is in the eye of the beholder, and a fantasy.
    By guaranteeing freedom of expression, the First Amendment also guarntees offense.
      Great to hear!

      That's not actually how its being used - I guess that I misrepresented the questin with that example. I just wanted to know if it was more proper to declare a list of local variables when you enter a block, than declaring variables to be local as you introduce them
        While there are certanly many situations where you might want to have a list of a block's variables declared near the top, there are just as many (if not more) cases where clarity is improved by declaring them as you need them, and no sooner.

        Whenever it makes sense, I like to work it out to where lexical variables have the smallest scope possible, for the briefest amount of code.

        For example:

        sub myfunc { my( $this, $that, @those ) = @_; # Here it may make sense to pull # in the contents early on for # simplicity and clarity's sake. # Code here.... # more code here.... foreach my $alias ( @those ) { # Note we declare $alias only # where it's needed. # Do some stuff here. # Do more stuff... my $another = $alias * 5; # $another is limited to the sco +pe # of the foreach loop. $this += $another; } while ( my $choice = shift @those ) { # Another tightly scoped var +. # Do something else. } return $this }

        This is contrived, but the idea is that I'm keeping scopes narrow, and declaring variables only where they're needed. To me, this helps to keep the declaration near where the variable is actually used, and that, in my opinion, improves readability and maintainability.


        Dave

Re: Question on use of 'my'
by duff (Parson) on May 14, 2004 at 13:59 UTC
    IMHO, you should declare your variables as close to their first use as possible, so always declaring them at the top of your sub in one big block isn't good :-). WRT, one my versus several, I'll often declare one variable per line when I want to put some commentary off to the side about that variable.