Skip to content

Instantly share code, notes, and snippets.

{
my %cache = {};
our sub long_running_thing( $x ) is export
{ # imagine your own long running method here
return %cache{$x} if ( %cache.exists($x) );
my $result;
# do some long running thing, generate result
%cache{$x} = $result;
{
my %cache = {};
sub long_running_thing { # imagine your own long running method here
my $x = shift;
return $cache{$x} if( exists $cache{$x} );
my $result;
# do some long running thing, generate result
return $result;
}
}
my $to_the_power_of = -> $x, $y { return $x ** $y}
$to_the_power_of(2,3)
my $sub = { return 'hello world' }
my $sub = sub { return 11; };
$sub.() # returns 11
$sub() # also returns 11
sub compound_interest( Real $present_value, Real $iterations, Real $rate where ( * > 1))
{
return $present_value * ( 1 + $iterations ) ** $rate;
}
sub compound_interest( Real $present_value, Real $iterations, Real $rate where ( * > 1))
{
return $present_value * ( 1 + $iterations ) ** $rate;
}
sub compound_interest( Real $present_value, Real $iterations, Real $rate)
{
return $present_value * ( 1 + $iterations ) ** $rate;
}
sub compound_interest
{
my ($present_value, $iterations, $rate) = @_;
return $present_value * ( 1 + $iterations ) ** $rate;
}
1..* # a list 1 to infinity
@array[*] # all the elements in the list
(*, *, $x) = (1,2,3) # $x = 3, 1 & 2 are discarded. This is similar to (undef, undef, $x) = (1,2,3) in Perl5
%hash{*} # all the values in the hash