Dear Perlmonks,

I wanted a defined-or function that works on Perls before 5.10. I tried to find something like it but "defined" and "or" are such common words, I finally gave up searching.

I submitted this RFC to modules@perl.org but did not get any responses.

To illustrate its use, here are some results copied from running my test file.

ok 1 - 0 = defined_or( undef, 0);
ok 2 - 0 = defined_or( 0, undef);
ok 3 - 0 = defined_or( undef, defined_or( undef, 0));
ok 4 - 1 = defined_or( undef, 1);
ok 5 - 1 = defined_or( 1, undef);
ok 6 - 1 = defined_or( undef, defined_or( undef, 1));
ok 7 - undef = defined_or( undef, undef);
ok 8 - undef = defined_or( undef, undef);
ok 9 - undef = defined_or( undef, defined_or( undef, undef));
ok 10 - 1 = defined_or( 1, 0);
ok 11 - 0 = defined_or( 0, 1);
ok 12 - 1 = defined_or( 1, 1);
ok 13 - 1 = defined_or( 1, 1);

And the POD:

SYNOPSIS
        use DefinedOr qw/ defined_or /;

        my $foo = defined_or( shift(), 1); 
        my $bar = defined_or( $baz, defined_or( $boo, 1));

Description
    This is a simple implementation of defined/or (without the beautifully
    simple syntax) for those that are not using Perl 5.10 for some reason.

EXPORTED SUBROUTINES
  defined_or
    This subroutine takes two arguments. It returns the first argument if it
    is defined. Otherwise, it returns the second argument.

    Yes, this is actually a subroutine, not pretty syntax like //.

INCOMPATIBILITIES
    None reported.

BUGS
    None have been reported as of the current release. Please report any 
    bugs to bug-definedor@rt.cpan.org.

LIMITATIONS
    Currently runs fine on Perl 5.8.8. If someone wants to try it on older
    Perls, then delete the line that says use 5.008;. If you do, 
    please let me know what version of Perl you're running it on. 

AUTHOR
    Christopher Bottoms, "<molecules at cpan.org>"

ACKNOWLEDGEMENTS
    Thanks to all those who developed the true defined-or for Perl 6 and 
    Perl 5.10. This is a weak imitation.

LICENSE AND COPYRIGHT
    This program is free software; you can redistribute it and/or modify it
    under the terms of either: the GNU General Public License as published
    by the Free Software Foundation; or the Artistic License.

    See http://dev.perl.org/licenses/ for more information.

    Copyright 2010 Christopher Bottoms.

What do you think?

Thanks!


In reply to RFC: Defined-Or for before Perl 5.10 by molecules

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.