But you're not returning anything from the function. Which means it will return the value of the last thing evaluated. And that is 'while' loop in your function (it counts as a whole thing). So what does it return? Let's use the standard library module Devel::Peek to peek inside Perl's internals...
use Devel::Peek; # exports 'Dump' function
sub fn {
my $i = 5;
while ($i < 10) {
$i += 1;
}
}
my $p = fn();
Dump $p;
And here's the output:
SV = PVNV(0x1007070) at 0x102ac18
REFCNT = 1
FLAGS = (PADMY,IOK,NOK,POK,pIOK,pNOK,pPOK)
IV = 0
NV = 0
PV = 0x1016880 ""\0
CUR = 0
LEN = 10
Pretty interesting! Apparently, the 'while' loop returns empty string (actually, number 0 and empty string at once). That is, it returns false. Let's try with some string now...
use Devel::Peek;
sub fn {
my $i = 5;
while ($i < 10) {
$i += 1;
}
'some string';
}
my $p = fn();
Dump $p;
SV = PV(0x1f25e20) at 0x1f47c28
REFCNT = 1
FLAGS = (PADMY,POK,IsCOW,pPOK)
PV = 0x1fa22f0 "some string"\0
CUR = 11
LEN = 13
COW_REFCNT = 1
Now it returns 'some string' (look at PV value).
So, basically, just use 'return' keyword if you want to return something from your function. Loops don't return anything particularly useful. |