I modified your code a bit so you can see what's happening:
This ends up producing this for an output:use strict; my $R; my $i=1; sub mySub { printf "\n\n%d. R = \"%s\"\n",$i++,$R; unless ($R) { print "CONDITION WORKS\n"; $R = 'some string'; } } print mySub(); #prints CONDITION WORKS\nsome string print mySub(); #prints some string print mySub(); #again
On the first iteration $R is undefined and there for in a "false" state. Therefore your print inside the "unless" gets executed and an assignment gets performed on $R. Where it gets trickier is the next iterations. Since you have unless($R) as the last evalutation the value of $R becomes the return value for the sub since you didn't specify one. see sub and search for "return" if you want more on this.1. R = "" CONDITION WORKS some string 2. R = "some string" some string 3. R = "some string" some string
In reply to Re: Perl: last evaluated value as a returned value for a subroutine. See inside
by blue_cowdawg
in thread Perl: last evaluated value as a returned value for a subroutine. See inside
by Doctrin
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |