fxn has asked for the wisdom of the Perl Monks concerning the following question:
Dear Monks,
Having nothing better to do, I wondered whether that "last expression" that is returned by non-empty subroutines according to perlsub is well-defined. So I tested my understanding of that corner of the docs with these one-liners:
% perl -wle 'sub foo { 1 for @_ } print 2 if defined foo()' % perl -wle 'sub foo { 1 for @_ } print 2 if defined foo(3)' 2 % perl -wle 'sub foo { 1 for @_ } print 2 if foo(3) == 0' 2 % perl -wle 'sub foo { 1 for @_ } print 2 if foo(3) eq ""' 2
and I am puzzled. I guess the difference between the first and the rest have to do with the underlying implementation, though I don't actually know it. From a conceptual point of view I wouldn't have been able to predict that behaviour.
Then, IF the same value is returned always in the rest of the cases, I don't understand how can it be at the same time defined, == 0, and eq "" (edited: and not issue the warning Argument "" isn't numeric in numeric eq (==) ...).
So, is "the last expression" well-defined albeit not always apparent? Are those one-liners just showing unespecified behaviour (which would be a valid answer to my original question)?
-- fxn
PS1: To make things more interesting someone in MagNet#perl pointed out that if you change the 1 with a 43 something different a warning is issued:
% perl -wle 'sub foo { 43 for @_ } print 2 if defined foo()' Useless use of a constant in void context at -e line 1.
PS2: I have posted this question in a few places before, but got no definitive answer yet.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: "last expression" quiz
by sauoq (Abbot) on Oct 19, 2005 at 22:16 UTC | |
by fxn (Sexton) on Oct 19, 2005 at 22:21 UTC | |
by bart (Canon) on Oct 19, 2005 at 22:27 UTC | |
by Aristotle (Chancellor) on Oct 19, 2005 at 22:32 UTC | |
by sauoq (Abbot) on Oct 19, 2005 at 22:36 UTC | |
| |
by Aristotle (Chancellor) on Oct 19, 2005 at 22:58 UTC | |
by sauoq (Abbot) on Oct 19, 2005 at 22:44 UTC | |
by bart (Canon) on Oct 19, 2005 at 22:48 UTC | |
by fxn (Sexton) on Oct 20, 2005 at 09:27 UTC | |
by sauoq (Abbot) on Oct 20, 2005 at 09:38 UTC | |
by halley (Prior) on Oct 20, 2005 at 14:08 UTC | |
Re: "last expression" quiz
by Aristotle (Chancellor) on Oct 19, 2005 at 22:38 UTC | |
by Anonymous Monk on Oct 19, 2005 at 23:38 UTC | |
by Aristotle (Chancellor) on Oct 20, 2005 at 02:34 UTC | |
by Anonymous Monk on Oct 20, 2005 at 14:24 UTC | |
by sauoq (Abbot) on Oct 20, 2005 at 04:44 UTC | |
by Anonymous Monk on Oct 20, 2005 at 14:33 UTC | |
by sauoq (Abbot) on Oct 20, 2005 at 16:07 UTC | |
| |
by robin (Chaplain) on Oct 21, 2005 at 10:51 UTC | |
by Aristotle (Chancellor) on Oct 23, 2005 at 14:43 UTC | |
by robin (Chaplain) on Oct 23, 2005 at 15:14 UTC | |
by Aristotle (Chancellor) on Oct 27, 2005 at 05:37 UTC | |
Re: "last expression" quiz
by Skeeve (Parson) on Oct 19, 2005 at 22:19 UTC | |
Re: "last expression" quiz
by QM (Parson) on Oct 21, 2005 at 14:03 UTC |