in reply to howto reference to a external sub/object/constructor thingy
Just to explain the required change:
\&function returns a reference to function function. Temp::Tool->new is not a function — it's a Perl expression — so \& can't be used. sub { ... } creates an (anonymous) function and returns a reference to it. All you have to do is to create a small function which executes your Perl expression.
That trick can be used even if you have arguments, thanks to closures. For example,
printsmy $func; { my $arg1 = 'Hello'; $func = sub { my ($arg2) = @_; print("$arg1 $arg2\n"); } } { my $arg1 = 'Goodbye'; $func->('World'); }
Hello World
Lexical (my) variables are captured when sub is executed, and arguments can still be passed in as a normal function.
|
|---|