in reply to Re: Interpret array elements as actual Perl?
in thread Interpret array elements as actual Perl?

Isn't this better written as:

use strict; my @commands = ( sub {print "hello\n"}, sub {print "world\n"}, ); foreach my $command (@commands) { $command->(); # Or use args like this: $command->($arg1,$arg2); }
(Using subname() instead of &subname)

Replies are listed 'Best First'.
Re: Re: Re: Interpret array elements as actual Perl?
by calin (Deacon) on May 28, 2004 at 18:57 UTC

    I think it's a matter of taste. It may look cleaner in a cascaded dereference construct, but I tend to avoid it as much as I can for religious reasons (it looks OOPish ;) )

    You can pass arguments with the ampersand syntax too: &$command ($arg1, $arg2);

    Update: There are issuse with a bare & call and @_, see code:

    $ perl sub foo { my $x = sub {print "@_"}; &{$x}; } foo qw/bar baz/; ^D bar baz

    If you don't want to transfer @_ use &{$x} (); (or $x->();)