-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubroutines.pl
43 lines (30 loc) · 1.23 KB
/
subroutines.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
sub hyphenate {
# Extract the first argument from the array, ignore everything else
my $word = shift @_;
# An overly clever list comprehension
$word = join "-", map { substr $word, $_, 1 } (0 .. (length $word) - 1);
return $word . "\n";
}
if ($ARGV[0]){
print hyphenate($ARGV[0]); # "e-x-t-e-r-m-i-n-a-t-e"
} else{
print "I need minimum one string argument"
}
#Unlike almost every other major programming language, Perl calls by reference. This means that the variables or values available inside the body of a subroutine are not copies of the originals. They are the originals.
my $x = 7;
sub reassign {
$_[0] = 42;
}
reassign($x);
print $x; # "42"
#Like other Perl expressions, subroutine calls may display contextual behaviour. You can use the wantarray function (which should be called wantlist but never mind) to detect what context the subroutine is being evaluated in, and return a result appropriate to that context:
sub contextualSubroutine {
# Caller wants a list. Return a list
return ("Everest", "K2", "Etna") if wantarray;
# Caller wants a scalar. Return a scalar
return 3;
}
my @array = contextualSubroutine();
print @array."\n"; # "EverestK2Etna"
my $scalar = contextualSubroutine();
print $scalar."\n"; # "3"