Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (4)
    As of 2014-12-21 01:52 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (101 votes), past polls