Skip to content

Instantly share code, notes, and snippets.

@AndrewRussellHayes
Last active December 20, 2015 19:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndrewRussellHayes/6181807 to your computer and use it in GitHub Desktop.
Save AndrewRussellHayes/6181807 to your computer and use it in GitHub Desktop.
Wrote this when I was learning perl. Putting it here jus to play with git.
#!/usr/bin/perl
#recAdd.pl By: Andrew Hayes <http://AndrewHay.es/>
#Copyright (c) 2012 Andrew Hayes
#
# This perl script recursively adds a given number to every number between it and zero.
#
use strict;
use warnings;
use Scalar::Util qw(looks_like_number);
no warnings 'recursion';
main(@ARGV);
sub main
{
if (looks_like_number(my $number=shift || getNum())){
print'The answer is ';
message(my $test = addall($number));
}else{
message("Sorry, please re-run and enter a numeric value.");
}
}
sub getNum
{
print 'What number would you like to recoursively add?' . "\n";
return <>;
}
sub addall
{
my $num = shift or return;
if ($num == 1) {
return $num;
} else {
return $num + addall($num-1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment