Perl v5.20 combines multiple my() statements

Perl v5.20 continues to clean up and optimize its internals. Now perl optimizes a series of lexical variable declarations into a single list declaration.

You can write them out as separate statements:

my( $dog );
my( $cat );
my( $bird );

v5.20 turns that into a single declaration.

my( $dog, $cat, $bird );

That doesn’t sound like a bit deal, but it saves several steps, and steps translate to time no matter what they do.

Remember where you’re likely to declare lexical variables—at the start of subroutines. Call the subroutine enough times and those little bits of time add up:

sub foo {
	my( $dog );
	my( $cat );
	my( $bird );

	...
	}

With v5.20, either way you write it you get the faster form:

sub foo {
	my( $dog, $cat, $bird );
	...
	}

If I’m not initializing variables, I initialize with a list anyway, but I tend to declare variables as close to their use as I can get away with, which often means that I don’t list them at the top of the subroutine. Writing short subroutines, however, means I probably have them near the top.

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit

Leave a comment

0 Comments.

Leave a Reply

You must be logged in to post a comment.