Re: strange return value



T Beck <Tracy.Beck@xxxxxxxxxxxx> wrote in comp.lang.perl.misc:
>
> T Beck wrote:
> > fxnoria@xxxxxxxxx wrote:
> > > See these tests, the returned value is defined and has kind of a dual
> > > nature:
> > >
> > > % perl -wle 'print 2 if 0 eq ""'
> > > % perl -wle 'print 2 if 0 == ""'
> > > Argument "" isn't numeric in numeric eq (==) at -e line 1.
> > > 2
> > > % perl -wle 'sub foo { 1 for @_ } print 2 if defined foo(3)'
> > > 2
> >
>
> Argh... I completely read over the part where you pointed out it was
> defined. Apparently I haven't had enough coffee today...
>
> > I'd steer clear of relying on it unless someone can find a place in the
> > docs that says this is the way it's supposed to behave, but it
> > certainly seems to be the way that it {does} behave (for the moment,
> > anyway)
>
> It still seems to be a constant behavior, but not very comforting.
> You're right, the fact that it's returning an actual empty string is
> weird, to say the least.

Not weird at all, but normal behavior for Perl's idea of a boolean. A
boolean false has a double nature in that its numeric value is 0 and its
string value is an empty string. (A boolean true is a normal 1). Another
scalar with independent numeric and string values is $!.

So this supports the theory that what the sub returns is the boolean
result of the last comparison, arguably the "last statement executed".
More evidence:

% perl -wle 'sub foo { 1 until $_[0] } print foo("quack")'
quack

However, I'm confident in saying that this is nowhere documented. In fact
I consider this a (harmless) bug, it should behave like a plain "return;".

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
.



Relevant Pages

  • Re: How optimized ar eif-statements in JS
    ... doThatThing; ... non-zero numbers, non-empty strings, and boolean true. ... be equal to the empty string primitive. ... includes at least one implicit type-conversion to boolean anyway). ...
    (comp.lang.javascript)
  • Re: if (-1) returns true
    ... > It usually caused extra work for the compilers, ... > boolean up to a wider type. ... Check Jensen & Wirths "Pascal Report and User Manual" ... the broken "Reply" link at the bottom of the article. ...
    (comp.lang.c)
  • Re: if (-1) returns true
    ... > It usually caused extra work for the compilers, ... > boolean up to a wider type. ... Check Jensen & Wirths "Pascal Report and User Manual" ... the broken "Reply" link at the bottom of the article. ...
    (alt.comp.lang.borland-delphi)
  • Re: lc() with undefined arg
    ... > undef: 'reverse' in scalar context. ... the value is no longer undef but the empty string. ... "Reply" at the bottom of the article headers. ...
    (comp.lang.perl.misc)
  • .bottom of TextWidthHeight
    ... Within the With block of "sRect" ... Variable in question: ".Bottom" ... properly calculate the .Bottom variable where the rowsource of a listbox is ... Have the code arbutarily put in a string value that is not an empty string ...
    (microsoft.public.access.formscoding)