19 Nov 2009 23:53:18 UTC
- Distribution: ScriptUtil
- Module version: 0.02
- Source (raw)
- Browse (raw)
- How to Contribute
- Testers (95 / 0 / 29)
- KwaliteeBus factor: 0
- % Coverage
- License: unknown
- Activity24 month
- Download (6.45KB)
- MetaCPAN Explorer
- Subscribe to distribution
- This version
- Latest versionCAMSTUART Cameron Stuart
- REQUIRED MODULES
- CHANGE LOG
- COPYRIGHT AND LICENSE
ScriptUtil - Object Orientated class to make typical command line scripts easier to write
use ScriptUtil; # All of these parameters are optional: my $script = ScriptUtil->new( 'verbose' => 1, # Output messages to STDOUT 'nolog' => 1, # Disable logging 'nolock' => 1, # Disable script locking 'log_debug' => 5 # Debug level for logging 'log_filename' => 'foo.log', # Log file name 'log_path' => '/tmp', # Location of log files 'log_mode' => '>>', # Log file mode 'log_stderrredirect' => 0, # Redirect stderr into log 'log_storeexptext' => 1, # Log store internally all exp text 'log_datetimestamp' => 1, # Timestamp log data entries 'log_logfiledatetime' => 1, # Timestamp the log file name 'log_rotation' => 1, # Do log rotation if logging is enabled 'log_zip_after_days' => 2, # Zip log files older than X days if log_rotation is enabled 'log_rm_after_days' => 5, # Delete log files older than X days if log_rotation is enabled ); # Put a message into the log file unless the 'nolog' flag has been set # Also print to STDOUT if the verbose flag has been set $script->echo("Boo"); # Put a message into the log file unless the 'nolog' flag has been set # And 'log_debug' is greater than or equal to 10 $script->echo("Boo", 10);
ScriptUtil is a class to make typical command line scripts easier to write.
The aim of this module is to reduce the amount of copy & paste and repeated 'scaffolding code' at the top of your scripts.
As time goes by I intend to add more methods for common operations, if you have some suggestions feel free to drop me a line.
Common operations such as:
log file initialization (File::Log)
log file rotation
locking (so only one instance of your scripts runs at a time)
benchmarking performance (Benchmark)
cleaning white space out of strings
Will be taken care of for you when you instantiate a new ScriptUtil object.
You can override some or all of the defaults, or leave them as is, see the examples section for more information
use ScriptUtil; my $script = ScriptUtil->new( verbose => 1, log_path => '/var/logs/foo', log_rotation => 1, log_debug => 1, ); $script->echo("Cleaning up a string", 1); my $string = "\t Foo Bar \t\t \n\n\n"; $script->echo("String Before Cleanup: [" . $string . "]", 10); # log_debug = 1 so you won't see this $string = $script->trim($string); $script->echo("String After Cleanup: [" . $string . "]"); # uses default debug level you will see this $script->verbose(0); $script->echo("You will only see this message in the log file", 1);
There are no class methods, the object methods are described below. Private methods start with the underscore character '_' and should be treated as Private.
Called to create a ScriptUtil object. The following named parameters can be passed to the constructor in Moose style and they are all optional:
Used to determine how noisy the script should be. when
echois called this attribute will determine if messages should be printed to STDOUT. The default behavior is off (false).
Logging is done via Greg George's handy File::Log object by default. If you don't want logging set this to false. The default is on (true) IE logging will be done by default.
Disable script locking, when your script instantiates a ScriptUtil object, ScriptUtil will open your script in append mode and attempt to get get an exclusive lock. If it is unable to get a lock, ScriptUtil will
confessabout it. The default behavior is off (false) IE scripts will be locked by default.
Debug level for logging, see File::Log for more information. Default level is 5.
Log file name if logging is enabled, see File::Log for more information. The default value is
Location of log files if logging is enabled. The default value is
Log file mode, see File::Log for more information. The default value is >> IE append mode.
Redirect STDERR into log file, see File::Log for more information. You should probably leave this off if you are running in verbose mode. The default behavior is off (false) IE STDERR will not be redirected into logs.
Log store internally all exp text, see File::Log for more information. The default value is on (true).
Timestamp log data entries, see File::Log for more information. The default value is on (true).
Timestamp the log file name, see File::Log for more information. If you are using log rotation this will be set to on automatically. The default value is on (true).
Do log rotation if logging is enabled. The default value is off (false).
Zip log files older than X days if log_rotation is enabled. The default value is 2 days.
This can be set to
0days if you don't want zipping.
Delete log files older than X days if log_rotation is enabled. The default value is 31 days.
This can be set to
0days if you don't want deletion.
Outputs print messages into the log file unless
nologhas been set, and to STDOUT if
verbosehas been set.
Takes a string and an optional integer as arguments.
# Usage: echo(STRING message, INTEGER debug level) # Examples: $script->echo("This is a message"); $script->echo("This is a message with a debug level", 10);
Trims white space (tabs and spaces) out of a string.
Takes a string as an argument, returns a string.
# Usage: STRING = trim(STRING text) # Example: my $trimmed_string = $script->trim("\t \t This is a string that needs trimming ");
Updated Makfile.PL to correct dependancy problems
Cameron Stuart firstname.lastname@example.org
Copyright (C) 2009 by Cameron Stuart
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available.