From 2243178be4300769c7b604e1317276a0b80a4b36 Mon Sep 17 00:00:00 2001 From: Neil H watson Date: Sun, 3 Dec 2017 11:06:03 -0500 Subject: [PATCH] Intial attempt at a Perl entry on subroutines --- sheets/_perl/subroutines | 67 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 sheets/_perl/subroutines diff --git a/sheets/_perl/subroutines b/sheets/_perl/subroutines new file mode 100644 index 0000000..814a8a1 --- /dev/null +++ b/sheets/_perl/subroutines @@ -0,0 +1,67 @@ +# The reserved word 'sub' defines a subroutine. + +# Basic subroutine with on parameter: + +sub mysub { + my $param = shift; + + # 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. + + say $arg->{one}; + say $arg->{two}; +} + +# Calling the above subrouting: + +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"; +} + +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 + }); +} + +my $r = foo(); + +say "clause = [$r->{clause}]"; +for my $p ( @{ $r->{params} } ) { + say "param = [$p]"; +} + +# More help with Perl's fabulous perldoc command: + +perldoc sub