Skip to content

Instantly share code, notes, and snippets.

@tacitochaves
Created May 20, 2014 01:32
Show Gist options
  • Save tacitochaves/ab36c5ad1bf9cd687e46 to your computer and use it in GitHub Desktop.
Save tacitochaves/ab36c5ad1bf9cd687e46 to your computer and use it in GitHub Desktop.
Operação
#!/usr/bin/env perl
use 5.014;
say "####################################";
say "Program of sum and difference.";
say "####################################";
say "Entre com a opção desejada.";
say "1 - For sum";
say "2 - For difference";
say;
chomp( my $opcao = <STDIN> );
if ( $opcao ne 1 and $opcao ne 2 ) {
say "Opção inválida";
exit(1);
} elsif ( $opcao eq 1) {
print "Please, inform the first number: ";
chomp( my $num1 = <STDIN> );
print "Please, inform the second number: ";
chomp( my $num2 = <STDIN> );
say &sum($num1,$num2);
} else {
print "Please, inform the first numbeer: ";
chomp( my $num1 = <STDIN> );
print "Please, inform the second number: ";
chomp( my $num2 = <STDIN> );
say &dif($num1,$num2);
}
sub sum {
my $total;
my @numbers = @_;
foreach (@numbers) {
$total += $_;
}
return $total;
}
sub dif {
my $num1 = shift;
my $num2 = shift;
return $num1 - $num2;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment