Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Acceptible idiom ?

Reply
Thread Tools

Acceptible idiom ?

 
 
Koos Pol
Guest
Posts: n/a
 
      10-11-2004

Although you need to actively think Perl,

!!$a

is a lot shorter than

defined $a && ($a ne "" || $a <>0 );

Comments?
--
KP
43rd Law of Computing: "Anything that can go wr
fortune: Segmentation violation -- Core dumped
 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      10-11-2004
Koos Pol <(E-Mail Removed)> wrote in comp.lang.perl.misc:
>
> Although you need to actively think Perl,
>
> !!$a
>
> is a lot shorter than
>
> defined $a && ($a ne "" || $a <>0 );
>
> Comments?


That's a quite common idiom for normalizing booleans, not only in Perl.
It is useful in all languages that take a similar approach to booleans:
Everything is true except for a (small) number of exceptions that are
false.

I have called the construct "Das doppelte Notchen", but it takes a
native German speaker to estimate the full inanity of that pun.

Anno
 
Reply With Quote
 
 
 
 
Koos Pol
Guest
Posts: n/a
 
      10-11-2004
Anno Siegel wrote (Monday 11 October 2004 11:44):

>> !!$a
>>
>> is a lot shorter than
>>
>> defined $a && ($a ne "" || $a <>0 );


> That's a quite common idiom for normalizing booleans, not only
> in Perl.


Stupid things is, I've been doing the lower for years without
realizing the upper is a lot cleaner. And just this very moment
it suddenly dawned...

--
KP - Never too old to learn!
43rd Law of Computing: "Anything that can go wr
fortune: Segmentation violation -- Core dumped
 
Reply With Quote
 
Richard Gration
Guest
Posts: n/a
 
      10-11-2004
In article <newscache$tmye5i$yw$(E-Mail Removed)>, "Koos Pol"
<(E-Mail Removed)> wrote:


> Although you need to actively think Perl,
> !!$a
> is a lot shorter than
>
> defined $a && ($a ne "" || $a <>0 );
> Comments?


It seems to me that

($a <> 0) implies ($a ne "") for all $a

so that means the test could have been written

defined $a && ($a ne "")

Am I missing something?

Rich
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      10-11-2004
Richard Gration <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> In article <newscache$tmye5i$yw$(E-Mail Removed)>, "Koos Pol"
> <(E-Mail Removed)> wrote:
>
>
> > Although you need to actively think Perl,
> > !!$a
> > is a lot shorter than
> >
> > defined $a && ($a ne "" || $a <>0 );
> > Comments?

>
> It seems to me that
>
> ($a <> 0) implies ($a ne "") for all $a

^^
That's not Perl (nor was it in earlier postings)

> so that means the test could have been written
>
> defined $a && ($a ne "")
>
> Am I missing something?


The possibility that someone said "$a = 0"?

But anyway, your logic escapes me. Even if it were true that
'$a != 0 implies $a ne ""' (which it isn't), it would be the
test for "" that could be discarded, not the one for 0.

Anno
 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      10-11-2004
"Koos Pol" <(E-Mail Removed)> wrote in message
news:newscache$tmye5i$yw$(E-Mail Removed) ...
>
> Although you need to actively think Perl,

^^^^^^^^^^
>
> defined $a && ($a ne "" || $a <>0 );

^^^^^^

I find this contrast amusing.

Paul Lalli


 
Reply With Quote
 
Koos Pol
Guest
Posts: n/a
 
      10-11-2004
Richard Gration wrote (Monday 11 October 2004 13:54):

> It seems to me that
>
> ($a <> 0) implies ($a ne "") for all $a
>
> so that means the test could have been written
>
> defined $a && ($a ne "")
>
> Am I missing something?



Yes, you do (not your fault). I'm maintaing a script which misuses
a var to have both numeric and string values


--
KP
43rd Law of Computing: "Anything that can go wr
fortune: Segmentation violation -- Core dumped
 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      10-11-2004
"Anno Siegel" <(E-Mail Removed)-berlin.de> wrote in message
news:ckdudi$of3$(E-Mail Removed)-Berlin.DE...

> But anyway, your logic escapes me. Even if it were true that
> '$a != 0 implies $a ne ""' (which it isn't),


It isn't? Can you give me a non-zero value which is string-equal to the
empty string?

The converse, $a ne '' implying $a != 0, is clearly false, as is
exemplified by the value $a = 'foo'; The original does indeed hold,
however. There are no values which are not numerically equal to 0 which
are string equal to the empty string.

.... unless I'm really not thinking well today. Feel free to show me how
I'm wrong...

Paul Lalli


 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      10-11-2004
Paul Lalli <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> "Anno Siegel" <(E-Mail Removed)-berlin.de> wrote in message
> news:ckdudi$of3$(E-Mail Removed)-Berlin.DE...
>
> > But anyway, your logic escapes me. Even if it were true that
> > '$a != 0 implies $a ne ""' (which it isn't),

>
> It isn't? Can you give me a non-zero value which is string-equal to the
> empty string?


use Scalar::Util qw( dualvar);
my $paradox = dualvar( 123, '');
print $paradox + 0, " is also '$paradox'\n"

But I'll admit that I wasn't thinking of this possibility, I was just
confused. For "normal" Perl variables your implication is true. That
includes "natural duals" like booleans and (on my system) also $!.

It is another consideration that you must switch off warnings to
make undisturbed use of the implication.

Anno
 
Reply With Quote
 
Koos Pol
Guest
Posts: n/a
 
      10-11-2004
Paul Lalli wrote (Monday 11 October 2004 14:33):

>> Although you need to actively think Perl,

> ^^^^^^^^^^
>>
>> defined $a && ($a ne "" || $a <>0 );

> ^^^^^^
>
> I find this contrast amusing.



Yes, well, sometimes I do have my head screwed the other way
around

--
KP
43rd Law of Computing: "Anything that can go wr
fortune: Segmentation violation -- Core dumped
 
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
Would it be acceptible Abo UK VOIP 43 08-20-2007 06:48 PM
Re: Would it be acceptible Nomen Nescio UK VOIP 2 08-11-2007 12:25 AM
Re: Would it be acceptible George Orwell UK VOIP 1 08-10-2007 11:28 AM
is this behaviour acceptible? bala C++ 7 05-24-2005 10:49 AM
Idiom Question - Finding Instances Rhino Java 5 02-18-2004 05:36 AM



Advertisments