in reply to Action at a great distance

This is a bad idea because ..
The same set of @args are passed as ALIASES to the subs. The subs could modify the @args and have un-intended side effects.

Here is an easy, possibly inefficient workaround by copying the @args:

foreach (@callbacks) { $_->((@args)); }
Update: Oops - this was untested. Use $_->( @{ @args } );

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

Replies are listed 'Best First'.
Re: Re: Action at a great distance
by chromatic (Archbishop) on Mar 17, 2004 at 19:46 UTC

    Did you test this?

    use strict; use Test::More tests => 1; my @cb = ( sub { $_[0] = reverse $_[0] }, sub { $_[1] = reverse $_[1] }, ); my @args = qw( one two ); for my $cb (@cb) { $cb->((@args)); } is_deeply( \@args, [qw( one two )] );

    Besides that, any argument passing in Perl has that drawback.

Re: Re: Action at a great distance
by etcshadow (Priest) on Mar 17, 2004 at 20:13 UTC
    Dude... those parentheses don't cause the array to be copied. If you want do an in-line copy like that, I recommend:
    foreach (@callbacks) { $_->( @{[ @args ]} ); }
    ------------ :Wq Not an editor command: Wq
        bracket happy? :)

        $->( @{ \@args } );

        No, actually... by saying @{ \@list }, you're just referrencing and dereferrencing the same list. In order to make an inline copy of the list, you'd need to add back in there a second, new (anonymous) array.
        [me@host]$ perl -le 'sub foo { $_[0]++ } @x = (0,1); foo(@x); print "@ +x"' 1 1 [me@host]$ perl -le 'sub foo { $_[0]++ } @x = (0,1); foo(@{ \@x }); pr +int "@x"' 1 1 [me@host]$ perl -le 'sub foo { $_[0]++ } @x = (0,1); foo(@{[ @x ]}); p +rint "@x"' 0 1 [me@host]$
        Please... if you're going to "correct" someone, then make sure that you are correct and that (s)he is incorrect.
        ------------ :Wq Not an editor command: Wq
Re: Re: Action at a great distance
by Fletch (Bishop) on Mar 17, 2004 at 20:53 UTC

    Erm, and that's any different than explicitly calling multiple subs with the same array as an argument how? Just because you're using a coderef to call it or not doesn't change the fact that subs receive aliases.

    ## I.e. just as "dangerous" foo( @args ); blah( @args ); zorch( @args );