Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > what is the return value type of !1 ?

Reply
Thread Tools

what is the return value type of !1 ?

 
 
shul
Guest
Posts: n/a
 
      03-24-2009
Hi,

I am curious. If I take !1 and put it into an array or hash, what
lives in that position?

I know that !1 is false, which can be represented by () or 0.

Well if I do this

my @arp = 1 .. 4;

my $len= $#arp;
print "length is $len\n";

my $out1 = ! 1;
push(@arp, $out1);
print "$arp[4]\n";
my $len= $#arp;
print "length is $len\n";

well clearly the @arp array is longer is longer, but I dont get
anything printed when I try to print
print " the value is $arp[4]\n";

it just prints

"the value is "

So what is that object at position $arp[4]?

What operator will identify what lives at $arp[4]?

Note if I push a new thing on to @arp it goes to the next position $arp
[5].

shouldn't it be 0 or maybe it really its just () ?

This came up while I was reading a article which discussed different
ways of selecting unique elements of an array, which used a ! operator
together with ++ on a hash.
 
Reply With Quote
 
 
 
 
Tim McDaniel
Guest
Posts: n/a
 
      03-24-2009
In article
<8412cdc2-d3ec-4025-abe3->,
shul <> wrote:
>I am curious. If I take !1 and put it into an array or hash, what
>lives in that position?


The same thing as if you put it into a scalar ...

>I know that !1 is false, which can be represented by () or 0.


That can be stated more precisely. Luckily, "man perlsyn" does.
There are actually 5 things that are treated as false in a boolean
context, and !(something true) has a special value:

Truth and Falsehood

The number 0, the strings '0' and '', the empty list "()", and
"undef" are all false in a boolean context. All other values are
true. 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.

>but I dont get anything printed when I try to print
>print " the value is $arp[4]\n";
>
>it just prints
>
>"the value is "


Well, you do get something: the null string, but that's a rather
pedantic statement. In
print " the value is $arp[4]\n";
With $arp[4] in the double-quoted string, it's evaluated as a string,
so as "man perlsyn" says, "it is treated as ''".

Answers to variants you didn't ask about:

By default, using it in print on its own is in string context too, so

$ perl -e '$x = !1; print "This false is <", $x, ">\n"'
This false is <>

But if you force a numeric context:

$ perl -we '$x = !1; print "This false is <", $x+0, ">\n"'
This false is <0>

Note the difference between that and this:

$ perl -we '$x = ""; print "This false is <", $x+0, ">\n"'
Argument "" isn't numeric in add at -e line 1.
This false is <0>

That is, !1 really is a special false, as "man perlsyn" says:
"" gives a warning message that !1 doesn't.

--
Tim McDaniel,
 
Reply With Quote
 
 
 
 
shul
Guest
Posts: n/a
 
      03-25-2009
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.

That is what I needed! A special false value!

It is a 'special false value', that when evaluated is interpreted in
context as string or number.

Now I can analyze that algorithm to think about the context .

Thanks!

MItchell
 
Reply With Quote
 
Ilya Zakharevich
Guest
Posts: n/a
 
      03-25-2009
On 2009-03-25, shul <> 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.
>
> That is what I needed! A special false value!


BS.

Hope this helps,
Ilya
 
Reply With Quote
 
Ilya Zakharevich
Guest
Posts: n/a
 
      03-25-2009
On 2009-03-24, Tim McDaniel <> wrote:
> That can be stated more precisely. Luckily, "man perlsyn" does.


.... wrongly (as goes with the most of current Perl docs)...

> There are actually 5 things that are treated as false in a boolean
> context, and !(something true) has a special value:
>
> Truth and Falsehood
>
> The number 0, the strings '0' and '', the empty list "()", and
> "undef" are all false in a boolean context. All other values are
> true.


Wrong.

> 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).

Hope this helps,
Ilya
 
Reply With Quote
 
Xho Jingleheimerschmidt
Guest
Posts: n/a
 
      03-25-2009
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
 
Reply With Quote
 
Tim McDaniel
Guest
Posts: n/a
 
      03-25-2009
In article <slrngsjaq7.a7i.nospam->,
Ilya Zakharevich <nospam-> wrote:
>On 2009-03-24, Tim McDaniel <> wrote:
>> That can be stated more precisely. Luckily, "man perlsyn" does.

>
>... wrongly (as goes with the most of current Perl docs)...
>
>> There are actually 5 things that are treated as false in a boolean
>> context, and !(something true) has a special value:
>>
>> Truth and Falsehood
>>
>> The number 0, the strings '0' and '', the empty list "()", and
>> "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?

>> 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).


Well, as the examples showed, the value prints as a null string --
but, unlike "", it does not throw a warning under "-w" when used in
arithmetic. I don't know of any other value that has those
properties. Why do you write that it's not "special"?

--
Tim McDaniel,
 
Reply With Quote
 
Tad J McClellan
Guest
Posts: n/a
 
      03-25-2009
Tim McDaniel <> wrote:
> In article <slrngsjaq7.a7i.nospam->,
> Ilya Zakharevich <nospam-> wrote:
>>On 2009-03-24, Tim McDaniel <> wrote:
>>> That can be stated more precisely. Luckily, "man perlsyn" does.

>>
>>... wrongly (as goes with the most of current Perl docs)...
>>
>>> There are actually 5 things that are treated as false in a boolean

^^^^^^^^
>>> context, and !(something true) has a special value:
>>>
>>> Truth and Falsehood
>>>
>>> The number 0, the strings '0' and '', the empty list "()", and

^^^^^^^^^^^^^^^^^^^
>>> "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?



I dunno if this is what Ilya had in mind, but the empty list
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.

(parens are not a "list constructor". You very often see lists in
parens but that is for precedence reasons.
)


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
 
Reply With Quote
 
Tim McDaniel
Guest
Posts: n/a
 
      03-25-2009
In article <>,
Tad J McClellan <> 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,
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      03-25-2009
>>>>> "TJM" == Tad J McClellan <> 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 ------ -------- 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 ---------
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"raise (type, value, traceback)" and "raise type, value, traceback" Jack Bates Python 0 05-02-2011 05:23 PM
Why is return type in getfullspec().annotations named as "return"? andrew cooke Python 1 04-03-2011 01:02 AM
compiler error: argument of type "VALUE *" is incompatible with parameter of type "VALUE" me2faster@excite.com Ruby 1 05-05-2005 11:23 PM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM
Return by value -- primitive type vs class type DaKoadMunky C++ 8 05-14-2004 09:46 PM



Advertisments