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.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.