You don't have the syntax quite right. Your examples call the subroutine (the &
bypasses any prototypes) and passes either the return value (&calculate()
) or a reference to it (\&calculate()
) to the -command
option. You want to assign a reference to the subroutine itself, which you can do via:
-command => \&calculate
Note the lack of trailing parentheses. Also note that you can't pass arguments this way. If you want to do that you need to either wrap the call in an anonymous subroutine:
-command => sub { calculate(12) }
or pass the option an ARRAY reference instead of a CODE reference:
-command => [\&calculate, 12]
There's a subtle difference between the two forms that's important if you use a variable instead of a literal value.
-command => sub { calculate($x) } # creates a closure over $x
-command => [\&calculate, $x] # makes a copy of $x
Using the first form changes to $x
will be visible when the command is invoked. Under the second form they won't be; each invocation will see the value at the moment the binding was created. Both forms are useful; you just need to exercise good judgment when deciding which to use.