Check Re^8: Unhappy returns (just updated) for some tests as to what gets returned when. Apparently:
- If the last thing that happened was an iteration of a for loop, you will get back !1, alias scalar false (it is 0 and an empty string at the same time).
- If the last thing that happened was an iteration of a while loop, you get back nothing at all.
- If the last thing that happened was an if block, you get back the value of the last expression evaluated – that would be either the condition, if it evaluated to false, or the last expression from the body of the conditional block.
In other words, what perlsub says is deceptive and incomplete.
Update: oh goodness, it gets weirder still:
- If the last thing that happened was an iteration of the do {} while $cond or do {} until $cond pseudo-loop, you get back a code reference (whether the condition turned out true of false).
It’s probably safe to say that Perl simply has no rule here, and that what you get back from a sub depends on implementation details of perl.
Makeshifts last the longest.
-
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.
|