in reply to Re^2: Perl function calls.
in thread Perl function calls.
And here's the output:use Devel::Peek; # exports 'Dump' function sub fn { my $i = 5; while ($i < 10) { $i += 1; } } my $p = fn(); Dump $p;
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...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
use Devel::Peek; sub fn { my $i = 5; while ($i < 10) { $i += 1; } 'some string'; } my $p = fn(); Dump $p;
Now it returns 'some string' (look at PV value).SV = PV(0x1f25e20) at 0x1f47c28 REFCNT = 1 FLAGS = (PADMY,POK,IsCOW,pPOK) PV = 0x1fa22f0 "some string"\0 CUR = 11 LEN = 13 COW_REFCNT = 1
So, basically, just use 'return' keyword if you want to return something from your function. Loops don't return anything particularly useful.
|
|---|