Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

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 ( #790438=note: print w/replies, xml ) Need Help??


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

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 '';

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

Replies are listed 'Best First'.
Re^2: Help understanding a single line of perl: my $count = $row[0] ? $row[0] : 0;
by ikegami (Patriarch) on Aug 21, 2009 at 19:37 UTC

    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: note [id://790438]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2023-11-28 19:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?