in reply to Interpret array elements as actual Perl?

You'd better go with an array of anonymous subs, IMHO.

my @commands = ( sub {print "hello\n"}, sub {print "world\n"} ); foreach my $command (@commands) { &{$command}; }

Replies are listed 'Best First'.
Re: Re: Interpret array elements as actual Perl?
by meetraz (Hermit) on May 28, 2004 at 18:38 UTC
    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)

      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->();)