2
0
mirror of https://github.com/chubin/cheat.sheets synced 2024-11-19 03:25:44 +00:00
cheat.sheets/sheets/_perl/subroutines

59 lines
1.3 KiB
Plaintext
Raw Normal View History

# The reserved word 'sub' defines a subroutine.
# Basic subroutine with on parameter:
sub mysub {
my $param = shift;
2018-07-27 17:55:28 +00:00
#
# Do stuff here
return $myvalue
}
$x = mysub( "value" )
# Named parameters to subroutines:
sub named_params_example {
my ( $arg ) = @_;
# Use my ( $self, $arg ) = @_; if you are in object land.
2018-07-27 17:55:28 +00:00
#
say $arg->{one};
say $arg->{two};
}
2018-07-27 17:55:28 +00:00
#
# Calling the above subroutine:
named_params_example({
one => "First argument",
two => "Second argument"
});
# Named parameters with default values:
sub named_params_with_defaults {
my ( $arg ) = @_;
# Use my ( $self, $arg ) = @_; if you are in object land.
#
# Set defaults
# If option given Use options Else
my $one = exists $arg->{one} ? $arg->{one} : "default1";
my $two = exists $arg->{two} ? $arg->{two} : "default2";
}
2018-07-27 17:55:28 +00:00
#
named_params_with_defaults({ one => "override arg 'one'" });
# Return named params from a subroutine:
sub foo {
my $clause = 'This is a clause';
my @param = qw/ one two thee /;
return ({
clause => $clause,
params => \@param
});
}
2018-07-27 17:55:28 +00:00
#
my $r = foo();
2018-07-27 17:55:28 +00:00
#
say "clause = [$r->{clause}]";
for my $p ( @{ $r->{params} } ) {
say "param = [$p]";
}
# More help with Perl's fabulous perldoc command:
perldoc sub