1b39c5158Smillertpackage Hints_pod_examples; 2b39c5158Smillertuse strict; 3b39c5158Smillertuse warnings; 4b39c5158Smillert 5b8851fccSafresh1use Exporter 5.57 'import'; 6b39c5158Smillert 7b39c5158Smillertour %DOES = ( 'autodie::hints::provider' => 1 ); 8b39c5158Smillert 9b39c5158Smillertour @EXPORT_OK = qw( 10b39c5158Smillert undef_scalar false_scalar zero_scalar empty_list default_list 11b39c5158Smillert empty_or_false_list undef_n_error_list foo re_fail bar 12b39c5158Smillert think_positive my_system bizarro_system 13b39c5158Smillert); 14b39c5158Smillert 15b39c5158Smillertuse autodie::hints; 16b39c5158Smillert 17b39c5158Smillertsub AUTODIE_HINTS { 18b39c5158Smillert return { 19b39c5158Smillert # Scalar failures always return undef: 20*9f11ffb7Safresh1 undef_scalar => { fail => sub { !defined($_[0]) } }, 21b39c5158Smillert 22b39c5158Smillert # Scalar failures return any false value [default behaviour]: 23b39c5158Smillert false_scalar => { fail => sub { return ! $_[0] } }, 24b39c5158Smillert 25b39c5158Smillert # Scalar failures always return zero explicitly: 26*9f11ffb7Safresh1 zero_scalar => { fail => sub { defined($_[0]) && $_[0] eq '0' } }, 27b39c5158Smillert 28b39c5158Smillert # List failures always return empty list: 29b39c5158Smillert # We never want these called in a scalar context 30*9f11ffb7Safresh1 empty_list => { scalar => sub { 1 }, list => sub { !@_ } }, 31b39c5158Smillert 32b39c5158Smillert # List failures return C<()> or C<(undef)> [default expectation]: 33b39c5158Smillert default_list => { fail => sub { ! @_ || @_ == 1 && !defined $_[0] } }, 34b39c5158Smillert 35b39c5158Smillert # List failures return C<()> or a single false value: 36b39c5158Smillert empty_or_false_list => { fail => sub { ! @_ || @_ == 1 && !$_[0] } }, 37b39c5158Smillert 38b39c5158Smillert # List failures return (undef, "some string") 39b39c5158Smillert undef_n_error_list => { fail => sub { @_ == 2 && !defined $_[0] } }, 40b39c5158Smillert }; 41b39c5158Smillert} 42b39c5158Smillert 43b39c5158Smillert# Define some subs that all just return their arguments 44b39c5158Smillertsub undef_scalar { return wantarray ? @_ : $_[0] } 45b39c5158Smillertsub false_scalar { return wantarray ? @_ : $_[0] } 46b39c5158Smillertsub zero_scalar { return wantarray ? @_ : $_[0] } 47b39c5158Smillertsub empty_list { return wantarray ? @_ : $_[0] } 48b39c5158Smillertsub default_list { return wantarray ? @_ : $_[0] } 49b39c5158Smillertsub empty_or_false_list { return wantarray ? @_ : $_[0] } 50b39c5158Smillertsub undef_n_error_list { return wantarray ? @_ : $_[0] } 51b39c5158Smillert 52b39c5158Smillert 53b39c5158Smillert# Unsuccessful foo() returns 0 in all contexts... 54b39c5158Smillertautodie::hints->set_hints_for( 55b39c5158Smillert \&foo, 56b39c5158Smillert { 57*9f11ffb7Safresh1 scalar => sub { defined($_[0]) && $_[0] == 0 }, 58*9f11ffb7Safresh1 list => sub { @_ == 1 && defined($_[0]) && $_[0] == 0 }, 59b39c5158Smillert } 60b39c5158Smillert); 61b39c5158Smillert 62b39c5158Smillertsub foo { return wantarray ? @_ : $_[0] } 63b39c5158Smillert 64b39c5158Smillert# Unsuccessful re_fail() returns 'FAIL' or '_FAIL' in scalar context, 65b39c5158Smillert# returns (-1) in list context... 66b39c5158Smillertautodie::hints->set_hints_for( 67b39c5158Smillert \&re_fail, 68b39c5158Smillert { 69b39c5158Smillert scalar => qr/^ _? FAIL $/xms, 70*9f11ffb7Safresh1 list => sub { @_ == 1 && $_[0] eq -1 }, 71b39c5158Smillert } 72b39c5158Smillert); 73b39c5158Smillert 74b39c5158Smillertsub re_fail { return wantarray ? @_ : $_[0] } 75b39c5158Smillert 76b39c5158Smillert# Unsuccessful bar() returns 0 in all contexts... 77b39c5158Smillertautodie::hints->set_hints_for( 78b39c5158Smillert \&bar, 79b39c5158Smillert { 80*9f11ffb7Safresh1 scalar => sub { defined($_[0]) && $_[0] == 0 }, 81*9f11ffb7Safresh1 list => sub { @_ == 1 && defined($_[0]) && $_[0] == 0 }, 82b39c5158Smillert } 83b39c5158Smillert); 84b39c5158Smillert 85b39c5158Smillertsub bar { return wantarray ? @_ : $_[0] } 86b39c5158Smillert 87b39c5158Smillert# Unsuccessful think_positive() returns negative number on failure... 88b39c5158Smillertautodie::hints->set_hints_for( 89b39c5158Smillert \&think_positive, 90b39c5158Smillert { 91b39c5158Smillert scalar => sub { $_[0] < 0 }, 92b39c5158Smillert list => sub { $_[0] < 0 }, 93b39c5158Smillert } 94b39c5158Smillert); 95b39c5158Smillert 96b39c5158Smillertsub think_positive { return wantarray ? @_ : $_[0] } 97b39c5158Smillert 98b39c5158Smillert# Unsuccessful my_system() returns non-zero on failure... 99b39c5158Smillertautodie::hints->set_hints_for( 100b39c5158Smillert \&my_system, 101b39c5158Smillert { 102b39c5158Smillert scalar => sub { $_[0] != 0 }, 103b39c5158Smillert list => sub { $_[0] != 0 }, 104b39c5158Smillert } 105b39c5158Smillert); 106b39c5158Smillertsub my_system { return wantarray ? @_ : $_[0] }; 107b39c5158Smillert 108b39c5158Smillert1; 109