Acceptible idiom ?

Discussion in 'Perl Misc' started by Koos Pol, Oct 11, 2004.

  1. Koos Pol

    Koos Pol Guest

    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
    Koos Pol, Oct 11, 2004
    #1
    1. Advertising

  2. Koos Pol

    Anno Siegel Guest

    Koos Pol <> 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
    Anno Siegel, Oct 11, 2004
    #2
    1. Advertising

  3. Koos Pol

    Koos Pol Guest

    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
    Koos Pol, Oct 11, 2004
    #3
  4. In article <newscache$tmye5i$yw$>, "Koos Pol"
    <> 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
    Richard Gration, Oct 11, 2004
    #4
  5. Koos Pol

    Anno Siegel Guest

    Richard Gration <> wrote in comp.lang.perl.misc:
    > In article <newscache$tmye5i$yw$>, "Koos Pol"
    > <> 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
    Anno Siegel, Oct 11, 2004
    #5
  6. Koos Pol

    Paul Lalli Guest

    "Koos Pol" <> wrote in message
    news:newscache$tmye5i$yw$...
    >
    > Although you need to actively think Perl,

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

    ^^^^^^

    I find this contrast amusing.

    Paul Lalli
    Paul Lalli, Oct 11, 2004
    #6
  7. Koos Pol

    Koos Pol Guest

    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
    Koos Pol, Oct 11, 2004
    #7
  8. Koos Pol

    Paul Lalli Guest

    "Anno Siegel" <-berlin.de> wrote in message
    news:ckdudi$of3$-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
    Paul Lalli, Oct 11, 2004
    #8
  9. Koos Pol

    Anno Siegel Guest

    Paul Lalli <> wrote in comp.lang.perl.misc:
    > "Anno Siegel" <-berlin.de> wrote in message
    > news:ckdudi$of3$-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
    Anno Siegel, Oct 11, 2004
    #9
  10. Koos Pol

    Koos Pol Guest

    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 :-D

    --
    KP
    43rd Law of Computing: "Anything that can go wr
    fortune: Segmentation violation -- Core dumped
    Koos Pol, Oct 11, 2004
    #10
  11. In article <ckdudi$of3$-Berlin.DE>, "Anno Siegel"
    <-berlin.de> wrote:


    > Richard Gration <> wrote in comp.lang.perl.misc:
    >> In article <newscache$tmye5i$yw$>, "Koos Pol"
    >> <> 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


    I see your point about flawed logic, however ... what's going on here.
    The two expressions appear to be identical, truthwise. Now I'm confused

    [rich@richg perl]$ cat truth.pl
    #!/usr/bin/perl

    truth('a',$c);
    $c = 0;
    truth('b',$c);
    $c = '';
    truth('c',$c);
    $c = 10;
    truth('d',$c);
    $c = 'asdf';
    truth('e',$c);

    sub truth {
    ($t,$n) = @_;

    print "$t 1" if (defined $n && ($n ne "" || $n != 0));
    print "\t";
    print "$t 2" if (defined $n && ($n ne ""));
    print "\n";
    }
    [rich@richg perl]$ ./truth.pl

    b 1 b 2

    d 1 d 2
    e 1 e 2
    [rich@richg perl]$
    Richard Gration, Oct 11, 2004
    #11
  12. "Paul Lalli" <> writes:

    > "Anno Siegel" <-berlin.de> wrote in message
    > news:ckdudi$of3$-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 problem is warnings. Arithmetically comparing a non-numeric
    string, including the empty string, to a numeric value, including 0,
    results in a warning under -w:

    perl -w -le'print (("" != 0) ? "notzero" : "iszero");'

    ----Scott.
    Scott W Gifford, Oct 11, 2004
    #12
  13. Koos Pol

    Anno Siegel Guest

    Richard Gration <> wrote in comp.lang.perl.misc:
    > In article <ckdudi$of3$-Berlin.DE>, "Anno Siegel"
    > <-berlin.de> wrote:
    >
    >
    > > Richard Gration <> wrote in comp.lang.perl.misc:
    > >> In article <newscache$tmye5i$yw$>, "Koos Pol"
    > >> <> 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

    >
    > I see your point about flawed logic, however ... what's going on here.
    > The two expressions appear to be identical, truthwise. Now I'm confused


    *I* was confused (as has been cleared up elsewhere in the thread).
    Normally Perl doesn't allow you to create a non-zero variable whose
    string value is ''.

    But despite appearances, the string value and the numeric value of a
    scalar are completely independent. $! is a variable that shows this
    behavior. dualvar() in Scalar::Util can create such variables ad lib.
    Another possibility is an object whose string value and numeric value
    could, again, be overloaded with anything.

    Anno
    Anno Siegel, Oct 11, 2004
    #13
  14. Koos Pol

    Joe Smith Guest

    Richard Gration wrote:

    > I see your point about flawed logic, however ... what's going on here.
    > The two expressions appear to be identical, truthwise. Now I'm confused
    >
    > [rich@richg perl]$ cat truth.pl
    > #!/usr/bin/perl
    >
    > truth('a',$c);
    > $c = 0;
    > truth('b',$c);
    > $c = '';
    > truth('c',$c);
    > $c = 10;
    > truth('d',$c);
    > $c = 'asdf';
    > truth('e',$c);
    >
    > sub truth {
    > ($t,$n) = @_;
    >
    > print "$t 1" if (defined $n && ($n ne "" || $n != 0));
    > print "\t";
    > print "$t 2" if (defined $n && ($n ne ""));
    > print "\n";
    > }


    That should be
    print "$t 1" if (defined $n && ($n ne "" && $n != 0));

    -Joe
    Joe Smith, Oct 11, 2004
    #14
  15. Koos Pol <> wrote in news:newscache$tmye5i$yw$1
    @news.emea.compuware.com:

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


    First and foremost, the above is a syntax error. But let's suppose you
    really meant "$a != 0" up there.

    If $a is 0, then !!$a is '', but defined $a && ($a ne "" || $a != 0) is 1.
    So !!$a may be shorter, but it is not equivalent.

    --
    Eric
    `$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
    $!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
    $_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
    ;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`
    Eric J. Roode, Oct 12, 2004
    #15
  16. Koos Pol

    Koos Pol Guest

    Randal L. Schwartz wrote (Monday 11 October 2004 16:25):

    > Koos: replacing (defined $a && ($a != "")) with (!!$a)


    > There is no promise that !! returns a value of "1" for true.
    > Caveat executor.


    That alright. Just interested in true/false and getting rid of
    that ubiquitous dreadful test.

    Koos
    --
    KP
    43rd Law of Computing: "Anything that can go wr
    fortune: Segmentation violation -- Core dumped
    Koos Pol, Oct 12, 2004
    #16
  17. Koos Pol

    Koos Pol Guest

    Abigail wrote (Monday 11 October 2004 23:06):

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


    > Yes, but what is your point?
    >
    > defined $a && ($a ne "" || $a <>0 );
    >
    > is a syntax error,


    Yes. I was typing faster then thinking. My bad.

    > and if you mean '!=' where you write '<>',
    > the expressions are not equivalent. '($a ne "" || $a != 0)' is
    > only false if $a is some overloaded object that is the empty
    > string in string context, and 0 is numeric context. So, assuming
    > that $a isn't such an object...


    Assume I'm maintainig a script which misuses a var to have both
    string and numeric values... :-(

    > But 'defined $a' is true for '$a == 0', but for '$a == 0',
    > '!!$a' is false.


    And that is exactly what I was after. !! makes is possible to
    check for true even with undefined variables.

    Koos
    --
    KP
    43rd Law of Computing: "Anything that can go wr
    fortune: Segmentation violation -- Core dumped
    Koos Pol, Oct 12, 2004
    #17
  18. Koos Pol

    Ala Qumsieh Guest

    Koos Pol wrote:

    > And that is exactly what I was after. !! makes is possible to
    > check for true even with undefined variables.


    Undefined variables can never be true. Your code simply reduces to:

    if ($a) { ... }

    but, I have a feeling you already knew that :)

    --Ala
    Ala Qumsieh, Oct 12, 2004
    #18
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rhino
    Replies:
    5
    Views:
    531
    Jon A. Cruz
    Feb 18, 2004
  2. Replies:
    6
    Views:
    334
  3. Steve Jorgensen
    Replies:
    4
    Views:
    421
    Soren Kuula
    Aug 28, 2005
  4. Ivan Vecerina

    Re: Extendable envelope/letter idiom

    Ivan Vecerina, Jun 24, 2003, in forum: C++
    Replies:
    0
    Views:
    1,113
    Ivan Vecerina
    Jun 24, 2003
  5. bala
    Replies:
    7
    Views:
    347
Loading...

Share This Page