Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • by jordan (120) on 2002.09.11 16:04 (#12761) Homepage Journal
    The first one is actually justified in Perl in the case that you need to return numeric 0 or 1, although you might prefer the ?: operator:

            return ( $dbh ? 1 : 0 );

    Some people, not I, stay away from the ?: like the plague because they think it's unreadable.

    There's a trick I've used in C to turn an integer into 1/0:

            return !!dbh;

    This is too clever, I think, simply:

            return ( dbh != 0 );

    Is probably preferable.

    But, doing this in Perl:

            return !!$dbh;

    Returns 1 and "".

    If you really want to return numeric 1 or 0, the if statement your co-worked had is not terrible, really. Very easy to understand and it works. Returning $dbh, while satisfying the same logical tests to the caller, may be returning any data type.

    The other two are, well, harder to justify, to say the least.