| 1 | package Data::Default; | ||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 1 | ▶ | use base 'Exporter'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use vars qw[@ISA @EXPORT_OK %EXPORT_TAGS]; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | # version | ||||||||||
| 7 | 1 | ▶ | use vars '$VERSION'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | $VERSION = '0.11'; | |||||||||
| 9 | |||||||||||
| 10 | =head1 NAME | ||||||||||
| 11 | |||||||||||
| 12 | Data::Default -- Small utility for getting the default value of | ||||||||||
| 13 | an argument or variable. | ||||||||||
| 14 | |||||||||||
| 15 | =head1 SYNOPSIS | ||||||||||
| 16 | |||||||||||
| 17 | use Data::Default ':all'; | ||||||||||
| 18 | |||||||||||
| 19 | # variables | ||||||||||
| 20 | my ($var); | ||||||||||
| 21 | |||||||||||
| 22 | # later... | ||||||||||
| 23 | |||||||||||
| 24 | if (default $var, 1) { | ||||||||||
| 25 | # stuff if the $var is undef or set to a true value | ||||||||||
| 26 | } | ||||||||||
| 27 | else { | ||||||||||
| 28 | # stuff if the $var is defined and set to true | ||||||||||
| 29 | } | ||||||||||
| 30 | |||||||||||
| 31 | =head1 DESCRIPTION | ||||||||||
| 32 | |||||||||||
| 33 | Just a little utility for getting the default value of an argument or | ||||||||||
| 34 | parameter. All it really does is accept an array of arguments, then | ||||||||||
| 35 | return the first argument that is defined. | ||||||||||
| 36 | |||||||||||
| 37 | This function is usually used in a subroutine to get the | ||||||||||
| 38 | default value of a parameter. A typical usage would be in a subroutine | ||||||||||
| 39 | like this: | ||||||||||
| 40 | |||||||||||
| 41 | sub mysub { | ||||||||||
| 42 | my (%opts) = @_; | ||||||||||
| 43 | |||||||||||
| 44 | if (default $opts{'some-option'}, 1) { | ||||||||||
| 45 | # do stuff some-option was sent and was true or was not sent | ||||||||||
| 46 | } | ||||||||||
| 47 | else { | ||||||||||
| 48 | # do stuff some-option was sent and was false | ||||||||||
| 49 | } | ||||||||||
| 50 | } | ||||||||||
| 51 | |||||||||||
| 52 | You might prefer to use C<Attribute::Default> by Stephen Nelson which | ||||||||||
| 53 | provides similar functionality. | ||||||||||
| 54 | |||||||||||
| 55 | =head1 INSTALLATION | ||||||||||
| 56 | |||||||||||
| 57 | String::Util can be installed with the usual routine: | ||||||||||
| 58 | |||||||||||
| 59 | perl Makefile.PL | ||||||||||
| 60 | make | ||||||||||
| 61 | make test | ||||||||||
| 62 | make install | ||||||||||
| 63 | |||||||||||
| 64 | =cut | ||||||||||
| 65 | |||||||||||
| 66 | # export | ||||||||||
| 67 | 1 | @ISA = 'Exporter'; | |||||||||
| 68 | 1 | @EXPORT_OK = qw[ default defcontent modcontent choose ]; | |||||||||
| 69 | 1 | %EXPORT_TAGS = ('all' => [@EXPORT_OK]); | |||||||||
| 70 | |||||||||||
| 71 | # default | ||||||||||
| 72 | 1 | ▶ | sub default { | ||||||||
|
Subroutine
default: called CC=3 SCAR=11.7
Pod
undocumented
| |||||||||||
| 73 | 1 | ▶ | for (my $i=0; $i<=$#_; $i++) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 74 | 2 | ▶ | defined($_[$i]) and return $_[$i]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 75 | } | ||||||||||
| 76 | |||||||||||
| 77 | 0 | return undef; | |||||||||
| 78 | } | ||||||||||
| 79 | |||||||||||
| 80 | # return true | ||||||||||
| 81 | 1 | 1; | |||||||||
| 82 | |||||||||||
| 83 | =head1 TERMS AND CONDITIONS | ||||||||||
| 84 | |||||||||||
| 85 | Copyright (c) 2010 by Miko O'Sullivan. All rights reserved. This program is | ||||||||||
| 86 | free software; you can redistribute it and/or modify it under the same terms | ||||||||||
| 87 | as Perl itself. This software comes with B<NO WARRANTY> of any kind. | ||||||||||
| 88 | |||||||||||
| 89 | =head1 AUTHORS | ||||||||||
| 90 | |||||||||||
| 91 | Miko O'Sullivan | ||||||||||
| 92 | F<miko@idocs.com> | ||||||||||
| 93 | |||||||||||
| 94 | =head1 VERSION | ||||||||||
| 95 | |||||||||||
| 96 | =over | ||||||||||
| 97 | |||||||||||
| 98 | =item Version 0.10 November 7, 2010 | ||||||||||
| 99 | |||||||||||
| 100 | Initial release | ||||||||||
| 101 | |||||||||||
| 102 | =item Version 0.11 November 8, 2010 | ||||||||||
| 103 | |||||||||||
| 104 | Fixed bug: Exporter was not being loaded. | ||||||||||
| 105 | |||||||||||
| 106 | =back | ||||||||||
| 107 | |||||||||||
| 108 | |||||||||||
| 109 | =cut | ||||||||||