The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Astro::App::Satpass2::Utils - Utilities for Astro::App::Satpass2

SYNOPSIS

 use Astro::App::Satpass2::Utils qw{ instance };
 instance( $foo, 'Bar' )
    or die '$foo is not an instance of Bar';

DESCRIPTION

This module is a grab-bag of utilities needed by Astro::App::Satpass2.

This module is private to the Astro::App::Satpass2 package. Any and all functions in it can be modified or revoked without prior notice. The documentation is for the convenience of the author.

All documented subroutines can be exported, but none are exported by default.

SUBROUTINES

This module supports the following exportable subroutines:

expand_tilde

 $expansion = $self->expand_tilde( $file_name );

This mixin (so-called) performs tilde expansion on the argument, returning the result. Arguments that do not begin with a tilde are returned unmodified. In addition to the usual ~/ and ~user/, we support ~+/ (equivalent to ./) and ~~/ (the user's configuration directory). The expansion of ~~/ will result in an exception if the configuration directory does not exist.

All that is required of the invocant is that it support the package's suite of error-reporting methods whinge(), wail(), and weep().

has_method

 has_method( $object, $method );

This exportable subroutine returns a code reference to the named method if the given object has the method, or a false value otherwise. What you actually get is the result of $invocant->can( $method ) if the invocant is a blessed reference, or a return otherwise.

instance

 instance( $object, $class )

This exportable subroutine returns a true value if $object is an instance of $class, and false otherwise. The $object argument need not be a reference, nor need it be blessed, though in these cases the return is false.

load_package

 load_package( $module );
 load_package( $module, 'Astro::App::Satpass2' );
 load_package( { lib => '.lib' }, $module );
 $object->load_package( { complaint => 'wail' }. $module );

This exportable subroutine loads a Perl module. The first argument is the name of the module itself. Subsequent arguments are prefixes to try, without any trailing colons.

This subroutine can also be called as a method. If this is done errors will be reported with a call to the invocant's weep() method if that exists. Otherwise Carp will be loaded and errors will be reported by Carp::confess().

An optional first argument is a reference to a hash of option values. The supported values are:

complaint

This specifies how to report errors if load_package() is called as a method. Valid values are 'whinge', 'wail', and 'weep'. An invalid value is equivalent to 'weep', which is the default. If not called as a method, this option is ignored and a call to Carp::confess() is done.

fatal

If load_package() is called as a method, this argument specifies how to report a failure to load the requested module. Valid values are 'whinge', 'wail' and 'weep'. An invalid value is equivalent to 'wail', which is the default. If load_package() is not called as a method, any true value will cause Carp::croak() to be called, and the failure not to be recorded, so that the load can be retried with a different path.

Either way, a false value causes load_package() to simply return if the requested module can not be loaded.

lib

This specifies a directory to add to @INC before attempting the load. If it is not specified, lib/ in the configuration directory is used. If it is specified as undef, nothing is added to @INC. No expansion is done on the directory name.

In the examples, if $module contains 'Foo', the first example will try to require 'Foo', and the second will try to require 'Astro::App::Satpass2::Foo' and require 'Foo', in that order. The first attempt that succeeds returns the name of the module actually loaded. If no attempt succeeds, undef is returned.

Arguments are cached, and subsequent attempts to load a module simply return the contents of the cache.

merge_hashes

 my $hash_ref = merge_hashes( \%hash1, \%hash2, ... );

This subroutine returns a reference to a hash that contains keys merged from all the hash references passed as arguments. Arguments which are not hash references are removed before processing. If there are no arguments, an empty hash is returned. If there is exactly one argument, it is returned. If there is more than one argument, a new hash is constructed from all keys of all hashes, and that hash is returned. If the same key appears in more than one argument, the value from the right-most argument is the one returned.

my_dist_config

 my $cfg_dir = my_dist_config( { 'create-directory' => 1 } );

This subroutine returns a path to the user's configuration directory. If environment variable ASTRO_APP_SATPASS2_CONFIG_DIR is defined, that is returned regardless of any arguments. Otherwise it simply wraps

 File::HomeDir->my_dist_config( 'Astro-App-Satpass2' );

You can pass an optional reference to an options hash (sic!). The only supported option is {'create-directory'}, which is passed verbatim to the File::HomeDir 'create' option.

If the configuration directory is found or successfully created, the path to it is returned. Otherwise undef is returned.

quoter

 say scalar quoter( @vals );
 say quoter( @vals );

This exportable subroutine quotes and escapes its arguments as necessary for the parser. Specifically, if an argument is:

* undef, 'undef' is returned;

* a number, $string is returned unmodified;

* an empty string, '' is returned;

* a string containing white space, quotes, or dollar signs, the value is escaped and enclosed in double quotes ("").

* anything else is returned unmodified.

If called in scalar context, the results are concatenated with join ' ', .... Otherwise they are simply returned.

__arguments

 my ( $self, $opt, @args ) = __arguments( @_ );

This subroutine is intended to be used to unpack the arguments of an Astro::App::Satpass2 interactive method or a code macro.

Specifically, this subroutine expects to be called from a subroutine or method that has the Verb() attribute, and expects the contents of the parentheses in the Verb() attribute to be a set of white-space-delimited Getopt::Long option specifications. Further, if the subroutine has a Configure() attribute, it will be used to configure the Getopt::Long object.

The first argument is expected to be the invocant, and is always returned intact.

Subsequent arguments are preprocessed by calling their dereference() method if it exists. This is a severe wart on the code, but was needed (I thought) to get certain arguments through Template-Toolkit. Arguments that do not have a dereference() method are left unmodified, as are any unblessed arguments.

If the first remaining argument after preprocessing is a hash reference, it is assumed that the options have already been processed, and we simply return the invocant and the remaining arguments as they now stand.

If the first remaining argument after preprocessing is not a hash reference, we run all the remaining arguments through Getopt::Long, and return the invocant, the options hash populated by Getopt::Long, and all remaining arguments. If Getopt::Long encounters an error an exception is thrown. This is done using the invocant's wail() method if it has one, otherwise Carp is loaded and Carp::croak() is called.

SUPPORT

Support is by the author. Please file bug reports at http://rt.cpan.org, or in electronic mail to the author.

AUTHOR

Thomas R. Wyant, III wyant at cpan dot org

COPYRIGHT AND LICENSE

Copyright (C) 2011-2015 by Thomas R. Wyant, III

This program is free software; you can redistribute it and/or modify it under the same terms as Perl 5.10.0. For more details, see the full text of the licenses in the directory LICENSES.

This program is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose.