Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Perl: last evaluated value as a returned value for a subroutine. See inside

by blue_cowdawg (Monsignor)
on Feb 21, 2013 at 17:15 UTC ( #1019986=note: print w/replies, xml ) Need Help??


in reply to Perl: last evaluated value as a returned value for a subroutine. See inside

I modified your code a bit so you can see what's happening:

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
This ends up producing this for an output:
1. R = "" CONDITION WORKS some string 2. R = "some string" some string 3. R = "some string" some string
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.


Peter L. Berghold -- Unix Professional
Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1019986]
help
Chatterbox?
[hippo]: The thesholds/benefits balance at the time was nuts.
[hippo]: But that doesn't last because, you know ... politicians. :(
[Corion]: hippo: Yeah, if you still get enough money to get by and don't have expensive hobbies or other fixed costs that can work out well
[ambrus]: ah good. I pay only rent too. but that still costs significant money.
[Corion]: ambrus: Yes, but that is already budgeted for
[holli]: Corion is well off. I am sure there is a script of his running in the frankfurt banking data center, diligently mining bitcoins ;-)
[Corion]: holli: Hahaha :-D

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (8)
As of 2017-09-21 15:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    During the recent solar eclipse, I:









    Results (249 votes). Check out past polls.

    Notices?