[31053] in Perl-Users-Digest
Perl-Users Digest, Issue: 2298 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Mar 25 21:14:23 2009
Date: Wed, 25 Mar 2009 18:14:15 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Wed, 25 Mar 2009 Volume: 11 Number: 2298
Today's topics:
Re: what is the return value type of !1 ? (Tim McDaniel)
Re: what is the return value type of !1 ? <ben@morrow.me.uk>
Re: what is the return value type of !1 ? <uri@stemsystems.com>
Re: what is the return value type of !1 ? <tadmc@seesig.invalid>
Re: what is the return value type of !1 ? <peter@makholm.net>
Re: what is the return value type of !1 ? <uri@stemsystems.com>
Re: what is the return value type of !1 ? <ben@morrow.me.uk>
Re: what is the return value type of !1 ? <xhoster@gmail.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Wed, 25 Mar 2009 18:16:38 +0000 (UTC)
From: tmcd@panix.com (Tim McDaniel)
Subject: Re: what is the return value type of !1 ?
Message-Id: <gqdse6$k1k$1@reader1.panix.com>
In article <slrngskomo.3mr.tadmc@tadmc30.sbcglobal.net>,
Tad J McClellan <tadmc@seesig.invalid> wrote:
>I dunno if this is what Ilya had in mind, but the empty list
[glossed in snipped text as "()" from "man perlsyn"]
>is most certainly not a false value.
>
>Boolean context is a (special kind of) scalar context
>
> unless ( warn wantarray ? "list context\n" : "scalar context\n" ) {
> print "false value\n";
> }
>
> (prints only "scalar context")
>
>and
>
> there's no such thing as a list in scalar context (perlfaq4.pod)
>
>eg.
>
> unless ( () ) {
> print "false value\n";
> }
>
>There is no empty list there, merely some parenthesis.
If it's not a list, what is it? It's not a reference typeglob
filehandle &c &c ... how can it be a scalar if there's no value in
there? What's its value? A few tests suggests it evaluates to
an undefined value, but is that stated somewhere?
>(parens are not a "list constructor". You very often see lists in
> parens but that is for precedence reasons.
>)
--
Tim McDaniel, tmcd@panix.com
------------------------------
Date: Wed, 25 Mar 2009 18:18:33 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: what is the return value type of !1 ?
Message-Id: <p49q96-r9a.ln1@osiris.mauzo.dyndns.org>
Quoth Tad J McClellan <tadmc@seesig.invalid>:
>
> Boolean context is a (special kind of) scalar context
This is correct.
> unless ( warn wantarray ? "list context\n" : "scalar context\n" ) {
> print "false value\n";
> }
>
> (prints only "scalar context")
This code doesn't demonstrate what you think it does. wantarray tells
you what context the *currently executing sub* was called in, so you
need something like
sub ctx {
warn defined wantarray ? wantarray ? "LIST" : "SCALAR" : "VOID";
}
unless (ctx) {
warn "false value";
}
> and
>
> there's no such thing as a list in scalar context (perlfaq4.pod)
I've pointed out before that I don't think this way of looking at things
is helpful. I would say 'evaluating a list in scalar context evaluates
every element but the last in void context, then evaluates the last in
scalar context and returns that'.
> eg.
>
> unless ( () ) {
> print "false value\n";
> }
>
> There is no empty list there, merely some parenthesis.
There is *something* there. If the parens were only there for
precedence, they could safely be omitted, which they can't:
~% perl -e'unless ( () ) { 1 }'
~% perl -e'unless ( ) { 1 }'
syntax error at -e line 1, near "( ) "
Execution of -e aborted due to compilation errors.
~%
> (parens are not a "list constructor". You very often see lists in
> parens but that is for precedence reasons.
> )
While this is generally true, () is special (under some circumstances:
the parsing of parens in Perl is very complicated and context-
dependant). It represents the empty list, which evaluates to undef in
scalar context:
~% perl -MDevel::Peek -e'Dump scalar(())'
SV = NULL(0x0) at 0x281863d8
REFCNT = 2147483627
FLAGS = (READONLY)
~% perl -MDevel::Peek -e'Dump undef'
SV = NULL(0x0) at 0x281863d8
REFCNT = 2147483627
FLAGS = (READONLY)
~%
Ben
------------------------------
Date: Wed, 25 Mar 2009 13:52:42 -0500
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: what is the return value type of !1 ?
Message-Id: <x7wsadcu4l.fsf@mail.sysarch.com>
>>>>> "TJM" == Tad J McClellan <tadmc@seesig.invalid> writes:
>>>> The number 0, the strings '0' and '', the empty list "()", and
TJM> ^^^^^^^^^^^^^^^^^^^
>>>> "undef" are all false in a boolean context. All other values
>>>> are true.
>>>
>>> Wrong.
>>
>> I would like to learn more about Perl, so would you please explain
>> what is correct?
TJM> I dunno if this is what Ilya had in mind, but the empty list
TJM> is most certainly not a false value.
it kind of is in a special way. look at this known idiom (and this is
documented somewhere)
if ( my( $foo, $bar ) = get_foobar() ) {
if the sub returns an empty list (and it will be called in list
context), then the assigned list will become false. this isn't really a
list in scalar context but does work and i use it.
perldata covers this:
List assignment in scalar context returns the number of elements
produced by the expression on the right side of the assignment:
$x = (($foo,$bar) = (3,2,1)); # set $x to 3, not 2
$x = (($foo,$bar) = f()); # set $x to f()'s return count
This is handy when you want to do a list assignment in a Boolean
context, because most list functions return a null list when finished,
which when assigned produces a 0, which is interpreted as FALSE.
so the empty list itself isn't a false value but it coerces to one when
used as in the above cases and will become false.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
------------------------------
Date: Wed, 25 Mar 2009 16:04:02 -0500
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: what is the return value type of !1 ?
Message-Id: <slrngsl722.64e.tadmc@tadmc30.sbcglobal.net>
Ben Morrow <ben@morrow.me.uk> wrote:
>
> Quoth Tad J McClellan <tadmc@seesig.invalid>:
>>
>> Boolean context is a (special kind of) scalar context
>
> This is correct.
>
>> unless ( warn wantarray ? "list context\n" : "scalar context\n" ) {
>> print "false value\n";
>> }
>>
>> (prints only "scalar context")
>
> This code doesn't demonstrate what you think it does. wantarray tells
> you what context the *currently executing sub* was called in, so you
> need something like
>
> sub ctx {
> warn defined wantarray ? wantarray ? "LIST" : "SCALAR" : "VOID";
> }
>
> unless (ctx) {
> warn "false value";
> }
I had it written that way for testing, then replaced the sub
with wantarray directly.
I should have left it the way it was. :-)
>> and
>>
>> there's no such thing as a list in scalar context (perlfaq4.pod)
>
> I've pointed out before that I don't think this way of looking at things
> is helpful.
It is if you read the sentence that follows it in the FAQ.
(and if you know what the "comma operator" is.)
> I would say 'evaluating a list in scalar context evaluates
> every element but the last in void context, then evaluates the last in
> scalar context and returns that'.
I would say 'when evaluating what looks like a list in scalar context
you are really evaluating the comma operator'
and then maybe add your description of the comma operator's behavior.
>> eg.
>>
>> unless ( () ) {
>> print "false value\n";
>> }
>>
>> There is no empty list there, merely some parenthesis.
>
> There is *something* there. If the parens were only there for
> precedence, they could safely be omitted, which they can't:
>
> ~% perl -e'unless ( () ) { 1 }'
> ~% perl -e'unless ( ) { 1 }'
> syntax error at -e line 1, near "( ) "
> Execution of -e aborted due to compilation errors.
> ~%
Yeah, I tried that before posting too, but it just confused me...
>> (parens are not a "list constructor". You very often see lists in
>> parens but that is for precedence reasons.
>> )
>
> While this is generally true, () is special (under some circumstances:
> the parsing of parens in Perl is very complicated and context-
> dependant). It represents the empty list, which evaluates to undef in
> scalar context:
>
> ~% perl -MDevel::Peek -e'Dump scalar(())'
> SV = NULL(0x0) at 0x281863d8
> REFCNT = 2147483627
> FLAGS = (READONLY)
> ~% perl -MDevel::Peek -e'Dump undef'
> SV = NULL(0x0) at 0x281863d8
> REFCNT = 2147483627
> FLAGS = (READONLY)
> ~%
... and now I'm less confused.
Thanks!
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
------------------------------
Date: Wed, 25 Mar 2009 22:17:03 +0100
From: Peter Makholm <peter@makholm.net>
Subject: Re: what is the return value type of !1 ?
Message-Id: <87r60lxpyo.fsf@vps1.hacking.dk>
Ben Morrow <ben@morrow.me.uk> writes:
> Well, it's a dualvar, which is reasonably special. It is possible to
> create an equivalent dualvar with
>
> use Scalar::Util;
>
> my $false = dualvar 0, "";
This is not quite the same value as I get with !1. The latter has bot
the IOK and NOK flags set but with dualvar I can only get one of these
flags set together with POK.
You can see it with Devel::Peek::Dump() but I don't think I can come
up with a piece of perl code taht shows any oberserval difference.
XS-code could detect it though.
//Makholm
------------------------------
Date: Wed, 25 Mar 2009 16:33:42 -0500
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: what is the return value type of !1 ?
Message-Id: <x7wsadb83t.fsf@mail.sysarch.com>
>>>>> "PM" == Peter Makholm <peter@makholm.net> writes:
PM> Ben Morrow <ben@morrow.me.uk> writes:
>> Well, it's a dualvar, which is reasonably special. It is possible to
>> create an equivalent dualvar with
>>
>> use Scalar::Util;
>>
>> my $false = dualvar 0, "";
PM> This is not quite the same value as I get with !1. The latter has bot
PM> the IOK and NOK flags set but with dualvar I can only get one of these
PM> flags set together with POK.
PM> You can see it with Devel::Peek::Dump() but I don't think I can come
PM> up with a piece of perl code taht shows any oberserval difference.
PM> XS-code could detect it though.
you seem to be wrong here. both have a string value set and POK as well.
perl -MDevel::Peek -MScalar::Util=dualvar -le'print Dump !1; print Dump dualvar 0, "" ; $x = "" ;$x+0; print Dump $x'
SV = PVNV(0x8161018) at 0x815e290
REFCNT = 2147483647
FLAGS = (IOK,NOK,POK,READONLY,pIOK,pNOK,pPOK)
IV = 0
NV = 0
PV = 0x8161008 ""\0
CUR = 0
LEN = 4
SV = PVNV(0x8161a38) at 0x8163248
REFCNT = 1
FLAGS = (TEMP,IOK,POK,pIOK,pPOK)
IV = 0
NV = 0
PV = 0x816f810 ""\0
CUR = 0
LEN = 4
SV = PVNV(0x8161a38) at 0x81771f8
REFCNT = 1
FLAGS = (IOK,NOK,POK,pIOK,pNOK,pPOK)
IV = 0
NV = 0
PV = 0x816f810 ""\0
CUR = 0
LEN = 4
so both are '' and 0 at the same time. this is no different than caching
a converted value from string to/from a number. look at the third dump
which is a cached coercion. it is easy to make, just clunkier code
and this shows in detail how it caches the other possible value:
perl -MDevel::Peek -le'; $x = "" ;print Dump $x; $x+0; print Dump $x'
SV = PV(0x8160048) at 0x8176d64
REFCNT = 1
FLAGS = (POK,pPOK)
PV = 0x8173208 ""\0
CUR = 0
LEN = 4
SV = PVNV(0x8161450) at 0x8176d64
REFCNT = 1
FLAGS = (IOK,NOK,POK,pIOK,pNOK,pPOK)
IV = 0
NV = 0
PV = 0x8173208 ""\0
CUR = 0
LEN = 4
note how the second dump adds IOK and NOK but the string stuff stays the
same.
so !0 and other dual false values aren't very special at all. you can
have dual values whenever you coerce a variable. it just happens that
perl's builtin false's will be in that dualform to begin with. and the
dualval function can even make the two values unrelated to each
other. !1 is just another case where perl will cache both data formats.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
------------------------------
Date: Wed, 25 Mar 2009 23:32:08 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: what is the return value type of !1 ?
Message-Id: <ogrq96-vod.ln1@osiris.mauzo.dyndns.org>
Quoth Tad J McClellan <tadmc@seesig.invalid>:
> Ben Morrow <ben@morrow.me.uk> wrote:
> >
> > Quoth Tad J McClellan <tadmc@seesig.invalid>:
> >>
> >> there's no such thing as a list in scalar context (perlfaq4.pod)
> >
> > I've pointed out before that I don't think this way of looking at things
> > is helpful.
>
> It is if you read the sentence that follows it in the FAQ.
> (and if you know what the "comma operator" is.)
I have, and I do...
> > I would say 'evaluating a list in scalar context evaluates
> > every element but the last in void context, then evaluates the last in
> > scalar context and returns that'.
>
> I would say 'when evaluating what looks like a list in scalar context
> you are really evaluating the comma operator'
>
> and then maybe add your description of the comma operator's behavior.
I knew I should have left that paragraph out :). We've had this
discussion before, and there's really no point going over it again. You
(and the faq) know better than I what beginning Perl users find helpful;
I know (and care) that the comma operator in fact always creates an
OP_LIST, regardless of context. We don't end up disagreeing about
anything material, so I'm sorry I brought it up again.
> ... and now I'm less confused.
I'm glad :).
Ben
------------------------------
Date: Tue, 24 Mar 2009 23:43:05 -0700
From: Xho Jingleheimerschmidt <xhoster@gmail.com>
Subject: Re: what is the return value type of !1 ?
Message-Id: <49cac3e3$0$25378$ed362ca5@nr5-q3a.newsreader.com>
Ilya Zakharevich wrote:
>
>> Negation of a true value by "!" or "not" returns a special
>> false value. When evaluated as a string it is treated as '', but
>> as a number, it is treated as 0.
>
> There is nothing especially "special" about the returned value (unless
> one noticed that it is the same value for all statements returning FALSE).
It seems somewhat special to me. It is defined, and has a length of
zero, but unlike the empty string it does not trigger an 'Argument ""
isn't numeric' warning message when used as a number.
How else would you describe a value that has these three properties?
Xho
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc. For subscription or unsubscription requests, send
#the single line:
#
# subscribe perl-users
#or:
# unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V11 Issue 2298
***************************************