? : statement not working as expected

Discussion in 'Perl Misc' started by Patrick Hartman, Mar 9, 2010.

  1. Hi, I am trying to use ? : statements instead of if/else blocks
    wherever possible to keep my code shorter. Below is a conditional
    statement that for some reason is not working as expected with the ? :
    method:

    #!/usr/bin/perl -w
    use strict;

    # image name
    my $image = 'yf446_mk_nb_0_0_6_ia';

    # break out seperate pieces
    my($bare_style,$color,$brand,$slot1,$slot2,$shot,$type) = $image =~ m/
    ^(\w+)_(\w+)_(\w+)_(\w+)_(\w+)_(\w+)_(\w+)$/;


    # this is not working as expected, returns the 'else' result
    {
    my $style;
    $brand eq 'nb' ? $style = $bare_style . $color : $style =
    $bare_style;
    print "?: version: $style\n";
    }

    # this works as expected
    {
    my $style;

    if ($brand eq 'nb') {
    $style = $bare_style . $color;
    }
    else {
    $style = $bare_style;
    }

    print "if/else block version: $style\n";
    }


    Any ideas why this would not work in the first statement? I appreciate
    it,

    Patrick
     
    Patrick Hartman, Mar 9, 2010
    #1
    1. Advertisements

  2. Patrick Hartman

    Uri Guttman Guest

    >>>>> "PH" == Patrick Hartman <> writes:

    PH> Hi, I am trying to use ? : statements instead of if/else blocks
    PH> wherever possible to keep my code shorter. Below is a conditional
    PH> statement that for some reason is not working as expected with the ? :
    PH> method:

    this is a classic newbie misuse of ?:.

    PH> my $style;
    PH> $brand eq 'nb' ? $style = $bare_style . $color : $style =
    PH> $bare_style;
    PH> print "?: version: $style\n";

    $style = $brand eq 'nb' ? "$bare_style$color" : $bare_style ;

    ?: is meant to return an expression based on a boolean. it it NOT meant
    to do side effects like assignment. you could fix your code with () but
    it would still be wrong. the issue was precedence. the proper solution i
    pasted above. the whole idea is to assign once but choose what to
    assign. then the precedence (= is higher binding than ?:) works to your
    advantage.

    and given what you are doing, there are other ways too:

    $style = $bare_style . ($brand eq 'nb' ? $color : '') ;

    $style = $bare_style ;
    $style .= $color if $brand eq 'nb' ;

    uri

    --
    Uri Guttman ------ -------- http://www.sysarch.com --
    ----- Perl Code Review , Architecture, Development, Training, Support ------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
     
    Uri Guttman, Mar 9, 2010
    #2
    1. Advertisements

  3. Patrick Hartman

    Ted Zlatanov Guest

    On Tue, 9 Mar 2010 09:33:44 -0800 (PST) Patrick Hartman <> wrote:

    PH> # image name
    PH> my $image = 'yf446_mk_nb_0_0_6_ia';

    PH> # break out seperate pieces
    PH> my($bare_style,$color,$brand,$slot1,$slot2,$shot,$type) = $image =~ m/
    PH> ^(\w+)_(\w+)_(\w+)_(\w+)_(\w+)_(\w+)_(\w+)$/;

    Doesn't it bother you that there's so much repetition there?

    split('_', $image) will return a list of elements just as easily.

    Also note that \w will match the '_' character as well so your
    expression can only work accidentally.

    PH> # this is not working as expected, returns the 'else' result
    PH> {
    PH> my $style;
    PH> $brand eq 'nb' ? $style = $bare_style . $color : $style =
    PH> $bare_style;
    PH> print "?: version: $style\n";
    PH> }

    Never, ever assign in a conditional. Those who break that rule usually
    do it for a very good reason (e.g. in an input loop) but 99% of the time
    you simply should not do it.

    PH> # this works as expected
    PH> {
    PH> my $style;

    PH> if ($brand eq 'nb') {
    PH> $style = $bare_style . $color;
    PH> }
    PH> else {
    PH> $style = $bare_style;
    PH> }

    PH> print "if/else block version: $style\n";
    PH> }

    PH> Any ideas why this would not work in the first statement? I appreciate
    PH> it,

    Uri explained, but really, which one would you rather see in a program?

    Ted
     
    Ted Zlatanov, Mar 9, 2010
    #3
  4. Thanks for the explanation and insight Uri and Ted, I appreciate it. I
    am clearly using the conditional too often (yes I am a newbie). Also
    Ted thanks for pointing out the problem with my expression, I forgot
    that \w includes stuff besides letters.

    Patrick
     
    Patrick Hartman, Mar 9, 2010
    #4
    1. Advertisements

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. Richard Huff
    Replies:
    2
    Views:
    557
    Richard Huff
    Jan 6, 2004
  2. Natty Gur
    Replies:
    1
    Views:
    2,030
    Marshal Antony
    Mar 3, 2004
  3. =?Utf-8?B?amFtbWVy?=

    Validation not working as expected

    =?Utf-8?B?amFtbWVy?=, Aug 19, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    508
    =?Utf-8?B?amFtbWVy?=
    Aug 19, 2005
  4. Steve Franks

    Cache not working as expected

    Steve Franks, Sep 11, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    600
    Jeremy S.
    Sep 11, 2005
  5. SteveSu
    Replies:
    2
    Views:
    18,016
    SteveSu
    Nov 8, 2005
  6. J055
    Replies:
    1
    Views:
    3,323
    Steven Cheng[MSFT]
    Mar 13, 2006
  7. radvieira
    Replies:
    1
    Views:
    2,773
    Thomas Hawtin
    Aug 30, 2005
  8. Jay
    Replies:
    8
    Views:
    558
    Bertilo Wennergren
    Nov 6, 2003
Loading...