Last active
December 20, 2015 19:18
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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