Scriptalicious(3pm) | User Contributed Perl Documentation | Scriptalicious(3pm) |
Scriptalicious - Make scripts more delicious to SysAdmins
use Scriptalicious -progname => "pu"; our $VERSION = "1.00"; my $url = "."; getopt getconf("u|url" => \$url); run("echo", "doing something with $url"); my $output = capture("svn", "info", $url); __END__ =head1 NAME pu - an uncarved block of wood =head1 SYNOPSIS pu [options] arguments =head1 DESCRIPTION This script's function is to be a blank example that many great and simple scripts may be built upon. Remember, you cannot carve rotten wood. =head1 COMMAND LINE OPTIONS =over =item B<-h, --help> Display a program usage screen and exit. =item B<-V, --version> Display program version and exit. =item B<-v, --verbose> Verbose command execution, displaying things like the commands run, their output, etc. =item B<-q, --quiet> Suppress all normal program output; only display errors and warnings. =item B<-d, --debug> Display output to help someone debug this script, not the process going on. =back
This module helps you write scripts that conform to best common practices, quickly. Just include the above as a template, and your script will accept all of the options that are included in the manual page, as well as summarising them when you use the "-h" option.
(Unfortunately, it is not possible to have a `use' dependency automatically add structure to your POD yet, so you have to include the above manually. If you want your help message and Perldoc to be meaningful, that is.)
Shortcuts are provided to help you abort or die with various error conditions; all of which print the name of the program running (taken from $0 if not passed). The motive for this is that "small" scripts tend to just get written and forgotten; so, when you have a larger system that is built out of lots of these pieces it is sometimes guesswork figuring out which script a printed message comes from!
For instance, if your program is called with invalid arguments, you may simply call "abort" with a one-line message saying what the particular problem was. When the script is run, it will invite the person running the script to try to use the "--help" option, which gives them a summary and in turn invites them to read the Perldoc. So, it reads well in the source;
@ARGV and abort "unexpected arguments: @ARGV"; $file or abort "no filename supplied";
And in the output;
somescript: no filename supplied! Try `somescript --help' for a summary of options
On the other hand, if you call "barf", then it is considered to be a hard run-time failure, and the invitation to read the "--help" page to get usage not given. Also, the messages are much tidier than you get with "die" et al.
open FOO, "<$file" or barf "failed to open $file; $!";
Which will print:
somescript: failed to open somefile; Permission denied
Scriptalicious has no hard dependencies; all the methods, save reading passwords from the user, will work in the absence of extra installed modules on all versions of Perl from 5.6.0 onwards.
To avoid unnecessary explicit importing of symbols, the following symbols and functions are exported into the caller's namespace:
The configuration file is expected to be in ~/.PROGNAMErc, /etc/perl/PROGNAME.conf, or /etc/PROGNAME.conf. Only the first found file is read, and unknown options are ignored for the time being.
The file is expected to be in YAML format, with the top entity being a hash, and the keys of the hash being the same as specifying options on the command line. Using YAML as a format allows some simplificiations to getopt-style processing - "=s%" and "=s@" style options are expected to be in a real hash or list format in the config file, and boolean options must be set to "true" or "false" (or some common equivalents).
Returns the configuration file as Load()'ed by YAML in scalar context, or the argument list it was passed in list context.
For example, this minimal script (missing the documentation, but hey at least it's a start);
getopt getconf ( "something|s" => \$foo, "invertable|I!" => \$invertable, "integer|i=i" => \$bar, "string|s=s" => \$cheese, "list|l=s@" => \@list, "hash|H=s%" => \%hash, );
Will accept the following invocation styles;
foo.pl --help foo.pl --version foo.pl --verbose foo.pl --debug foo.pl --quiet foo.pl --something foo.pl --invertable foo.pl --no-invertable <=== FORM DIFFERS IN CONFIG FILE foo.pl --integer=7 foo.pl --string=anything foo.pl --list one --list two --list three foo.pl --hash foo=bar --hash baz=cheese
Equivalent config files:
something: 1 invertable: on invertable: off integer: 7 string: anything list: - one - two - three list: [ one, two, three ] hash: foo: bar baz: cheese
Note that more complex and possibly arcane usages of Getopt::Long features may not work with getconf (patches welcome).
This can be handy for things like database connection strings; all you have to do is make an option for them, and then the user of the script, or a SysAdmin can set up their own config file for the script to automatically set the options.
"run()" and the three alternatives listed below may perform arbitrary filehandle redirection before executing the command. This can be a very convenient way to do shell-like filehandle plumbing.
For example;
run( -in => sub { print "Hello, world!\n" }, -out => "/tmp/outfile", -out2 => "/dev/null", @command );
This will connect the child process' standard input ("-in") to a closure that is printing ""Hello, world!\n"". The output from the closure will appear on standard input of the run command. Note that the closure is run in a sub-process and so will not be able to set variables in the calling program.
It will also connect the program's standard output ("-out") to "/tmp/outfile", and its standard error (filehandle 2) to "/dev/null" (the traditional way of junking error output).
If you wanted to connect two filehandles to the same place, you could pass in "GLOB" references instead;
run( -out => \*MYOUT, -out2 => \*MYOUT, @command );
Any filehandle can be opened in any mode; "-in" merely defaults to meaning "-in0", and "-out" defaults to meaning "-out1". There is no "-err"; use "-out2". "-rw" exists (defaulting to "-rw0"), but is probably of limited use.
Here is an example of using "prompt_passwd()" to hijack "gpg"'s password grabbing;
my $password = prompt_passwd("Encryption password: "); my $encrypted = run( -in4 => sub { print "$password\n" }, "gpg", "--passphrase-fd", "4", "-c", $file )
Usage:
my ($rc, @output) = capture_err("somecommand", @args);
From Scriptalicious 1.08, the "u" character is used in place of the Greek "mu" due to encoding compatibility issues.
examples:
time_unit(10.1) => "10.10s" time_unit(1) => "1.000s" time_unit(0.1) => "100ms" time_unit(86401,2) => "1d 0h" time_unit(86401,3) => "1d 0h" time_unit(86401) => "1d 0h:0m" time_unit(86400+3600/2) => "1d 0h:30m" time_unit(86401,5) => "1d 0h:0m:1s" time_unit(7*86400) => "1w0d 0h" time_unit(-0.0002) => "-200us"
Note that this differs from the previous behaviour of prompt_regex, which took a sub.
You can also spell these as "prompt_nY" and "prompt_Ny".
use Scriptalicious; tsay hello => { name => "Bernie" }; __END__ __hello__ Hello, [% name %] [% INCLUDE yomomma %] __yomomma__ Yo momma's so fat your family portrait has stretchmarks.
This will print:
Hello, Bernie Yo momma's so fat your family portrait has stretchmarks.
Note that the script goes to lengths to make sure that the information is always printed whether or not Template Toolkit is installed. This gets pretty verbose, but at least solves the "argh! that script failed, but I don't know why because it needed this huge dependency to tell me" problem.
For example, the above would be printed as:
scriptname:
Simon Cozen's Getopt::Auto module does a very similar thing to this module, in a quite different way. However, it is missing "say", "run", etc. So you'll need to use some other module for those. But it does have some other features you might like and is probably engineered better.
There's a template script at "Documentation and help texts" in Getopt::Long that contains a script template that demonstrates what is necessary to get the basic man page / usage things working with the traditional Getopt::Long and Pod::Usage combination.
Getopt::Plus is a swiss army chainsaw of Getopt::* style modules, contrasting to this module's approach of elegant simplicity (quiet in the cheap seats!).
Getopt::EUCLID is Damian Conway's take on this.
Finally, if you don't mind the dependencies of Moose (or Mouse), then MooseX::Getopt and MooseX::SimpleConfig are much more elegant approaches to getopt handling and configuration than this module.
If you have solved this problem in a new and interesting way, or even rehashed it in an old, boring and inelegant way and want your module to be listed here, please contact the
Sam Vilain, samv@cpan.org
Copyright 2005-2008, Sam Vilain. All rights reserved. This program is free software; you can use it and/or distribute it under the same terms as Perl itself; either the latest stable release of Perl when the module was written, or any subsequent stable release.
Please note that this applies retrospectively to all Scriptalicious releases; apologies for the lack of an explicit license.
2022-10-16 | perl v5.34.0 |