Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Help understanding a single line of perl: my $count = $row[0] ? $row[0] : 0;

by kdmurphy001 (Sexton)
on Aug 21, 2009 at 18:38 UTC ( [id://790433]=perlquestion: print w/replies, xml ) Need Help??

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

Hello Perl Guru's of the World, I just need a little help understaning what the following line is doing.
my $count = $row[0] ? $row[0] : 0;
Most of it is straight forward but the ? $row[0] : 0; throws me off. I don't know what the "?" and ":" are doing. $row[0] refer to a database pull and will be a value no smaller then 1 (1 to something large). Thank you so much! Going through someone elses scripts can be a bit daunting at times.
  • Comment on Help understanding a single line of perl: my $count = $row[0] ? $row[0] : 0;
  • Download Code

Replies are listed 'Best First'.
Re: Help understanding a single line of perl: my $count = $row[0] ? $row[0] : 0;
by SuicideJunkie (Vicar) on Aug 21, 2009 at 18:42 UTC
Re: Help understanding a single line of perl: my $count = $row[0] ? $row[0] : 0;
by ikegami (Patriarch) on Aug 21, 2009 at 18:44 UTC
    ?: is the conditional operator. See perlop for documentation. In short,
    cond ? then_expr : else_expr
    is similar to
    if (cond) { then_expr } else { else_expr }

    except it can be used inside another expression. It returns the value of then_expr or else_expr as appropriate. That means

    my $count = $row[0] ? $row[0] : 0;
    is short for
    my $count; if ($row[0]) { $count = $row[0]; } else { $count = 0; }

    Update:

    It could also have been written as

    my $count = $row[0] || 0;

    It seems to me it's trying to avoid assigning undef to $count, using zero instead when the situation occurs.

      Well, it's probably closer to:

      my $count = do { if ($row[0]) { $row[0]; } else { 0 } };
      but that's less readable to the uninitiated than what you have. :-P

        if already returns the right thing. It's simply a parser check that prevents it from being used as an expression. (But I guess you know that.)

        I would have used do if it would have made it equivalent, but since it didn't, it was more readable to explain do rather than using do.

      Ah, yes that makes sense. I added a CASE statement to the sql statement but I think that was unnecessary. Thank you so much!
Re: Help understanding a single line of perl: my $count = $row[0] ? $row[0] : 0;
by ramlight (Friar) on Aug 21, 2009 at 18:54 UTC
    One question that might occur to someone who is new to this construct is "Why use it in this case?"

    Because an undefined value is false, this little snippet of code will make sure that count is always defined to a specific value, even if $row[0] is not defined. So if your @row was the result of some operation that happened to return an empty list, $count would set to 0 rather than '';

      So if your @row was the result of some operation that happened to return an empty list, $count would set to 0 rather than '';

      That should be "rather than undef".

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://790433]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2024-04-20 02:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found