theAcolyte has asked for the wisdom of the Perl Monks concerning the following question:

Ok, I saw a code snippet somewhere on this site a few days ago.. and just spent the better part of an hour trying to find it again, without luck.

It showed a nice, simple sytax for saying

if(!defined($someValue)) { $myVar = "default" } else { $myVar = $somevalue }

except it didn't use an if or an else or even, if I remmeber correctly, curlie brackets. I seem to remember a ? or a : in there.

Anyone help out? :-) Thanks

  • Comment on Syntax for 1 line "either this or this"

Replies are listed 'Best First'.
Re: Syntax for 1 line "either this or this"
by BrowserUk (Patriarch) on May 29, 2003 at 22:58 UTC

     $myvar = !defined $somevalue ? "default" : $somevalue;

    or better yet

     $myvar = defined $somevalue ? $somevalue : "default";


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller


      This looks like exactly what I remembered. Thanks so much. If it's not too imposing, however, could you explain why/how this works? OTOH, I may be able to find info on "?" and ":" as operators in Programming Perl. :-)
        You will find information about it in the Camel book... the "?" and ":" comprise a single operator called the "ternary" or "trinary" operator, so look for that in the index. You can also find information about it in perlop. Search for "Ternary".

        -- Mike

        --
        just,my${.02}

        Programming Perl 3rd Ed. Section 3.16 Conditional operator.

        Or perlsyn Conditional operator.


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller


Re: Syntax for 1 line "either this or this"
by halley (Prior) on May 30, 2003 at 00:48 UTC
    A related operator idiom for assuming a default value:
    $myVar = $somevalue || 'default';

    Technically, it's not quite the same as the previous statements, but I'd say it is more commonly used than the trinary form.

    There's whether something exists, whether it is defined (has a value different from undef), whether it has a "true" value (anything but undef or '' or 0 or '0'), and whether it's a reference or a non-referential scalar.

    --
    [ e d @ h a l l e y . c c ]

      Above example ought to be qualified w/ a statement similar to "To set a default value if $somevalue is false, use this ...".

      I discovered that i need to set default iff a given value is undefined, not false. Thankfully, perl 6 will make it to take care of that just as easily -- search for "It's a setup" -- as the above example w/ help of // operator.

      Change, May 30 2003: Removed "binary" from the last sentence describing the behaviour of //. The operator is listed being binary in the referenced portion ("It's a setup") of Exegesis 3, but as an unary operator at the end of the article.

Re: Syntax for 1 line "either this or this"
by arthas (Hermit) on May 29, 2003 at 23:10 UTC
    Here it goes:
    $myVar = !defined($somevalue) ? "default" : $somevalue;
    This trinary operator comes directly from C language.

    Michele.
Re: Syntax for 1 line "either this or this"
by pzbagel (Chaplain) on May 29, 2003 at 22:58 UTC
    $myVar=!defined($someValue)?"default":$someValue;
Re: Syntax for 1 line "either this or this"
by DigitalKitty (Parson) on May 30, 2003 at 16:18 UTC
    Hi theAcolyte.

    The ternary operator '?:' was inherited from 'C' I believe. It works like this:

    #!/usr/bin/perl -w use strict; my $myvar; my $val1; $myvar = defined $val1 ? $val1 : "Blah"; print $myvar;

    If the value stored in the variable $val1 is defined, assign the value stored at its address to $myvar. Otherwise, assign the string "Blah" to it. The logic could be expressed as:

    If $var1 is defined, perform the assignment on the immediate right of the '?'. Else, perform the assignment on the right of the ':'.

    Hope this helps,
    -Katie.
Re: Syntax for 1 line "either this or this"
by Anonymous Monk on May 31, 2003 at 07:14 UTC
    was it something like this?
    $myVar = (!defined($someValue) ? "default" : $somevalue);