[SUMMARY] the 'sub return value' thread (was: do { EXPR for EXPR })
On Oct 21, 2005, at 19:03, Robin Houston wrote:
> The heat generated by the ongoing "sub return values" thread
> seems to be obscuring the fact, remarked by demerphq, that
> part of the confusion has been caused by an honest-to-goodness
> bug in perl.
I don't quite understand what do you mean there by "caused". Those
threads have nothing to do with that bug (though it might have
appeared on the way).
Here goes an executive summary:
THE PROBLEM
1. perlsub documents a sub with no return statement
returns "the last expression evaluated".
2. Take sub foo { 1 for 1 }.
3. The set of expressions in the body of foo is S = {1}.
4. foo() returns !1, which does not belong to S.
5. Therefore, perlsub is wrong.
THE DISCUSSION
Some messages discussed whether 3 holds, which is the case,
and other messages discussed how this should be fixed.
Basically, some people go for documenting values "returned
after" control structures, and some people go for leaving
that kind of usages unspecified. Some propose going first
for the latter as a quick fix, and work in the former.
THE SOLUTION SO FAR (modulus cosmetic changes)
The current perlsub.pod in the repository leaves
unspecified the case where a loop control structure
is the last statement. This is a patch by Rafael
that relaxes a bit the first patch by me.
-- fxn
PS: More counterexamples have been provided, in particular one that
does not involve a loop control structure, namely "sub foo { unless
(1) { 1 } }", hence perlsub.pod is still wrong.
0 fxn10/22/2005 8:48:07 AM
On Sat, Oct 22, 2005 at 10:48:07AM +0200, Xavier Noria wrote:
> On Oct 21, 2005, at 19:03, Robin Houston wrote:
>
> >The heat generated by the ongoing "sub return values" thread
> >seems to be obscuring the fact, remarked by demerphq, that
> >part of the confusion has been caused by an honest-to-goodness
> >bug in perl.
>
> I don't quite understand what do you mean there by "caused". Those
> threads have nothing to do with that bug (though it might have
> appeared on the way).
>
> Here goes an executive summary:
>
> THE PROBLEM
>
> 1. perlsub documents a sub with no return statement
> returns "the last expression evaluated".
>
> 2. Take sub foo { 1 for 1 }.
>
> 3. The set of expressions in the body of foo is S = {1}.
>
> 4. foo() returns !1, which does not belong to S.
>
> 5. Therefore, perlsub is wrong.
>
>
> THE DISCUSSION
>
> Some messages discussed whether 3 holds, which is the case,
> and other messages discussed how this should be fixed.
>
> Basically, some people go for documenting values "returned
> after" control structures, and some people go for leaving
> that kind of usages unspecified. Some propose going first
> for the latter as a quick fix, and work in the former.
>
>
> THE SOLUTION SO FAR (modulus cosmetic changes)
>
> The current perlsub.pod in the repository leaves
> unspecified the case where a loop control structure
> is the last statement. This is a patch by Rafael
> that relaxes a bit the first patch by me.
>
> -- fxn
>
> PS: More counterexamples have been provided, in particular one that
> does not involve a loop control structure, namely "sub foo { unless
> (1) { 1 } }", hence perlsub.pod is still wrong.
unless does usually return one of the expressions; unless (**** }
compiles like FOO or do { BAR }, not !FOO and do { BAR }.
It just doesn't work that way when it gets constant-folded away.
Retry your example with:
$ perl -wle 'sub foo { unless (shift) { 1 } } print foo(0); print foo(42)'
1
42
0 sthoenna10/23/2005 10:38:45 AM
On Oct 23, 2005, at 12:38, Yitzchak Scott-Thoennes wrote:
> unless does usually return one of the expressions; unless (**** }
> compiles like FOO or do { BAR }, not !FOO and do { BAR }.
>
> It just doesn't work that way when it gets constant-folded away.
> Retry your example with:
>
> $ perl -wle 'sub foo { unless (shift) { 1 } } print foo(0); print
> foo(42)'
> 1
> 42
Is that what explains that the problem is found here as well
% perl -wle 'sub foo { if (shift) { 1 for 1 } } print "uh" if foo
(1) != 1'
uh
? Because do EXPR has the same issue and is used beneath?
-- fxn
0 fxn10/23/2005 11:17:42 AM
On Oct 23, 2005, at 13:17, Xavier Noria wrote:
> ? Because do EXPR
Oh, I mean do BLOCK, I guess these threads have reinforced the word
"expression" to much in my brain :-).
-- fxn