App-Basis
view release on metacpan or search on metacpan
lib/App/Basis.pm view on Meta::CPAN
# ----------------------------------------------------------------------------
# control how we output things to help with testing
sub _output
{
my ( $where, $msg ) = @_ ;
if ( !$_test_mode ) {
if ( $where =~ /stderr/i ) {
say STDERR $msg ;
} else {
say $msg ;
}
}
}
# ----------------------------------------------------------------------------
sub set_log_file
{
my ($file) = @_ ;
lib/App/Basis.pm view on Meta::CPAN
sub set_verbose
{
$verbose = shift ;
}
sub verbose
{
my ($msg) = @_ ;
say STDERR $msg if ($verbose) ;
}
sub verbose_data
{
if ( @_ % 2 ) {
say STDERR Dump(@_) if ($verbose) ;
} else {
my ($data) = @_ ;
say STDERR Dump($data) if ($verbose) ;
}
}
# ----------------------------------------------------------------------------
# check that the option structure does not have repeated things in it
# returns string of any issue
sub _validate_options
{
my ($options) = @_ ;
lib/App/Basis.pm view on Meta::CPAN
$txt = _header($txt) ;
$txt = _bold_underline_strike($txt) ;
$txt = _list($txt) ;
$txt = _quote($txt) ;
$txt = _color($txt) ;
# put back inline blocks
$txt =~ s{(`)(.+?)\1}{$1._get_source($2,\%code).$1}egm ;
# put back code blocks too
$txt =~ s{(`{3})(.+?)(?<!`)\1(?!`)}
{$1._get_source($2,\%code).$1}egs ;
say $txt;
}
# ----------------------------------------------------------------------------
# make sure we do any cleanup required
END {
# call any user supplied cleanup
if ($_app_simple_cleanup_func) {
$_app_simple_cleanup_func->() ;
lib/App/Basis.pm view on Meta::CPAN
}
sub debug_func {
my ($lvl, $debug) = @_;
if(!$debug) {
$debug = $lvl ;
# set a default level
$lvl = 'INFO' ;
}
say STDERR strftime( '%Y-%m-%d %H:%M:%S', gmtime( time() ) ) . " [$lvl] " . get_program() . " " . $debug;
}
# main
my %opt = App::Basis::init_app(
help_text => 'Sample program description'
, help_cmdline => 'extra stuff to print about command line use'
, options => {
'file|f=s' => {
desc => 'local system location of xml data'
, required => 1
lib/App/Basis.pm view on Meta::CPAN
=head1 DESCRIPTION
There are a number of ways to help script development and to encorage people to do the right thing.
One of thses is to make it easy to get parameters from the command line. Obviously you can play with Getopt::Long and
continuously write the same code and add in your own handlers for help etc, but then your co-workers and friends
make not be so consistent, leading to scripts that have no help and take lots of cryptic parameters.
So I created this module to help with command line arguments and displaying help, then I added L<App::Basis::Config> because
everyone needs config files and does not want to constantly repeat themselves there either.
So how is better than other similar modules? I can't say that it is, but it meets my needs.
There is app help available, there is basic debug functionality, which you can extend using your own function,
you can daemonise your script or run a shell command and get the output/stderr/return code.
If you choose to use App::Basis::Config then you will find easy methods to manage reading/saving YAML based config data.
There are (or will be) other App::Basis modules available to help you write scripts without you having to do complex things
or write lots of code.
There is a helper script to create the boilerplate for an appbasis script, see L<appbasis>
lib/App/Basis.pm view on Meta::CPAN
Dump a data structure to STDERR if verbose has been turned on
its different to debug logging with generally will go to a file
set_verbose( 1) ;
verbose_data( \%some_hash) ;
=item init_app
B<Parameters> hash of these things
help_text - what to say when people do app --help
help_cmdline - extra things to put after the sample args on a sample command line (optional)
cleanup - coderef of function to call when your script ends (optional)
debug - coderef of function to call to save/output debug data (optional, recommended)
'verbose' - use verbose mode (optional) will trigger set_verbose by default
log_file - alternate name of file to store debug to
ctrlc_func - coderef of function to call when user presses ctrl-C
options - hashref of program arguments
simple way
'fred' => 'some description of fred'
'fred|f' => 'fred again, also allows -f as a variant'
lib/App/Basis/Config.pm view on Meta::CPAN
=item has_data
test if there is any data in the config
=item changed
has the config data changed since the last save, or mark it as changed
if( $data->changed) {
say "somethings changed"
}
# my $data = $cfg->raw ;
$data->{deep}->{nested}->{item} = 123 ;
# mark the data as changed
$data->changed( 1) ;
# save in the default config file
$data->store() ;
B<Parameter>
flag optional, used as a getter if flag is missing, otherwise a setter
( run in 0.822 second using v1.01-cache-2.11-cpan-d7a12ab2c7f )