in reply to Re: Strange benchmark result that has me totally confounded
in thread Strange benchmark result that has me totally confounded

But I'm not modifying @messages, am I?

Unless I'm missing something, I'm only modifying $post. The value of @messages isn't touched during the foreach loop because data from the array is copied into the scalar.

That said, I see a change in performance as well when I use local to create a copy of @messages, and I just don't get it.

  • Comment on Re: Re: Strange benchmark result that has me totally confounded

Replies are listed 'Best First'.
Re: Re: Re: Strange benchmark result that has me totally confounded
by simonm (Vicar) on Apr 17, 2004 at 00:32 UTC
    But I'm not modifying @messages, am I?

    Surprise! You are, due to the magic of variable aliasing:

    my @messages = <DATA>; foreach my $post ( @messages ) { $post =~ s/ /-/g; } print @messages; __DATA__ one foo bar three words here
Re: Re: Re: Strange benchmark result that has me totally confounded
by chromatic (Archbishop) on Apr 16, 2004 at 23:15 UTC

    As perldoc perlsyn says:

    In other words, the "foreach" loop index variable is an implicit alias for each item in the list that you're looping over.

    I don't think local will do it either; not unless you want an empty array or are doing something a lot more clever than I can imagine. You need to reset the values of the array for each benchmark iteration.