App-PhotoDB
view release on metacpan or search on metacpan
lib/App/PhotoDB/funcs.pm view on Meta::CPAN
use Exporter qw(import);
use Config::IniHash;
use YAML;
use Image::ExifTool;
use Term::ReadLine;
use Term::ReadLine::Perl;
use File::Basename;
use Time::Piece;
use Text::TabularDisplay;
our @EXPORT_OK = qw(prompt db updaterecord deleterecord newrecord notimplemented nocommand nosubcommand listchoices lookupval lookuplist today validate ini printlist round pad lookupcol thin resolvenegid chooseneg annotatefilm keyword parselensmodel ...
=head2 prompt
Prompt the user for an arbitrary value. Has various options for data validation and customisation of the prompt.
If the provided input fails validation, or if a blank string is given when required=1 then the prompt is repeated.
=head4 Usage
my $camera = &prompt({prompt=>'What model is the camera?', required=>1, default=>$$defaults{model}, type=>'text'});
lib/App/PhotoDB/funcs.pm view on Meta::CPAN
my $term = $App::PhotoDB::term;
my $rv;
# Repeatedly prompt user until we get a response of the correct type
do {
my $input = $term->readline($msg);
# Use default value if user gave blank input
$rv = ($input eq "") ? $default:$input;
# Prompt again if the input doesn't pass validation, or if it's a required field that was blank
} while (!&validate({val => $rv, type => $type}) || ($rv eq '' && $required == 1));
# Rewrite friendly bools and then return the value
if ($type eq 'boolean') {
return friendlybool($rv);
} else {
return $rv;
}
}
=head2 term
lib/App/PhotoDB/funcs.pm view on Meta::CPAN
=cut
sub term {
my $term = Term::ReadLine->new('PhotoDB');
$term->ornaments(0);
$term->MinLine(7);
return $term;
}
=head2 validate
Validate that a value is a certain data type
=head4 Usage
my $result = &validate({val => 'hello', type => 'text'});
=head4 Arguments
=item * C<$val> The value to be validated
=item * C<$type> Data type to validate as, out of C<text>, C<integer>, C<boolean>, C<date>, C<decimal>, C<time>. Defaults to C<text>.
=head4 Returns
Returns C<1> if the value passes validation as the requested type, and C<0> if it doesn't.
=cut
sub validate {
# Pass in a hashref of arguments
my $href = shift;
# Unpack the hashref and set default values
my $val = $href->{val}; # The value to be validated
my $type = $href->{type} || 'text'; # Data type to validate as, out of text, integer, boolean, date, decimal, time
die "Must provide value for \$val\n" if !defined($val);
# Empty string always passes validation
if ($val eq '') {
return 1;
}
elsif ($type eq 'boolean') {
if ($val =~ m/^(y(es)?|no?|false|true|1|0)$/i) {
return 1;
( run in 0.293 second using v1.01-cache-2.11-cpan-05444aca049 )