Re: strange return value
- From: anno4000@xxxxxxxxxxxxxxxxxxxxxxx (Anno Siegel)
- Date: 20 Oct 2005 20:14:45 GMT
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.
.
- Follow-Ups:
- Re: strange return value
- From: T Beck
- Re: strange return value
- From: Big and Blue
- Re: strange return value
- References:
- strange return value
- From: fxnoria
- Re: strange return value
- From: fxnoria
- Re: strange return value
- From: T Beck
- strange return value
- Prev by Date: Re: Can (nearly) any Perl program be written on a single line?
- Next by Date: Re: Perl Script Question - Working with Directories
- Previous by thread: Re: strange return value
- Next by thread: Re: strange return value
- Index(es):
Relevant Pages
|