|Just another Perl shrine|
RFC: Defined-Or for before Perl 5.10by molecules (Monk)
|on Oct 29, 2010 at 17:21 UTC||Need Help??|
molecules has asked for the
wisdom of the Perl Monks concerning the following question:
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 firstname.lastname@example.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 email@example.com. 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?