Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Edit: Because I couldn't find the documentation I expected the return value of an if block to be undefined behaviour, but Haukex's answer makes much more sense. You can ignore the rest of this post (except maybe the bit about Deparse, which actually confirms Haukex's version).

You used an if in a do block. So I'll give you an answer for a for loop, maybe in a function ;-)
perlsub:
If the last statement is a loop control structure like a foreach or a while, the returned value is unspecified

perlsyn:
Perceptive Perl hackers may have noticed that a for loop has a return value, and that this value can be captured by wrapping the loop in a do block. The reward for this discovery is this cautionary advice: The return value of a for loop is unspecified and may change without notice. Do not rely on it.

I quote those because I couldn't even find the relevant documentation for other kind of blocks. As far as I can tell it's not just unspecified, it's undocumented. The reason is quite clearer though, some freedom is left for perl to try and optimize your statement. So if (0) {SOMETHING} might be removed entirely, or replaced by a no-op. The condition in if (1) might be removed because the block is always executed. Actually this is confirmed by deparsing the code (perl -MO=Deparse yourfile.pl):

BEGIN { $/ = "\n"; $\ = "\n"; } use strict; use warnings; my $uninitialized; print do { $uninitialized }; print do { () }; print do { do { () } }; print do { 0 }; print '----------'; print scalar do { $uninitialized }; print scalar do { () }; print scalar do { do { () } }; print scalar do { 0 };
You can see that the if (1) {} is turned into do {}, and the if (0) is turned into 0. But this is not reliable behaviour, that's just a side effect of perl trying to replace some statements by the best equivalent it could find.


In reply to Re: printing unitialized value of the 'do BLOCK' by Eily
in thread printing unitialized value of the 'do BLOCK' by rsFalse

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-18 22:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found