DavidSzczesniak
12/13/2017 - 1:07 PM

Function References

Create a function references by simply using the reference operator and the function sigl(&) on the name of the function.

# Simple example
sub bake_cake { say 'Baking a wonderful cake!'};

my $cake_ref = \&bake_cake;

# Create an anonymous function
my $pie_ref = sub{say 'Making a delicious pie!'};
# Using this built-in bareword without a name does not register it with the current namespace. 
# to access this function, invoke it with the dereferencing arrow:
$cake_ref->();
$pie_ref->();
# Pass arguments to the function within the parentheses:
$bake_something_ref -> ('cupcakes');

\!h # This time, avoid using the alternate invocation syntax '&$' as it has subtle implications for parsing and argument passing.