Shlomi Fish wrote:
Hi hw!

Please see
http://www.shlomifish.org/philosophy/computers/netiquette/email/reply-to-list.html
.

On Sat, 1 Jul 2017 19:15:22 +0200 hw <h...@gc-24.de> wrote:

Shlomi Fish wrote:
Hi Shawn!

On Sat, 1 Jul 2017 11:32:30 -0400
Shawn H Corey <shawnhco...@gmail.com> wrote:

On Sat, 1 Jul 2017 17:27:02 +0200
hw <h...@gc-24.de> wrote:


Hi,

can someone please explain this:


perl -e 'my $i = 0; $i = defined($i) ? (!!$i) : 0; print "i: $i\n";'
i:


Particularly:


+ Why doesn´t it print 1?

Because !!$i is zero


+ How is this not a bug?

Nope, no bug.


+ What is being printed here?

!!$i which is !(!(0)) which is !(1) which is 0


I suspect !1 returns an empty string in scalar context.

What would be the reasoning for a numerical operator turning
numerical values into (empty) strings within a numerical context?


"!" is not a numerical operator - it is a *logical* operator. If one passes a
true value to it one gets a false value.

False and true are genuinely numeric.  You can´t say for a string
whether it is true or false; it is a string.

Else, one gets a true value. See
http://perldoc.perl.org/perlop.html . If used in a numerical context, false
values are treated as zeroes, but not all of them are zeroes in other contexts.

There is no other context involved here than a numerical one.  There
is also no reason to implicitly change the context to any other one,
like a string context.


perl -e 'my $i = 0; $i = defined($i) ? (!!$i) : 0; print $i;'
perl -e 'my $i = 0; $i = defined($i) ? (!!$i) : 0; printf("%d", $i);'
perl -e 'my $i = undef; $i = defined($i) ? (!!$i) : 0; print $i;'


These give you different results, and that is just wrong.  I did
assign a /number/ to $i and never a string.



Regards,

        Shlomi






+ How do you do what I intended in perl?


How do we know what you intend?










--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to