perl flawed or my fault

Discussion in 'Perl Misc' started by paul, Oct 3, 2006.

  1. paul

    paul Guest

    Hello all,

    please help me the check the following code

    use strict;

    my $something = 'phal';
    my $otherthing = 'paul';

    my $note_whom = 'szmuzu\'mi';

    defined($something) ? $note_whom = $something : $otherthing =
    'singapore';

    print "$note_whom \n $otherthing \n";

    The result is: singapore
    singapore

    I don't know why the scalar $note_whom get the value that is assigned
    to $otherthing.

    Please give me the explaination, and thank you.
    paul, Oct 3, 2006
    #1
    1. Advertising

  2. paul

    DJ Stunks Guest

    paul wrote:
    > Hello all,
    >
    > please help me the check the following code
    >
    > use strict;
    >
    > my $something = 'phal';
    > my $otherthing = 'paul';
    >
    > my $note_whom = 'szmuzu\'mi';
    >
    > defined($something) ? $note_whom = $something : $otherthing =
    > 'singapore';
    >
    > print "$note_whom \n $otherthing \n";
    >
    > The result is: singapore
    > singapore
    >
    > I don't know why the scalar $note_whom get the value that is assigned
    > to $otherthing.
    >
    > Please give me the explaination, and thank you.


    note precendence...

    C:\tmp>perl -MO=Deparse,-p tmp.pl
    use warnings;
    use strict 'refs';
    (my $something = 'phal');
    (my $otherthing = 'paul');
    (my $note_whom = q[szmuzu'mi]);
    ((defined($something) ? ($note_whom = $something) : $otherthing) =
    'singapore');
    print("$note_whom \n $otherthing \n");

    HTH,
    -jp
    DJ Stunks, Oct 3, 2006
    #2
    1. Advertising

  3. paul wrote:
    >
    > please help me the check the following code
    >
    > use strict;
    >
    > my $something = 'phal';
    > my $otherthing = 'paul';
    >
    > my $note_whom = 'szmuzu\'mi';
    >
    > defined($something) ? $note_whom = $something : $otherthing =
    > 'singapore';
    >
    > print "$note_whom \n $otherthing \n";
    >
    > The result is: singapore
    > singapore
    >
    > I don't know why the scalar $note_whom get the value that is assigned
    > to $otherthing.
    >
    > Please give me the explaination, and thank you.


    Because of precedence you have:

    (defined($something) ? $note_whom = $something : $otherthing) = 'singapore';

    You need to add parentheses:

    defined($something) ? ($note_whom = $something) : ($otherthing = 'singapore');



    John
    --
    Perl isn't a toolbox, but a small machine shop where you can special-order
    certain sorts of tools at low cost and in short order. -- Larry Wall
    John W. Krahn, Oct 3, 2006
    #3
  4. "John W. Krahn" <> writes:
    > Because of precedence you have:
    >
    > (defined($something) ? $note_whom = $something : $otherthing) = 'singapore';
    >
    > You need to add parentheses:
    >
    > defined($something) ? ($note_whom = $something) : ($otherthing = 'singapore');


    Or better yet, write it out:

    if (defined($something)) {
    $note_whome = $something;
    } else {
    $otherthing = 'singapore';
    }

    I always recommend against the ?: operator unless it's for something
    small and self-contained. I avoid using it except for rvalues, unless
    not using it will make things unacceptably ugly. Yes, this is a vague
    standard. Here' an exampe of what is acceptable:

    my $foo = $bar ? 0 : 1;

    Here's something that isn't:

    ($var == 'foo' and $bar < 6 or $x > $y) ? ($this = something($else, 'maybe') or die "um, I dunno") : call_cows($potatoes);

    -=Eric
    Eric Schwartz, Oct 3, 2006
    #4
  5. paul

    paul Guest

    Thank you everyone, I see the mistake already.

    John W. Krahn wrote:
    > paul wrote:
    > >
    > > please help me the check the following code
    > >
    > > use strict;
    > >
    > > my $something = 'phal';
    > > my $otherthing = 'paul';
    > >
    > > my $note_whom = 'szmuzu\'mi';
    > >
    > > defined($something) ? $note_whom = $something : $otherthing =
    > > 'singapore';
    > >
    > > print "$note_whom \n $otherthing \n";
    > >
    > > The result is: singapore
    > > singapore
    > >
    > > I don't know why the scalar $note_whom get the value that is assigned
    > > to $otherthing.
    > >
    > > Please give me the explaination, and thank you.

    >
    > Because of precedence you have:
    >
    > (defined($something) ? $note_whom = $something : $otherthing) = 'singapore';
    >
    > You need to add parentheses:
    >
    > defined($something) ? ($note_whom = $something) : ($otherthing = 'singapore');
    >
    >
    >
    > John
    > --
    > Perl isn't a toolbox, but a small machine shop where you can special-order
    > certain sorts of tools at low cost and in short order. -- Larry Wall
    paul, Oct 3, 2006
    #5
  6. paul

    -berlin.de Guest

    Eric Schwartz <> wrote in comp.lang.perl.misc:
    > "John W. Krahn" <> writes:


    > > defined($something) ? ($note_whom = $something) : ($otherthing = 'singapore');

    >
    > Or better yet, write it out:
    >
    > if (defined($something)) {
    > $note_whome = $something;
    > } else {
    > $otherthing = 'singapore';
    > }
    >
    > I always recommend against the ?: operator unless it's for something
    > small and self-contained. I avoid using it except for rvalues, unless
    > not using it will make things unacceptably ugly. Yes, this is a vague
    > standard. Here' an exampe of what is acceptable:
    >
    > my $foo = $bar ? 0 : 1;
    >
    > Here's something that isn't:
    >
    > ($var == 'foo' and $bar < 6 or $x > $y) ? ($this = something($else,
    > 'maybe') or die "um, I dunno") : call_cows($potatoes);


    One point about ternary ?: is that it is an expression that has a
    value, not primarily a means of flow control. The value should be
    the main point if the construct is used, not the side-effects of
    the code in the branches. In my view, small and self-contained
    enters the image only in so far as it can be hard to find a readable
    format for arbitrarily nested ?: with big complex branches. If
    such a format can be found, there is no limit to the complexity.

    I am of a split mind about using ?: in lvalue context. It *is*
    kind of obscure, but there are situations that simply call for
    it. One is the loop of a binary search, for example

    while ( $low < $high - 1 ) {
    my $mid = ($low + $high)/2;
    $list->[ $mid] le $targ ? $low : $high = $mid;
    }

    The if-else equivalent to ?: is four lines, even cuddling the else.
    I can never bring myself to use it.

    Anno
    -berlin.de, Oct 3, 2006
    #6
  7. Eric Schwartz wrote:
    > "John W. Krahn" <> writes:
    >>Because of precedence you have:
    >>
    >>(defined($something) ? $note_whom = $something : $otherthing) = 'singapore';
    >>
    >>You need to add parentheses:
    >>
    >>defined($something) ? ($note_whom = $something) : ($otherthing = 'singapore');

    >
    > Or better yet, write it out:
    >
    > if (defined($something)) {
    > $note_whome = $something;
    > } else {
    > $otherthing = 'singapore';
    > }


    Or better yet, don't use "cuddled else". :)

    perldoc perlstyle



    John
    --
    Perl isn't a toolbox, but a small machine shop where you can special-order
    certain sorts of tools at low cost and in short order. -- Larry Wall
    John W. Krahn, Oct 3, 2006
    #7
  8. paul

    -berlin.de Guest

    <-berlin.de> wrote in comp.lang.perl.misc:
    > Eric Schwartz <> wrote in comp.lang.perl.misc:
    > > "John W. Krahn" <> writes:


    [...]

    > while ( $low < $high - 1 ) {
    > my $mid = ($low + $high)/2;
    > $list->[ $mid] le $targ ? $low : $high = $mid;
    > }
    >
    > The if-else equivalent to ?: is four lines, even cuddling the else.


    Five!

    > I can never bring myself to use it.
    -berlin.de, Oct 3, 2006
    #8
  9. -berlin.de wrote:
    > Eric Schwartz <> wrote in comp.lang.perl.misc:
    >>"John W. Krahn" <> writes:

    >
    >>>defined($something) ? ($note_whom = $something) : ($otherthing = 'singapore');

    >>Or better yet, write it out:
    >>
    >>if (defined($something)) {
    >> $note_whome = $something;
    >>} else {
    >> $otherthing = 'singapore';
    >>}
    >>
    >>I always recommend against the ?: operator unless it's for something
    >>small and self-contained. I avoid using it except for rvalues, unless
    >>not using it will make things unacceptably ugly. Yes, this is a vague
    >>standard. Here' an exampe of what is acceptable:
    >>
    >>my $foo = $bar ? 0 : 1;
    >>
    >>Here's something that isn't:
    >>
    >>($var == 'foo' and $bar < 6 or $x > $y) ? ($this = something($else,
    >>'maybe') or die "um, I dunno") : call_cows($potatoes);

    >
    > One point about ternary ?: is that it is an expression that has a
    > value, not primarily a means of flow control. The value should be
    > the main point if the construct is used, not the side-effects of
    > the code in the branches. In my view, small and self-contained
    > enters the image only in so far as it can be hard to find a readable
    > format for arbitrarily nested ?: with big complex branches. If
    > such a format can be found, there is no limit to the complexity.
    >
    > I am of a split mind about using ?: in lvalue context. It *is*
    > kind of obscure, but there are situations that simply call for
    > it. One is the loop of a binary search, for example
    >
    > while ( $low < $high - 1 ) {
    > my $mid = ($low + $high)/2;
    > $list->[ $mid] le $targ ? $low : $high = $mid;
    > }
    >
    > The if-else equivalent to ?: is four lines, even cuddling the else.
    > I can never bring myself to use it.


    I assume that you meant to write:

    ( $list->[ $mid] le $targ ? $low : $high ) = $mid;

    because your example never assigns a value to $low.



    John
    --
    Perl isn't a toolbox, but a small machine shop where you can special-order
    certain sorts of tools at low cost and in short order. -- Larry Wall
    John W. Krahn, Oct 3, 2006
    #9
  10. paul

    -berlin.de Guest

    John W. Krahn <> wrote in comp.lang.perl.misc:
    > -berlin.de wrote:



    > > while ( $low < $high - 1 ) {
    > > my $mid = ($low + $high)/2;
    > > $list->[ $mid] le $targ ? $low : $high = $mid;
    > > }
    > >
    > > The if-else equivalent to ?: is four lines, even cuddling the else.
    > > I can never bring myself to use it.

    >
    > I assume that you meant to write:
    >
    > ( $list->[ $mid] le $targ ? $low : $high ) = $mid;
    >
    > because your example never assigns a value to $low.


    Hmmm?

    perl -le '$_ ? $low : $high = 123 for 0, 1; print "$low $high"'

    prints

    123 123

    showing that both are set.

    Anno
    -berlin.de, Oct 3, 2006
    #10
  11. "John W. Krahn" <> writes:
    > Eric Schwartz wrote:
    > > Or better yet, write it out:
    > >
    > > if (defined($something)) {
    > > $note_whome = $something;
    > > } else {
    > > $otherthing = 'singapore';
    > > }

    >
    > Or better yet, don't use "cuddled else". :)
    >
    > perldoc perlstyle


    Feh. I like it that way; perlstyle can go hang. I think it's
    ugly. (Unless, of course, it's house style, in which case I follow
    house style.)

    -=Eric
    Eric Schwartz, Oct 3, 2006
    #11
  12. -berlin.de writes:
    > I am of a split mind about using ?: in lvalue context. It *is*
    > kind of obscure, but there are situations that simply call for
    > it. One is the loop of a binary search, for example
    >
    > while ( $low < $high - 1 ) {
    > my $mid = ($low + $high)/2;
    > $list->[ $mid] le $targ ? $low : $high = $mid;
    > }
    >
    > The if-else equivalent to ?: is four lines, even cuddling the else.
    > I can never bring myself to use it.


    I don't object to that so much, but I'd still use the if-else
    equivalent (and cuddle the else too! Bwahahahah!) just because I
    think it's more readable.

    -=Eric
    Eric Schwartz, Oct 3, 2006
    #12
  13. -berlin.de wrote:
    > John W. Krahn <> wrote in comp.lang.perl.misc:
    >>-berlin.de wrote:

    >
    >
    >>> while ( $low < $high - 1 ) {
    >>> my $mid = ($low + $high)/2;
    >>> $list->[ $mid] le $targ ? $low : $high = $mid;
    >>> }
    >>>
    >>>The if-else equivalent to ?: is four lines, even cuddling the else.
    >>>I can never bring myself to use it.

    >>I assume that you meant to write:
    >>
    >> ( $list->[ $mid] le $targ ? $low : $high ) = $mid;
    >>
    >>because your example never assigns a value to $low.

    >
    > Hmmm?
    >
    > perl -le '$_ ? $low : $high = 123 for 0, 1; print "$low $high"'
    >
    > prints
    >
    > 123 123
    >
    > showing that both are set.


    Yes, interesting, even though the Fine Manual states:

    <QUOTE>
    The operator may be assigned to if both the 2nd and 3rd arguments are
    legal lvalues (meaning that you can assign to them):

    ($a_or_b ? $a : $b) = $c;

    Because this operator produces an assignable result, using assignments
    without parentheses will get you in trouble.
    </QUOTE>



    John
    --
    Perl isn't a toolbox, but a small machine shop where you can special-order
    certain sorts of tools at low cost and in short order. -- Larry Wall
    John W. Krahn, Oct 3, 2006
    #13
  14. paul

    -berlin.de Guest

    John W. Krahn <> wrote in comp.lang.perl.misc:
    > -berlin.de wrote:
    > > John W. Krahn <> wrote in comp.lang.perl.misc:
    > >>-berlin.de wrote:

    > >
    > >
    > >>> while ( $low < $high - 1 ) {
    > >>> my $mid = ($low + $high)/2;
    > >>> $list->[ $mid] le $targ ? $low : $high = $mid;
    > >>> }
    > >>>
    > >>>The if-else equivalent to ?: is four lines, even cuddling the else.
    > >>>I can never bring myself to use it.
    > >>I assume that you meant to write:
    > >>
    > >> ( $list->[ $mid] le $targ ? $low : $high ) = $mid;
    > >>
    > >>because your example never assigns a value to $low.

    > >
    > > Hmmm?
    > >
    > > perl -le '$_ ? $low : $high = 123 for 0, 1; print "$low $high"'
    > >
    > > prints
    > >
    > > 123 123
    > >
    > > showing that both are set.

    >
    > Yes, interesting, even though the Fine Manual states:
    >
    > <QUOTE>
    > The operator may be assigned to if both the 2nd and 3rd arguments are
    > legal lvalues (meaning that you can assign to them):
    >
    > ($a_or_b ? $a : $b) = $c;
    >
    > Because this operator produces an assignable result, using assignments
    > without parentheses will get you in trouble.
    > </QUOTE>


    The parentheses in the Fine Manual are bogus then. It's the other
    way 'round, you have precedence problems when you want assignments
    in the branches of the expression

    $a_or_b ? $a = 123 : $b = 456;
    is
    ($a_or_b ? $a = 123 : $b) = 456;

    so the intended value of 123 for $a will be overwritten by 456 in
    the "true" case. At least the second assignment (to $b) needs
    parens. For symmetry I'd probably write

    $a_or_b ? ( $a = 123) : ( $b = 456);

    if at all. After all, it's a case where only the side-effects
    count.

    Anno
    -berlin.de, Oct 3, 2006
    #14
  15. Eric Schwartz <> wrote:

    > I'd still use the if-else
    > equivalent (and cuddle the else too! Bwahahahah!) just because I

    ^
    > think it's more readable.

    ^^^^^

    If you are the only person who reads your code, then that is
    a very sensible criterion.

    You are in an enviable position. Most of us have to consider
    other programmers when we write programs, where the criterion
    is what do most people think is more readable.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Oct 4, 2006
    #15
  16. [A complimentary Cc of this posting was sent to
    John W. Krahn
    <>], who wrote in article <SJyUg.49711$E67.23424@clgrps13>:
    > > I am of a split mind about using ?: in lvalue context. It *is*
    > > kind of obscure, but there are situations that simply call for
    > > it. One is the loop of a binary search, for example
    > >
    > > while ( $low < $high - 1 ) {
    > > my $mid = ($low + $high)/2;
    > > $list->[ $mid] le $targ ? $low : $high = $mid;
    > > }


    > > The if-else equivalent to ?: is four lines, even cuddling the else.
    > > I can never bring myself to use it.


    > I assume that you meant to write:
    >
    > ( $list->[ $mid] le $targ ? $low : $high ) = $mid;
    >
    > because your example never assigns a value to $low.


    This discussion shows my RULE ONE of Perl:

    Do not ever try to memorize the relative precedence of operators.

    If you religeously follow this advice, then the following RULE TWO
    will help you a lot:

    If in any doubt, put parentheses.

    Myself, I still manage to follow the rule one. I DO NOT know relative
    precedence of + vs &, or of = vs &, or of =~ vs =. This is a useless
    knowledge with very dear consenquences if you misremember...

    What is important to have clean readable text of a program:

    while vs or/and;
    or/and vs everything else;
    the "logical" part of arithetic operations.

    All the rest deserves blissfulness. What is (length 8 + 2)?

    Hope this helps,
    Ilya
    Ilya Zakharevich, Oct 4, 2006
    #16
    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. Stamen Gortchev

    MSDN Examples flawed

    Stamen Gortchev, Sep 25, 2003, in forum: ASP .Net
    Replies:
    11
    Views:
    558
    Stamen Gortchev
    Sep 29, 2003
  2. =?Utf-8?B?YnN0b2RkYXJ0?=

    Flawed logic

    =?Utf-8?B?YnN0b2RkYXJ0?=, Jul 31, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    726
    S. Justin Gengo
    Aug 2, 2004
  3. clintonG
    Replies:
    9
    Views:
    504
    clintonG
    Sep 5, 2005
  4. stax
    Replies:
    45
    Views:
    1,301
    Oliver Wong
    Feb 10, 2006
  5. Roshan
    Replies:
    5
    Views:
    551
Loading...

Share This Page