Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[SUMMARY] the "sub return value" thread (was: do { EXPR for EXPR })

13 views
Skip to first unread message

Xavier Noria

unread,
Oct 22, 2005, 4:48:07 AM10/22/05
to Perl5 Porters
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.

Yitzchak Scott-Thoennes

unread,
Oct 23, 2005, 6:38:45 AM10/23/05
to Xavier Noria, deme...@gmail.com, Perl5 Porters


unless does usually return one of the expressions; unless (FOO) { BAR }
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

Xavier Noria

unread,
Oct 23, 2005, 7:17:42 AM10/23/05
to Perl5 Porters
On Oct 23, 2005, at 12:38, Yitzchak Scott-Thoennes wrote:

> unless does usually return one of the expressions; unless (FOO)
> { BAR }
> 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

Xavier Noria

unread,
Oct 23, 2005, 7:30:26 AM10/23/05
to Perl5 Porters
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

0 new messages