programming-examples/perl/Subroutine/Using the return statement.pl
2019-11-15 12:59:38 +01:00

24 lines
501 B
Perl

#!/usr/local/bin/perl
$total = &get_total;
if ($total eq "error") {
print ("No input supplied.\n");
} else {
print("The total is $total.\n");
}
sub get_total {
$value = 0;
$inputline = <STDIN>;
$inputline =~ s/^\s+|\s*\n$//g;
if ($inputline eq "") {
return ("error");
}
@subwords = split(/\s+/, $inputline);
$index = 0;
while ($subwords[$index] ne "") {
$value += $subwords[$index++];
}
$retval = $value;
}