in reply to Question on use of 'my'

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

Replies are listed 'Best First'.
Re: Re: Question on use of 'my'
by Fletch (Bishop) on May 14, 2004 at 03:26 UTC

    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 ).