#!/usr/bin/perl
# Functions in Perl are called subroutines. Like bash, you don't define the
# parameters in the function declaration. We'll see how to access the parameters
# when we look at the Fibonacci subroutine below.
sub main {
# Again, the ubiquitous printf function. Apparently a staple of programming
printf "\nHow many numbers of the sequence would you like?\n";
# Once again, we don't need to declare variables before using them.
# Perl's scalar variables, prefaced with $, can be either strings or numbers
# We use <STDIN> to get the data from stdin here
$n = 10;
# As in PHP, we need to remove the newline at the end
chop $n;
&fibonacci($n);
}
# Except for the first line declaring the subroutine, and the different way that
# parameters passed to the subroutine are passed, this is identical to the PHP version
sub fibonacci {
$a = 0;
$b = 1;
# So now we see the parameter being used here. For clarity, I have written the for
# loop using $n like the other examples. To set $n using the first parameter passed
# to the subroutine, I access the scalar variable $_[0], which is the first element
# of the parameter array @_
$n = $_[0];
for ($i=0;$i<$n;$i++){
$sum = $a + $b;
$a = $b;
$b = $sum;
}
}
&main;
IyEvdXNyL2Jpbi9wZXJsCgojIEZ1bmN0aW9ucyBpbiBQZXJsIGFyZSBjYWxsZWQgc3Vicm91dGluZXMuIExpa2UgYmFzaCwgeW91IGRvbid0IGRlZmluZSB0aGUgCiMgcGFyYW1ldGVycyBpbiB0aGUgZnVuY3Rpb24gZGVjbGFyYXRpb24uIFdlJ2xsIHNlZSBob3cgdG8gYWNjZXNzIHRoZSBwYXJhbWV0ZXJzCiMgd2hlbiB3ZSBsb29rIGF0IHRoZSBGaWJvbmFjY2kgc3Vicm91dGluZSBiZWxvdy4KCnN1YiBtYWluIHsKCiMgQWdhaW4sIHRoZSB1YmlxdWl0b3VzIHByaW50ZiBmdW5jdGlvbi4gQXBwYXJlbnRseSBhIHN0YXBsZSBvZiBwcm9ncmFtbWluZwpwcmludGYgIlxuSG93IG1hbnkgbnVtYmVycyBvZiB0aGUgc2VxdWVuY2Ugd291bGQgeW91IGxpa2U/XG4iOwoKIyBPbmNlIGFnYWluLCB3ZSBkb24ndCBuZWVkIHRvIGRlY2xhcmUgdmFyaWFibGVzIGJlZm9yZSB1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