http://www.perlmonks.org?node_id=492402


in reply to Probably an easy one - store command in variable.

Greetings all,
I agree with Roy Johnson about the slashes. you are trying to make @results a reference on the left hand side, which is why you are getting the error.
Here are my suggestions for modifying your code.
my $command = sub { my @returnstring = $d->checklist( text => $text, list => [ @list ] + ); return \@returnstring; }; my $some_ref = &$command;
Now if you wanted the array from $d->checklist() to return as a reference itself you might try.
my $command = sub { my $returnstring = \($d->checklist( text => $text, list => [ @list + ] )); return $returnstring; }; my $some_ref = &$command;
However if you already have declared @returnstring outside of the scope of your anonymous sub and you wish to use this sub to modify it then you could simply do something like this...
my @returnstring; my $command = sub { @returnstring = $d->checklist( text => $text, list => [ @list ] ); };
again assuming that $d->checklist() returns an array.
A simple way to think of it is that references get assigned to something. So the \ is allowed on the right hand side only.
At least thats what works for me. :)
These examples are untested but I think, as Roy Johnson pointed out, that your slashes are the problem.

-InjunJoel
"I do not feel obliged to believe that the same God who endowed us with sense, reason and intellect has intended us to forego their use." -Galileo